diff --git a/src/services/exportService.ts b/src/services/exportService.ts index 5f54f0300..1ad0e007a 100644 --- a/src/services/exportService.ts +++ b/src/services/exportService.ts @@ -48,9 +48,8 @@ export interface ExportProgress { current: number; total: number; } -export interface ExportedCollection { - collectionID: number; - folderPath: string; +export interface ExportedCollectionPaths { + [collectionID: number]: string; } export interface ExportStats { failed: number; @@ -67,7 +66,7 @@ export interface ExportRecord { queuedFiles?: string[]; exportedFiles?: string[]; failedFiles?: string[]; - exportedCollections?: ExportedCollection[]; + exportedCollectionPaths?: ExportedCollectionPaths; } export enum ExportStage { INIT, @@ -211,15 +210,15 @@ class ExportService { exportDir: string ): Promise<{ paused: boolean }> { try { - if (!newCollections?.length) { + if (newCollections?.length) { await this.createNewCollectionFolders( newCollections, exportDir, collectionIDPathMap ); } - if (!renamedCollections?.length) { - await this.renamedFolderForRenamedCollections( + if (renamedCollections?.length) { + await this.renameCollectionFolders( renamedCollections, exportDir, collectionIDPathMap @@ -349,16 +348,14 @@ class ExportService { collectionFolderPath: string ) { const exportRecord = await this.getExportRecord(folder); - if (!exportRecord?.exportedCollections) { - exportRecord.exportedCollections = []; + if (!exportRecord?.exportedCollectionPaths) { + exportRecord.exportedCollectionPaths = {}; } - exportRecord.exportedCollections.push({ - collectionID: collection.id, - folderPath: collectionFolderPath, - }); - exportRecord.exportedCollections = dedupe( - exportRecord.exportedCollections - ); + exportRecord.exportedCollectionPaths = { + ...exportRecord.exportedCollectionPaths, + [collection.id]: collectionFolderPath, + }; + await this.updateExportRecord(exportRecord, folder); } @@ -366,7 +363,7 @@ class ExportService { const response = this.exportRecordUpdater.queueUpRequest(() => this.updateExportRecordHelper(folder, newData) ); - response.promise; + await response.promise; } async updateExportRecordHelper(folder: string, newData: ExportRecord) { try { @@ -426,7 +423,7 @@ class ExportService { collectionIDPathMap.set(collection.id, collectionFolderPath); } } - async renamedFolderForRenamedCollections( + async renameCollectionFolders( renamedCollections: Collection[], exportFolder: string, collectionIDPathMap: CollectionIDPathMap diff --git a/src/utils/export/index.ts b/src/utils/export/index.ts index 7ca477fce..06a949eac 100644 --- a/src/utils/export/index.ts +++ b/src/utils/export/index.ts @@ -18,8 +18,9 @@ export const getExportQueuedFiles = ( const queuedFiles = new Set(exportRecord?.queuedFiles); const unExportedFiles = allFiles.filter((file) => { if (queuedFiles.has(getExportRecordFileUID(file))) { - return file; + return true; } + return false; }); return unExportedFiles; }; @@ -29,12 +30,13 @@ export const getCollectionsCreatedAfterLastExport = ( exportRecord: ExportRecord ) => { const exportedCollections = new Set( - (exportRecord?.exportedCollections ?? []).map((c) => c.collectionID) + Object.keys(exportRecord?.exportedCollectionPaths).map((x) => Number(x)) ); const unExportedCollections = collections.filter((collection) => { if (!exportedCollections.has(collection.id)) { - return collection; + return true; } + return false; }); return unExportedCollections; }; @@ -42,10 +44,11 @@ export const getCollectionIDPathMapFromExportRecord = ( exportRecord: ExportRecord ): CollectionIDPathMap => { return new Map( - (exportRecord.exportedCollections ?? []).map((c) => [ - c.collectionID, - c.folderPath, - ]) + (Object.entries(exportRecord.exportedCollectionPaths) ?? []).map( + (e) => { + return [Number(e[0]), String(e[1])]; + } + ) ); }; @@ -56,12 +59,20 @@ export const getCollectionsRenamedAfterLastExport = ( const collectionIDPathMap = getCollectionIDPathMapFromExportRecord(exportRecord); const renamedCollections = collections.filter((collection) => { - if ( - collectionIDPathMap.has(collection.id) && - collectionIDPathMap.get(collection.id) !== collection.name - ) { - return collection; + if (collectionIDPathMap.has(collection.id)) { + const currentFolderName = collectionIDPathMap.get(collection.id); + const startIndex = currentFolderName.lastIndexOf('/'); + const lastIndex = currentFolderName.lastIndexOf('('); + const nameRoot = currentFolderName.slice( + startIndex + 1, + lastIndex !== -1 ? lastIndex : currentFolderName.length + ); + + if (nameRoot !== sanitizeName(collection.name)) { + return true; + } } + return false; }); return renamedCollections; }; @@ -73,8 +84,9 @@ export const getFilesUploadedAfterLastExport = ( const exportedFiles = new Set(exportRecord?.exportedFiles); const unExportedFiles = allFiles.filter((file) => { if (!exportedFiles.has(getExportRecordFileUID(file))) { - return file; + return true; } + return false; }); return unExportedFiles; }; @@ -86,8 +98,9 @@ export const getExportedFiles = ( const exportedFileIds = new Set(exportRecord?.exportedFiles); const exportedFiles = allFiles.filter((file) => { if (exportedFileIds.has(getExportRecordFileUID(file))) { - return file; + return true; } + return false; }); return exportedFiles; }; @@ -99,8 +112,9 @@ export const getExportFailedFiles = ( const failedFiles = new Set(exportRecord?.failedFiles); const filesToExport = allFiles.filter((file) => { if (failedFiles.has(getExportRecordFileUID(file))) { - return file; + return true; } + return false; }); return filesToExport; }; @@ -170,7 +184,7 @@ export const getUniqueFileSaveName = ( while ( exportService.exists(getFileSavePath(collectionPath, fileSaveName)) ) { - const filenameParts = splitFilenameAndExtension(filename); + const filenameParts = splitFilenameAndExtension(sanitizeName(filename)); if (filenameParts[1]) { fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; } else {