diff --git a/src/components/WatchFolder/index.tsx b/src/components/WatchFolder/index.tsx index 0c9f90e6b..8bbed4a25 100644 --- a/src/components/WatchFolder/index.tsx +++ b/src/components/WatchFolder/index.tsx @@ -9,7 +9,7 @@ import constants from 'utils/strings/constants'; import DialogBoxBase from 'components/DialogBox/base'; import DialogTitleWithCloseButton from 'components/DialogBox/titleWithCloseButton'; import UploadStrategyChoiceModal from 'components/pages/gallery/UploadStrategyChoiceModal'; -import { UPLOAD_STRATEGY } from 'components/pages/gallery/Upload'; +import { UPLOAD_STRATEGY } from 'constants/upload'; interface Iprops { open: boolean; diff --git a/src/components/pages/gallery/Upload.tsx b/src/components/pages/gallery/Upload.tsx index 86d9451a1..5e57c1806 100644 --- a/src/components/pages/gallery/Upload.tsx +++ b/src/components/pages/gallery/Upload.tsx @@ -33,7 +33,7 @@ import { SegregatedFinishedUploads, InProgressUpload, } from 'types/upload/ui'; -import { UPLOAD_STAGES } from 'constants/upload'; +import { UPLOAD_STAGES, UPLOAD_STRATEGY } from 'constants/upload'; const FIRST_ALBUM_NAME = 'My First Album'; @@ -59,11 +59,6 @@ interface Props { showSessionExpiredMessage: () => void; } -export enum UPLOAD_STRATEGY { - SINGLE_COLLECTION, - COLLECTION_PER_FOLDER, -} - export enum DESKTOP_UPLOAD_TYPE { FILES = 'files', FOLDERS = 'folders', diff --git a/src/constants/upload/index.ts b/src/constants/upload/index.ts index 6056600e3..33835a80b 100644 --- a/src/constants/upload/index.ts +++ b/src/constants/upload/index.ts @@ -31,6 +31,11 @@ export enum UPLOAD_STAGES { FINISH, } +export enum UPLOAD_STRATEGY { + SINGLE_COLLECTION, + COLLECTION_PER_FOLDER, +} + export enum UPLOAD_RESULT { FAILED, ALREADY_UPLOADED, diff --git a/src/services/upload/livePhotoService.ts b/src/services/upload/livePhotoService.ts index 740c4da2a..1b2000cc1 100644 --- a/src/services/upload/livePhotoService.ts +++ b/src/services/upload/livePhotoService.ts @@ -1,7 +1,6 @@ import { FILE_TYPE } from 'constants/file'; import { LIVE_PHOTO_ASSET_SIZE_LIMIT } from 'constants/upload'; import { encodeMotionPhoto } from 'services/motionPhotoService'; -import { FileMagicMetadata } from 'types/file'; import { ElectronFile, FileTypeInfo, @@ -62,12 +61,8 @@ export function getLivePhotoMetadata( }; } -export function getLivePhotoMagicMetadata( - imageFile: FileWithCollection -): FileMagicMetadata['data'] { - return { - filePaths: [getLivePhotoName((imageFile.file as any).path as string)], - }; +export function getLivePhotoFilePath(imageAsset: Asset): string { + return getLivePhotoName((imageAsset.file as any).path); } export function getLivePhotoSize(livePhotoAssets: LivePhotoAssets) { @@ -198,12 +193,11 @@ export function clusterLivePhotoFiles(mediaFiles: FileWithCollection[]) { imageAsset.metadata, videoAsset.metadata ); - const livePhotoMagicMetadata: FileMagicMetadata['data'] = - getLivePhotoMagicMetadata(firstMediaFile); + const livePhotoPath = getLivePhotoFilePath(imageAsset); uploadService.setFileMetadataAndFileTypeInfo(livePhotoLocalID, { fileTypeInfo: { ...livePhotoFileTypeInfo }, metadata: { ...livePhotoMetadata }, - magicMetadata: { ...livePhotoMagicMetadata }, + filePath: livePhotoPath, }); index += 2; } else { diff --git a/src/services/upload/uploadManager.ts b/src/services/upload/uploadManager.ts index b4d9eeede..276410dc5 100644 --- a/src/services/upload/uploadManager.ts +++ b/src/services/upload/uploadManager.ts @@ -1,4 +1,8 @@ -import { getLocalFiles, setLocalFiles } from '../fileService'; +import { + getLocalFiles, + setLocalFiles, + updateFileMagicMetadata, +} from '../fileService'; import { SetFiles } from 'types/gallery'; import { getDedicatedCryptoWorker } from 'utils/crypto'; import { @@ -6,7 +10,7 @@ import { sortFiles, preservePhotoswipeProps, decryptFile, - changeFilePaths, + appendNewFilePath, } from 'utils/file'; import { logError } from 'utils/sentry'; import { getMetadataJSONMapKey, parseMetadataJSON } from './metadataService'; @@ -20,7 +24,7 @@ import UIService from './uiService'; import UploadService from './uploadService'; import { CustomError } from 'utils/error'; import { Collection } from 'types/collection'; -import { EnteFile, FileMagicMetadata } from 'types/file'; +import { EnteFile } from 'types/file'; import { FileWithCollection, MetadataAndFileTypeInfo, @@ -230,7 +234,7 @@ class UploadManager { UIService.reset(mediaFiles.length); for (const { file, localID, collectionID } of mediaFiles) { try { - const { fileTypeInfo, metadata, magicMetadata } = + const { fileTypeInfo, metadata, filePath } = await (async () => { if (file.size >= MAX_FILE_SIZE_SUPPORTED) { logUploadInfo( @@ -260,10 +264,8 @@ class UploadManager { collectionID, fileTypeInfo )) || null; - const magicMetadata = { - filePaths: [(file as any).path as string], - } as FileMagicMetadata['data']; - return { fileTypeInfo, metadata, magicMetadata }; + const filePath = (file as any).path as string; + return { fileTypeInfo, metadata, filePath }; })(); logUploadInfo( @@ -274,7 +276,7 @@ class UploadManager { this.metadataAndFileTypeInfoMap.set(localID, { fileTypeInfo: fileTypeInfo && { ...fileTypeInfo }, metadata: metadata && { ...metadata }, - magicMetadata: magicMetadata && { ...magicMetadata }, + filePath: filePath, }); UIService.increaseFileUploaded(); } catch (e) { @@ -340,25 +342,18 @@ class UploadManager { this.existingFiles, fileWithCollection ); - const filePaths = UploadService.getFileMetadataAndFileTypeInfo( - fileWithCollection.localID - ).magicMetadata.filePaths; - await changeFilePaths( - fileUploadResult, - uploadedFile, - fileWithCollection.collection.key, - filePaths - ); - UIService.moveFileToResultList( - fileWithCollection.localID, - fileUploadResult - ); - UploadService.reducePendingUploadCount(); - await this.postUploadTask( + + const finalUploadResult = await this.postUploadTask( fileUploadResult, uploadedFile, fileWithCollection ); + + UIService.moveFileToResultList( + fileWithCollection.localID, + finalUploadResult + ); + UploadService.reducePendingUploadCount(); } } @@ -383,6 +378,10 @@ class UploadManager { uploadedFile ); } + await this.updateFilePaths( + uploadedFile, + fileWithCollection + ); break; case UPLOAD_RESULT.ADDED_SYMLINK: decryptedFile = uploadedFile; @@ -404,14 +403,16 @@ class UploadManager { fileWithCollection, uploadedFile ); + await this.updateFilePaths(decryptedFile, fileWithCollection); } + return fileUploadResult; } catch (e) { logError(e, 'failed to do post file upload action'); logUploadInfo( `failed to do post file upload action -> ${e.message} ${(e as Error).stack}` ); - throw e; + return UPLOAD_RESULT.FAILED; } } @@ -457,6 +458,18 @@ class UploadManager { } } + private async updateFilePaths( + decryptedFile: EnteFile, + fileWithCollection: FileWithCollection + ) { + const filePath = UploadService.getFileMetadataAndFileTypeInfo( + fileWithCollection.localID + ).filePath; + + const updatedFile = await appendNewFilePath(decryptedFile, filePath); + await updateFileMagicMetadata([updatedFile]); + } + async retryFailedFiles() { await this.queueFilesForUpload(this.failedFiles, [ ...this.collections.values(), diff --git a/src/services/watchFolder/watchFolderService.ts b/src/services/watchFolder/watchFolderService.ts index 61999ee6a..717b6daf2 100644 --- a/src/services/watchFolder/watchFolderService.ts +++ b/src/services/watchFolder/watchFolderService.ts @@ -16,8 +16,8 @@ import { diskFileRemovedCallback, diskFolderRemovedCallback, } from './watchFolderEventHandlers'; -import { UPLOAD_STRATEGY } from 'components/pages/gallery/Upload'; import { getParentFolderName } from './utils'; +import { UPLOAD_STRATEGY } from 'constants/upload'; class watchFolderService { private ElectronAPIs: ElectronAPIsInterface; diff --git a/src/types/upload/index.ts b/src/types/upload/index.ts index e473f419f..f42d5ead6 100644 --- a/src/types/upload/index.ts +++ b/src/types/upload/index.ts @@ -1,6 +1,6 @@ import { FILE_TYPE } from 'constants/file'; import { Collection } from 'types/collection'; -import { fileAttribute, FileMagicMetadata } from 'types/file'; +import { fileAttribute } from 'types/file'; export interface DataStream { stream: ReadableStream; @@ -92,7 +92,7 @@ export interface FileWithCollection extends UploadAsset { export interface MetadataAndFileTypeInfo { metadata: Metadata; fileTypeInfo: FileTypeInfo; - magicMetadata: FileMagicMetadata['data']; + filePath: string; } export type MetadataAndFileTypeInfoMap = Map; diff --git a/src/types/watchFolder/index.ts b/src/types/watchFolder/index.ts index 1c02e4b44..97fa96dce 100644 --- a/src/types/watchFolder/index.ts +++ b/src/types/watchFolder/index.ts @@ -1,4 +1,4 @@ -import { UPLOAD_STRATEGY } from 'components/pages/gallery/Upload'; +import { UPLOAD_STRATEGY } from 'constants/upload'; import { ElectronFile } from 'types/upload'; export interface WatchMapping { diff --git a/src/utils/file/index.ts b/src/utils/file/index.ts index ceed4ba2f..c009d1ab9 100644 --- a/src/utils/file/index.ts +++ b/src/utils/file/index.ts @@ -26,8 +26,7 @@ import ffmpegService from 'services/ffmpeg/ffmpegService'; import { NEW_FILE_MAGIC_METADATA, VISIBILITY_STATE } from 'types/magicMetadata'; import { IsArchived, updateMagicMetadataProps } from 'utils/magicMetadata'; import { ARCHIVE_SECTION, TRASH_SECTION } from 'constants/collection'; -import { UPLOAD_RESULT } from 'constants/upload'; -import { updateFileMagicMetadata } from 'services/fileService'; + export function downloadAsFile(filename: string, content: string) { const file = new Blob([content], { type: 'text/plain', @@ -454,35 +453,24 @@ export async function changeFileName(file: EnteFile, editedName: string) { return file; } -export const changeFilePaths = async ( - fileUploadResult: UPLOAD_RESULT, - file: EnteFile, - collectionKey: string, - filePaths: string[] -) => { - if ( - fileUploadResult === UPLOAD_RESULT.UPLOADED || - fileUploadResult === UPLOAD_RESULT.ALREADY_UPLOADED || - fileUploadResult === UPLOAD_RESULT.UPLOADED_WITH_STATIC_THUMBNAIL - ) { - let mergedMetadataFilePaths = [...filePaths]; - if (file.magicMetadata?.data.filePaths?.length > 0) { - mergedMetadataFilePaths = [ - ...new Set([ - ...file.magicMetadata.data.filePaths, - ...filePaths, - ]), - ]; - } - file.key = await getFileKey(file, collectionKey); - const updatedMagicMetadata = await updateMagicMetadataProps( - file.magicMetadata ?? NEW_FILE_MAGIC_METADATA, - file.key, - { filePaths: mergedMetadataFilePaths } - ); - file.magicMetadata = updatedMagicMetadata; - await updateFileMagicMetadata([file]); +export const appendNewFilePath = async (file: EnteFile, filePath: string) => { + let mergedMetadataFilePaths = [filePath]; + if (file.magicMetadata?.data.filePaths?.length > 0) { + mergedMetadataFilePaths = [ + ...new Set([ + ...file.magicMetadata.data.filePaths, + ...mergedMetadataFilePaths, + ]), + ]; } + const updatedMagicMetadata = await updateMagicMetadataProps( + file.magicMetadata ?? NEW_FILE_MAGIC_METADATA, + file.key, + { filePaths: mergedMetadataFilePaths } + ); + file.magicMetadata = updatedMagicMetadata; + + return file; }; export function isSharedFile(file: EnteFile) {