fix collection folder creation

This commit is contained in:
Abhinav 2021-12-07 15:41:38 +05:30
parent cf09858fb7
commit 55b2de1a53
2 changed files with 79 additions and 17 deletions

View file

@ -15,6 +15,7 @@ import {
getOldFileMetadataSavePath,
getExportedFiles,
getMetadataFolderPath,
getCollectionsCreatedAfterLastExport,
} from 'utils/export';
import { retryAsyncFunction } from 'utils/network';
import { logError } from 'utils/sentry';
@ -40,10 +41,15 @@ import { updateFileCreationDateInEXIF } from './upload/exifService';
import { MetadataObject } from './upload/uploadService';
import QueueProcessor from './upload/queueProcessor';
type CollectionIDPathMap = Map<number, string>;
export interface ExportProgress {
current: number;
total: number;
}
export interface ExportedCollection {
collectionID: number;
folderPath: string;
}
export interface ExportStats {
failed: number;
success: number;
@ -59,6 +65,7 @@ export interface ExportRecord {
queuedFiles?: string[];
exportedFiles?: string[];
failedFiles?: string[];
exportedCollections?: ExportedCollection[];
}
export enum ExportStage {
INIT,
@ -127,6 +134,8 @@ class ExportService {
// no-export folder set
return;
}
const user: User = getData(LS_KEYS.USER);
let filesToExport: File[];
const localFiles = await getLocalFiles();
const userPersonalFiles = localFiles
@ -138,15 +147,14 @@ class ExportService {
collections,
userPersonalFiles
);
const user: User = getData(LS_KEYS.USER);
const userCollections = nonEmptyCollections
.filter((collection) => collection.owner.id === user?.id)
.sort(
(collectionA, collectionB) =>
collectionA.id - collectionB.id
);
const exportRecord = await this.getExportRecord(exportDir);
await this.migrateExport(exportDir, collections, userPersonalFiles);
const exportRecord = await this.getExportRecord(exportDir);
if (exportType === ExportType.NEW) {
filesToExport = getFilesUploadedAfterLastExport(
@ -164,9 +172,23 @@ class ExportService {
exportRecord
);
}
const collectionIDPathMap: CollectionIDPathMap = new Map<
number,
string
>(
(exportRecord.exportedCollections ?? []).map((c) => [
c.collectionID,
c.folderPath,
])
);
const newCollections = getCollectionsCreatedAfterLastExport(
userCollections,
exportRecord
);
this.exportInProgress = this.fileExporter(
filesToExport,
userCollections,
newCollections,
collectionIDPathMap,
updateProgress,
exportDir
);
@ -182,10 +204,24 @@ class ExportService {
async fileExporter(
files: File[],
collections: Collection[],
collectionIDPathMap: CollectionIDPathMap,
updateProgress: (progress: ExportProgress) => void,
dir: string
): Promise<{ paused: boolean }> {
try {
for (const collection of collections) {
const collectionFolderPath = getUniqueCollectionFolderPath(
dir,
collection.name
);
collectionIDPathMap.set(collection.id, collectionFolderPath);
await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
collectionFolderPath
);
await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
getMetadataFolderPath(collectionFolderPath)
);
}
if (!files?.length) {
this.ElectronAPIs.sendNotification(
ExportNotification.UP_TO_DATE
@ -205,20 +241,7 @@ class ExportService {
total: files.length,
});
this.ElectronAPIs.sendNotification(ExportNotification.START);
const collectionIDPathMap = new Map<number, string>();
for (const collection of collections) {
const collectionFolderPath = getUniqueCollectionFolderPath(
dir,
collection.name
);
collectionIDPathMap.set(collection.id, collectionFolderPath);
await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
collectionFolderPath
);
await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
getMetadataFolderPath(collectionFolderPath)
);
}
for (const [index, file] of files.entries()) {
if (this.stopExport || this.pauseExport) {
if (this.pauseExport) {
@ -317,6 +340,25 @@ class ExportService {
await this.updateExportRecord(exportRecord, folder);
}
async addCollectionExportedRecord(
folder: string,
collection: Collection,
collectionFolderPath: string
) {
const exportRecord = await this.getExportRecord(folder);
if (!exportRecord?.exportedCollections) {
exportRecord.exportedCollections = [];
}
exportRecord.exportedCollections.push({
collectionID: collection.id,
folderPath: collectionFolderPath,
});
exportRecord.exportedCollections = dedupe(
exportRecord.exportedCollections
);
await this.updateExportRecord(exportRecord, folder);
}
async updateExportRecord(newData: ExportRecord, folder?: string) {
const response = this.exportRecordUpdater.queueUpRequest(() =>
this.updateExportRecordHelper(folder, newData)
@ -496,6 +538,11 @@ class ExportService {
oldCollectionFolderPath,
newCollectionFolderPath
);
await this.addCollectionExportedRecord(
dir,
collection,
newCollectionFolderPath
);
}
}

View file

@ -23,6 +23,21 @@ export const getExportQueuedFiles = (
return unExportedFiles;
};
export const getCollectionsCreatedAfterLastExport = (
collections: Collection[],
exportRecord: ExportRecord
) => {
const exportedCollections = new Set(
(exportRecord?.exportedCollections ?? []).map((c) => c.collectionID)
);
const unExportedCollections = collections.filter((collection) => {
if (!exportedCollections.has(collection.id)) {
return collection;
}
});
return unExportedCollections;
};
export const getFilesUploadedAfterLastExport = (
allFiles: File[],
exportRecord: ExportRecord