This commit is contained in:
Abhinav 2021-12-07 17:33:11 +05:30
parent 27d9d703c7
commit 85d06fc528
2 changed files with 45 additions and 34 deletions

View file

@ -48,9 +48,8 @@ export interface ExportProgress {
current: number; current: number;
total: number; total: number;
} }
export interface ExportedCollection { export interface ExportedCollectionPaths {
collectionID: number; [collectionID: number]: string;
folderPath: string;
} }
export interface ExportStats { export interface ExportStats {
failed: number; failed: number;
@ -67,7 +66,7 @@ export interface ExportRecord {
queuedFiles?: string[]; queuedFiles?: string[];
exportedFiles?: string[]; exportedFiles?: string[];
failedFiles?: string[]; failedFiles?: string[];
exportedCollections?: ExportedCollection[]; exportedCollectionPaths?: ExportedCollectionPaths;
} }
export enum ExportStage { export enum ExportStage {
INIT, INIT,
@ -211,15 +210,15 @@ class ExportService {
exportDir: string exportDir: string
): Promise<{ paused: boolean }> { ): Promise<{ paused: boolean }> {
try { try {
if (!newCollections?.length) { if (newCollections?.length) {
await this.createNewCollectionFolders( await this.createNewCollectionFolders(
newCollections, newCollections,
exportDir, exportDir,
collectionIDPathMap collectionIDPathMap
); );
} }
if (!renamedCollections?.length) { if (renamedCollections?.length) {
await this.renamedFolderForRenamedCollections( await this.renameCollectionFolders(
renamedCollections, renamedCollections,
exportDir, exportDir,
collectionIDPathMap collectionIDPathMap
@ -349,16 +348,14 @@ class ExportService {
collectionFolderPath: string collectionFolderPath: string
) { ) {
const exportRecord = await this.getExportRecord(folder); const exportRecord = await this.getExportRecord(folder);
if (!exportRecord?.exportedCollections) { if (!exportRecord?.exportedCollectionPaths) {
exportRecord.exportedCollections = []; exportRecord.exportedCollectionPaths = {};
} }
exportRecord.exportedCollections.push({ exportRecord.exportedCollectionPaths = {
collectionID: collection.id, ...exportRecord.exportedCollectionPaths,
folderPath: collectionFolderPath, [collection.id]: collectionFolderPath,
}); };
exportRecord.exportedCollections = dedupe(
exportRecord.exportedCollections
);
await this.updateExportRecord(exportRecord, folder); await this.updateExportRecord(exportRecord, folder);
} }
@ -366,7 +363,7 @@ class ExportService {
const response = this.exportRecordUpdater.queueUpRequest(() => const response = this.exportRecordUpdater.queueUpRequest(() =>
this.updateExportRecordHelper(folder, newData) this.updateExportRecordHelper(folder, newData)
); );
response.promise; await response.promise;
} }
async updateExportRecordHelper(folder: string, newData: ExportRecord) { async updateExportRecordHelper(folder: string, newData: ExportRecord) {
try { try {
@ -426,7 +423,7 @@ class ExportService {
collectionIDPathMap.set(collection.id, collectionFolderPath); collectionIDPathMap.set(collection.id, collectionFolderPath);
} }
} }
async renamedFolderForRenamedCollections( async renameCollectionFolders(
renamedCollections: Collection[], renamedCollections: Collection[],
exportFolder: string, exportFolder: string,
collectionIDPathMap: CollectionIDPathMap collectionIDPathMap: CollectionIDPathMap

View file

@ -18,8 +18,9 @@ export const getExportQueuedFiles = (
const queuedFiles = new Set(exportRecord?.queuedFiles); const queuedFiles = new Set(exportRecord?.queuedFiles);
const unExportedFiles = allFiles.filter((file) => { const unExportedFiles = allFiles.filter((file) => {
if (queuedFiles.has(getExportRecordFileUID(file))) { if (queuedFiles.has(getExportRecordFileUID(file))) {
return file; return true;
} }
return false;
}); });
return unExportedFiles; return unExportedFiles;
}; };
@ -29,12 +30,13 @@ export const getCollectionsCreatedAfterLastExport = (
exportRecord: ExportRecord exportRecord: ExportRecord
) => { ) => {
const exportedCollections = new Set( const exportedCollections = new Set(
(exportRecord?.exportedCollections ?? []).map((c) => c.collectionID) Object.keys(exportRecord?.exportedCollectionPaths).map((x) => Number(x))
); );
const unExportedCollections = collections.filter((collection) => { const unExportedCollections = collections.filter((collection) => {
if (!exportedCollections.has(collection.id)) { if (!exportedCollections.has(collection.id)) {
return collection; return true;
} }
return false;
}); });
return unExportedCollections; return unExportedCollections;
}; };
@ -42,10 +44,11 @@ export const getCollectionIDPathMapFromExportRecord = (
exportRecord: ExportRecord exportRecord: ExportRecord
): CollectionIDPathMap => { ): CollectionIDPathMap => {
return new Map<number, string>( return new Map<number, string>(
(exportRecord.exportedCollections ?? []).map((c) => [ (Object.entries(exportRecord.exportedCollectionPaths) ?? []).map(
c.collectionID, (e) => {
c.folderPath, return [Number(e[0]), String(e[1])];
]) }
)
); );
}; };
@ -56,12 +59,20 @@ export const getCollectionsRenamedAfterLastExport = (
const collectionIDPathMap = const collectionIDPathMap =
getCollectionIDPathMapFromExportRecord(exportRecord); getCollectionIDPathMapFromExportRecord(exportRecord);
const renamedCollections = collections.filter((collection) => { const renamedCollections = collections.filter((collection) => {
if ( if (collectionIDPathMap.has(collection.id)) {
collectionIDPathMap.has(collection.id) && const currentFolderName = collectionIDPathMap.get(collection.id);
collectionIDPathMap.get(collection.id) !== collection.name const startIndex = currentFolderName.lastIndexOf('/');
) { const lastIndex = currentFolderName.lastIndexOf('(');
return collection; const nameRoot = currentFolderName.slice(
startIndex + 1,
lastIndex !== -1 ? lastIndex : currentFolderName.length
);
if (nameRoot !== sanitizeName(collection.name)) {
return true;
}
} }
return false;
}); });
return renamedCollections; return renamedCollections;
}; };
@ -73,8 +84,9 @@ export const getFilesUploadedAfterLastExport = (
const exportedFiles = new Set(exportRecord?.exportedFiles); const exportedFiles = new Set(exportRecord?.exportedFiles);
const unExportedFiles = allFiles.filter((file) => { const unExportedFiles = allFiles.filter((file) => {
if (!exportedFiles.has(getExportRecordFileUID(file))) { if (!exportedFiles.has(getExportRecordFileUID(file))) {
return file; return true;
} }
return false;
}); });
return unExportedFiles; return unExportedFiles;
}; };
@ -86,8 +98,9 @@ export const getExportedFiles = (
const exportedFileIds = new Set(exportRecord?.exportedFiles); const exportedFileIds = new Set(exportRecord?.exportedFiles);
const exportedFiles = allFiles.filter((file) => { const exportedFiles = allFiles.filter((file) => {
if (exportedFileIds.has(getExportRecordFileUID(file))) { if (exportedFileIds.has(getExportRecordFileUID(file))) {
return file; return true;
} }
return false;
}); });
return exportedFiles; return exportedFiles;
}; };
@ -99,8 +112,9 @@ export const getExportFailedFiles = (
const failedFiles = new Set(exportRecord?.failedFiles); const failedFiles = new Set(exportRecord?.failedFiles);
const filesToExport = allFiles.filter((file) => { const filesToExport = allFiles.filter((file) => {
if (failedFiles.has(getExportRecordFileUID(file))) { if (failedFiles.has(getExportRecordFileUID(file))) {
return file; return true;
} }
return false;
}); });
return filesToExport; return filesToExport;
}; };
@ -170,7 +184,7 @@ export const getUniqueFileSaveName = (
while ( while (
exportService.exists(getFileSavePath(collectionPath, fileSaveName)) exportService.exists(getFileSavePath(collectionPath, fileSaveName))
) { ) {
const filenameParts = splitFilenameAndExtension(filename); const filenameParts = splitFilenameAndExtension(sanitizeName(filename));
if (filenameParts[1]) { if (filenameParts[1]) {
fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`; fileSaveName = `${filenameParts[0]}(${count}).${filenameParts[1]}`;
} else { } else {