added migration for already exported files

This commit is contained in:
Abhinav 2023-04-24 23:45:04 +05:30
parent 91c2bb7290
commit e6ac93cf70
3 changed files with 65 additions and 8 deletions

View file

@ -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<number, string>();
const usedFilePaths = new Set<string>();
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();

View file

@ -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;

View file

@ -40,6 +40,16 @@ export const convertIDPathObjectToMap = (
);
};
export const convertMapToIDPathObject = (
exportedEntityPaths: Map<number, string>
) => {
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<string>
) => {
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;
};