From e6ac93cf70041e17089c1c3d005841cd8661defa Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 24 Apr 2023 23:45:04 +0530 Subject: [PATCH] added migration for already exported files --- src/services/exportService.ts | 35 +++++++++++++++++++++++++++++++++++ src/types/export/index.ts | 8 -------- src/utils/export/index.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/services/exportService.ts b/src/services/exportService.ts index aec186766..73272467f 100644 --- a/src/services/exportService.ts +++ b/src/services/exportService.ts @@ -5,6 +5,7 @@ import { getExportRecordFileUID, getUniqueCollectionFolderPath, getUniqueFileSaveName, + getUniqueFileSaveNameForMigration, getOldFileSavePath, getOldCollectionFolderPath, getFileMetadataSavePath, @@ -14,6 +15,7 @@ import { getMetadataFolderPath, getCollectionsRenamedAfterLastExport, convertIDPathObjectToMap, + convertMapToIDPathObject, } from 'utils/export'; import { retryAsyncFunction } from 'utils/network'; import { logError } from 'utils/sentry'; @@ -666,6 +668,15 @@ class ExportService { version: currentVersion, }); } + if (currentVersion === 2) { + await this.addExportedFilePathsProperty( + getExportedFiles(files, exportRecord) + ); + currentVersion++; + await this.updateExportRecord({ + version: currentVersion, + }); + } } /* @@ -760,5 +771,29 @@ class ExportService { } await this.updateExportRecord(exportRecord); } + + private async addExportedFilePathsProperty(exportedFiles: EnteFile[]) { + const exportRecord = await this.getExportRecord(); + const exportedFilePaths = new Map(); + const usedFilePaths = new Set(); + const exportedCollectionPaths = convertIDPathObjectToMap( + exportRecord?.exportedCollectionPaths + ); + for (const file of exportedFiles) { + const collectionPath = exportedCollectionPaths.get( + file.collectionID + ); + const fileSaveName = getUniqueFileSaveNameForMigration( + collectionPath, + file.metadata.title, + usedFilePaths + ); + const filePath = getFileSavePath(collectionPath, fileSaveName); + exportedFilePaths.set(file.id, filePath); + } + exportRecord.exportedFilePaths = + convertMapToIDPathObject(exportedFilePaths); + await this.updateExportRecord(exportRecord); + } } export default new ExportService(); diff --git a/src/types/export/index.ts b/src/types/export/index.ts index 6541c0808..703cae5c1 100644 --- a/src/types/export/index.ts +++ b/src/types/export/index.ts @@ -25,14 +25,6 @@ export interface ExportRecordV1 { exportedCollectionPaths?: ExportedEntityPaths; } -export interface ExportRecordV2 { - version: number; - stage: ExportStage; - lastAttemptTimestamp: number; - exportedFiles: string[]; - exportedCollectionPaths: ExportedEntityPaths; -} - export interface ExportRecord { version: number; stage: ExportStage; diff --git a/src/utils/export/index.ts b/src/utils/export/index.ts index a8b235a1c..64389dcbf 100644 --- a/src/utils/export/index.ts +++ b/src/utils/export/index.ts @@ -40,6 +40,16 @@ export const convertIDPathObjectToMap = ( ); }; +export const convertMapToIDPathObject = ( + exportedEntityPaths: Map +) => { + const exportedEntityPathsObject: ExportedEntityPaths = {}; + exportedEntityPaths.forEach((value, key) => { + exportedEntityPathsObject[key] = value; + }); + return exportedEntityPathsObject; +}; + export const getCollectionsRenamedAfterLastExport = ( collections: Collection[], exportRecord: ExportRecord @@ -212,3 +222,23 @@ export const getOldFileMetadataSavePath = ( `${collectionFolderPath}/${ENTE_METADATA_FOLDER}/${ file.id }_${oldSanitizeName(file.metadata.title)}.json`; + +export const getUniqueFileSaveNameForMigration = ( + collectionPath: string, + filename: string, + usedFilePaths: Set +) => { + let fileSaveName = sanitizeName(filename); + let count = 1; + while (usedFilePaths.has(getFileSavePath(collectionPath, fileSaveName))) { + usedFilePaths.add(getFileSavePath(collectionPath, fileSaveName)); + const filenameParts = splitFilenameAndExtension(sanitizeName(filename)); + if (filenameParts[1]) { + fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; + } else { + fileSaveName = `${filenameParts[0]}(${count})`; + } + count++; + } + return fileSaveName; +};