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

View file

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