fix collection unique name and file name

This commit is contained in:
Abhinav 2021-11-17 14:54:26 +05:30
parent 2273dcdc76
commit 8d4dff4c38
2 changed files with 25 additions and 19 deletions

View file

@ -96,7 +96,7 @@ class ExportService {
private recordUpdateInProgress = Promise.resolve(); private recordUpdateInProgress = Promise.resolve();
private stopExport: boolean = false; private stopExport: boolean = false;
private pauseExport: boolean = false; private pauseExport: boolean = false;
private usedFilenames = new Map<number, Set<string>>(); private usedFileNames = new Map<number, Set<string>>();
constructor() { constructor() {
this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs']; this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs'];
@ -216,7 +216,6 @@ class ExportService {
collection.name, collection.name,
usedCollectionPaths usedCollectionPaths
); );
usedCollectionPaths.add(collectionFolderPath);
collectionIDPathMap.set(collection.id, collectionFolderPath); collectionIDPathMap.set(collection.id, collectionFolderPath);
await this.ElectronAPIs.checkExistsAndCreateCollectionDir( await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
collectionFolderPath collectionFolderPath
@ -358,7 +357,10 @@ class ExportService {
} }
async downloadAndSave(file: File, collectionPath: string) { async downloadAndSave(file: File, collectionPath: string) {
const usedFileNamesInCollection = this.usedFilenames.get( if (!this.usedFileNames.has(file.collectionID)) {
this.usedFileNames.set(file.collectionID, new Set<string>());
}
const usedFileNamesInCollection = this.usedFileNames.get(
file.collectionID file.collectionID
); );
file.metadata = mergeMetadata([file])[0].metadata; file.metadata = mergeMetadata([file])[0].metadata;
@ -382,7 +384,12 @@ class ExportService {
fileStream = updatedFileBlob.stream(); fileStream = updatedFileBlob.stream();
} }
if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) { if (file.metadata.fileType === FILE_TYPE.LIVE_PHOTO) {
this.exportMotionPhoto(fileStream, file, collectionPath); this.exportMotionPhoto(
fileStream,
file,
collectionPath,
usedFileNamesInCollection
);
} else { } else {
this.saveMediaFile(collectionPath, fileSaveName, fileStream); this.saveMediaFile(collectionPath, fileSaveName, fileStream);
this.saveMetadataFile(collectionPath, fileSaveName, file.metadata); this.saveMetadataFile(collectionPath, fileSaveName, file.metadata);
@ -392,14 +399,12 @@ class ExportService {
private async exportMotionPhoto( private async exportMotionPhoto(
fileStream: ReadableStream<any>, fileStream: ReadableStream<any>,
file: File, file: File,
collectionPath: string collectionPath: string,
usedFileNamesInCollection: Set<string>
) { ) {
const fileBlob = await new Response(fileStream).blob(); const fileBlob = await new Response(fileStream).blob();
const originalName = fileNameWithoutExtension(file.metadata.title); const originalName = fileNameWithoutExtension(file.metadata.title);
const motionPhoto = await decodeMotionPhoto(fileBlob, originalName); const motionPhoto = await decodeMotionPhoto(fileBlob, originalName);
const usedFileNamesInCollection = this.usedFilenames.get(
file.collectionID
);
const imageStream = generateStreamFromArrayBuffer(motionPhoto.image); const imageStream = generateStreamFromArrayBuffer(motionPhoto.image);
const imageSaveName = getUniqueFileSaveName( const imageSaveName = getUniqueFileSaveName(
motionPhoto.imageNameTitle, motionPhoto.imageNameTitle,

View file

@ -2,7 +2,7 @@ import { Collection } from 'services/collectionService';
import { ExportRecord, METADATA_FOLDER_NAME } from 'services/exportService'; import { ExportRecord, METADATA_FOLDER_NAME } from 'services/exportService';
import { File } from 'services/fileService'; import { File } from 'services/fileService';
import { MetadataObject } from 'services/upload/uploadService'; import { MetadataObject } from 'services/upload/uploadService';
import { formatDate } from 'utils/file'; import { formatDate, splitFilenameAndExtension } from 'utils/file';
export const getExportRecordFileUID = (file: File) => export const getExportRecordFileUID = (file: File) =>
`${file.id}_${file.collectionID}_${file.updationTime}`; `${file.id}_${file.collectionID}_${file.updationTime}`;
@ -102,15 +102,13 @@ export const getUniqueCollectionFolderPath = (
): string => { ): string => {
let collectionFolderPath = `${dir}/${sanitizeName(collectionName)}`; let collectionFolderPath = `${dir}/${sanitizeName(collectionName)}`;
let count = 1; let count = 1;
while ( while (usedCollectionPaths.has(collectionFolderPath)) {
usedCollectionPaths &&
usedCollectionPaths.has(collectionFolderPath)
) {
collectionFolderPath = `${dir}/${sanitizeName( collectionFolderPath = `${dir}/${sanitizeName(
collectionName collectionName
)}(${count})`; )}(${count})`;
count++; count++;
} }
usedCollectionPaths.add(collectionFolderPath);
return collectionFolderPath; return collectionFolderPath;
}; };
@ -119,16 +117,19 @@ export const getMetadataFolderPath = (collectionFolderPath: string) =>
export const getUniqueFileSaveName = ( export const getUniqueFileSaveName = (
filename: string, filename: string,
usedFileNamesInCollection: Set<string> usedFilenamesInCollection: Set<string>
) => { ) => {
let fileSaveName = sanitizeName(filename); let fileSaveName = sanitizeName(filename);
const count = 1; const count = 1;
while ( while (usedFilenamesInCollection.has(fileSaveName)) {
usedFileNamesInCollection && const filenameParts = splitFilenameAndExtension(fileSaveName);
usedFileNamesInCollection.has(fileSaveName) if (filenameParts[1]) {
) { fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`;
fileSaveName = sanitizeName(filename) + `(${count})`; } else {
fileSaveName = `${filenameParts[0]}(${count})`;
} }
}
usedFilenamesInCollection.add(fileSaveName);
return fileSaveName; return fileSaveName;
}; };