From 3dd90ba538d62aded22d23ed573044b7321eb913 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 7 Dec 2021 14:16:37 +0530 Subject: [PATCH] fix getUniqueFileSaveName , by providing collectionPath too for checking if file already exists at the fileSavePath --- src/services/exportService.ts | 20 ++++++++++++++++---- src/utils/export/index.ts | 14 ++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/services/exportService.ts b/src/services/exportService.ts index 6a063dabc..7e33b22a7 100644 --- a/src/services/exportService.ts +++ b/src/services/exportService.ts @@ -351,7 +351,10 @@ class ExportService { async downloadAndSave(file: File, collectionPath: string) { file.metadata = mergeMetadata([file])[0].metadata; - const fileSaveName = getUniqueFileSaveName(file.metadata.title); + const fileSaveName = getUniqueFileSaveName( + collectionPath, + file.metadata.title + ); let fileStream = await retryAsyncFunction(() => downloadManager.downloadFile(file) ); @@ -384,12 +387,18 @@ class ExportService { const originalName = fileNameWithoutExtension(file.metadata.title); const motionPhoto = await decodeMotionPhoto(fileBlob, originalName); const imageStream = generateStreamFromArrayBuffer(motionPhoto.image); - const imageSaveName = getUniqueFileSaveName(motionPhoto.imageNameTitle); + const imageSaveName = getUniqueFileSaveName( + collectionPath, + motionPhoto.imageNameTitle + ); this.saveMediaFile(collectionPath, imageSaveName, imageStream); this.saveMetadataFile(collectionPath, imageSaveName, file.metadata); const videoStream = generateStreamFromArrayBuffer(motionPhoto.video); - const videoSaveName = getUniqueFileSaveName(motionPhoto.videoNameTitle); + const videoSaveName = getUniqueFileSaveName( + collectionPath, + motionPhoto.videoNameTitle + ); this.saveMediaFile(collectionPath, videoSaveName, videoStream); this.saveMetadataFile(collectionPath, videoSaveName, file.metadata); } @@ -500,7 +509,10 @@ class ExportService { file ); file = mergeMetadata([file])[0]; - const newFileSaveName = getUniqueFileSaveName(file.metadata.title); + const newFileSaveName = getUniqueFileSaveName( + collectionIDPathMap.get(file.collectionID), + file.metadata.title + ); const newFileSavePath = getFileSavePath( collectionIDPathMap.get(file.collectionID), diff --git a/src/utils/export/index.ts b/src/utils/export/index.ts index 9efc84826..fcb7fdc03 100644 --- a/src/utils/export/index.ts +++ b/src/utils/export/index.ts @@ -118,16 +118,22 @@ export const getUniqueCollectionFolderPath = ( export const getMetadataFolderPath = (collectionFolderPath: string) => `${collectionFolderPath}/${METADATA_FOLDER_NAME}`; -export const getUniqueFileSaveName = (filename: string) => { +export const getUniqueFileSaveName = ( + collectionPath: string, + filename: string +) => { let fileSaveName = sanitizeName(filename); - const count = 1; - while (exportService.exists(fileSaveName)) { - const filenameParts = splitFilenameAndExtension(fileSaveName); + let count = 1; + while ( + exportService.exists(getFileSavePath(collectionPath, fileSaveName)) + ) { + const filenameParts = splitFilenameAndExtension(filename); if (filenameParts[1]) { fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; } else { fileSaveName = `${filenameParts[0]}(${count})`; } + count++; } return fileSaveName; };