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

View file

@ -23,6 +23,21 @@ export const getExportQueuedFiles = (
return unExportedFiles; 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 = ( export const getFilesUploadedAfterLastExport = (
allFiles: File[], allFiles: File[],
exportRecord: ExportRecord exportRecord: ExportRecord