From 88c2a52edff644c7641d2b861380fce65be021b4 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Wed, 24 Apr 2024 10:37:51 +0530 Subject: [PATCH] Spruce --- .../src/services/upload/metadataService.ts | 141 ++++++++---------- .../photos/src/services/upload/takeout.ts | 5 +- .../src/services/upload/uploadManager.ts | 8 +- 3 files changed, 72 insertions(+), 82 deletions(-) diff --git a/web/apps/photos/src/services/upload/metadataService.ts b/web/apps/photos/src/services/upload/metadataService.ts index c3a9ca252a..a9d5fec4c8 100644 --- a/web/apps/photos/src/services/upload/metadataService.ts +++ b/web/apps/photos/src/services/upload/metadataService.ts @@ -34,7 +34,6 @@ import { getMetadataJSONMapKeyForFile, type ParsedMetadataJSON, } from "./takeout"; -import uploadCancelService from "./uploadCancelService"; import { getFileName } from "./uploadService"; const EXIF_TAGS_NEEDED = [ @@ -310,89 +309,77 @@ export function getLivePhotoSize(livePhotoAssets: LivePhotoAssets) { * single live photo when appropriate. */ export const clusterLivePhotos = (mediaFiles: FileWithCollection2[]) => { - try { - const result: FileWithCollection2[] = []; - mediaFiles - .sort((f, g) => - nameAndExtension(getFileName(f.file))[0].localeCompare( - nameAndExtension(getFileName(g.file))[0], - ), - ) - .sort((f, g) => f.collectionID - g.collectionID); - let index = 0; - while (index < mediaFiles.length - 1) { - if (uploadCancelService.isUploadCancelationRequested()) { - throw Error(CustomError.UPLOAD_CANCELLED); - } - const f = mediaFiles[index]; - const g = mediaFiles[index + 1]; - const fFileType = getFileTypeFromExtensionForLivePhotoClustering( - getFileName(f.file), - ); - const gFileType = getFileTypeFromExtensionForLivePhotoClustering( - getFileName(g.file), - ); - const fFileIdentifier: LivePhotoIdentifier = { - collectionID: f.collectionID, - fileType: fFileType, - name: getFileName(f.file), - /* TODO(MR): ElectronFile changes */ - size: (f as FileWithCollection).file.size, - }; - const gFileIdentifier: LivePhotoIdentifier = { - collectionID: g.collectionID, - fileType: gFileType, - name: getFileName(g.file), - /* TODO(MR): ElectronFile changes */ - size: (g as FileWithCollection).file.size, - }; - if (areLivePhotoAssets(fFileIdentifier, gFileIdentifier)) { - let imageFile: File | ElectronFile | string; - let videoFile: File | ElectronFile | string; - if ( - fFileType === FILE_TYPE.IMAGE && - gFileType === FILE_TYPE.VIDEO - ) { - imageFile = f.file; - videoFile = g.file; - } else { - videoFile = f.file; - imageFile = g.file; - } - const livePhotoLocalID = f.localID; - result.push({ - localID: livePhotoLocalID, - collectionID: f.collectionID, - isLivePhoto: true, - livePhotoAssets: { - image: imageFile, - video: videoFile, - }, - }); - index += 2; + const result: FileWithCollection2[] = []; + mediaFiles + .sort((f, g) => + nameAndExtension(getFileName(f.file))[0].localeCompare( + nameAndExtension(getFileName(g.file))[0], + ), + ) + .sort((f, g) => f.collectionID - g.collectionID); + let index = 0; + while (index < mediaFiles.length - 1) { + const f = mediaFiles[index]; + const g = mediaFiles[index + 1]; + const fFileType = getFileTypeFromExtensionForLivePhotoClustering( + getFileName(f.file), + ); + const gFileType = getFileTypeFromExtensionForLivePhotoClustering( + getFileName(g.file), + ); + const fFileIdentifier: LivePhotoIdentifier = { + collectionID: f.collectionID, + fileType: fFileType, + name: getFileName(f.file), + /* TODO(MR): ElectronFile changes */ + size: (f as FileWithCollection).file.size, + }; + const gFileIdentifier: LivePhotoIdentifier = { + collectionID: g.collectionID, + fileType: gFileType, + name: getFileName(g.file), + /* TODO(MR): ElectronFile changes */ + size: (g as FileWithCollection).file.size, + }; + if (areLivePhotoAssets(fFileIdentifier, gFileIdentifier)) { + let imageFile: File | ElectronFile | string; + let videoFile: File | ElectronFile | string; + if ( + fFileType === FILE_TYPE.IMAGE && + gFileType === FILE_TYPE.VIDEO + ) { + imageFile = f.file; + videoFile = g.file; } else { - result.push({ - ...f, - isLivePhoto: false, - }); - index += 1; + videoFile = f.file; + imageFile = g.file; } - } - if (index === mediaFiles.length - 1) { + const livePhotoLocalID = f.localID; result.push({ - ...mediaFiles[index], + localID: livePhotoLocalID, + collectionID: f.collectionID, + isLivePhoto: true, + livePhotoAssets: { + image: imageFile, + video: videoFile, + }, + }); + index += 2; + } else { + result.push({ + ...f, isLivePhoto: false, }); - } - return result; - } catch (e) { - if (e.message === CustomError.UPLOAD_CANCELLED) { - throw e; - } else { - log.error("failed to cluster live photo", e); - throw e; + index += 1; } } + if (index === mediaFiles.length - 1) { + result.push({ + ...mediaFiles[index], + isLivePhoto: false, + }); + } + return result; }; interface LivePhotoIdentifier { diff --git a/web/apps/photos/src/services/upload/takeout.ts b/web/apps/photos/src/services/upload/takeout.ts index 660b6d0e71..ba6f402b43 100644 --- a/web/apps/photos/src/services/upload/takeout.ts +++ b/web/apps/photos/src/services/upload/takeout.ts @@ -97,12 +97,11 @@ export const tryParseTakeoutMetadataJSON = async ( log.error("Failed to parse takeout metadata JSON", e); return undefined; } -} +}; const NULL_PARSED_METADATA_JSON: ParsedMetadataJSON = { creationTime: null, modificationTime: null, - latitude: null, longitude: null ...NULL_LOCATION, }; @@ -153,4 +152,4 @@ const parseMetadataJSONText = (text: string) => { parsedMetadataJSON.longitude = locationData.longitude; } return parsedMetadataJSON; -} +}; diff --git a/web/apps/photos/src/services/upload/uploadManager.ts b/web/apps/photos/src/services/upload/uploadManager.ts index 0cc9db38fc..04080c62c9 100644 --- a/web/apps/photos/src/services/upload/uploadManager.ts +++ b/web/apps/photos/src/services/upload/uploadManager.ts @@ -43,7 +43,7 @@ import { segregateMetadataAndMediaFiles2, } from "utils/upload"; import { getLocalFiles } from "../fileService"; -import { clusterLivePhotoFiles } from "./metadataService"; +import { clusterLivePhotoFiles, clusterLivePhotos } from "./metadataService"; import { getMetadataJSONMapKeyForJSON, tryParseTakeoutMetadataJSON, @@ -369,7 +369,11 @@ class UploadManager { } if (mediaFiles.length) { - const clusteredMediaFiles = await clusterLivePhotos(mediaFiles); + const clusteredMediaFiles = clusterLivePhotos(mediaFiles); + + if (uploadCancelService.isUploadCancelationRequested()) { + throw Error(CustomError.UPLOAD_CANCELLED); + } this.uiService.setFilenames( new Map(