ente/src/services/exportService.ts

94 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-03-29 05:15:08 +00:00
import { runningInBrowser } from 'utils/common/utilFunctions';
import { collection } from './collectionService';
2021-03-29 05:15:08 +00:00
import downloadManager from './downloadManager';
import { file } from './fileService';
enum ExportNotification {
START = 'export started',
IN_PROGRESS = 'export already in progress',
FINISH = 'export finished',
2021-03-31 08:34:00 +00:00
ABORT = 'export aborted',
}
2021-03-29 05:15:08 +00:00
class ExportService {
ElectronAPIs: any = runningInBrowser() && window['ElectronAPIs'];
exportInProgress: Promise<void> = null;
2021-03-31 08:34:00 +00:00
abortExport: boolean = false;
async exportFiles(files: file[], collections: collection[]) {
if (this.exportInProgress) {
this.ElectronAPIs.sendNotification(ExportNotification.IN_PROGRESS);
return this.exportInProgress;
}
this.exportInProgress = this.fileExporter(files, collections);
return this.exportInProgress;
}
async fileExporter(files: file[], collections: collection[]) {
try {
const dir = await this.ElectronAPIs.selectRootDirectory();
if (!dir) {
// directory selector closed
return;
}
const exportedFiles: Set<string> = await this.ElectronAPIs.getExportedFiles(
dir
);
2021-03-31 08:34:00 +00:00
this.ElectronAPIs.showOnTray(`starting export`);
this.ElectronAPIs.registerStopExportListener(
() => (this.abortExport = true)
);
const collectionIDMap = new Map<number, string>();
for (let collection of collections) {
let collectionFolderPath = `${dir}/${
collection.id
2021-04-04 08:23:00 +00:00
}_${this.sanitizeName(collection.name)}`;
await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
collectionFolderPath
);
collectionIDMap.set(collection.id, collectionFolderPath);
}
this.ElectronAPIs.sendNotification(ExportNotification.START);
for (let [index, file] of files.entries()) {
2021-03-31 08:34:00 +00:00
if (this.abortExport) {
break;
}
2021-04-04 08:34:42 +00:00
const uid = `${file.id}_${this.sanitizeName(
file.metadata.title
)}`;
const filePath =
collectionIDMap.get(file.collectionID) + '/' + uid;
if (!exportedFiles.has(filePath)) {
await this.downloadAndSave(file, filePath);
this.ElectronAPIs.updateExportRecord(dir, filePath);
}
2021-03-31 08:34:00 +00:00
this.ElectronAPIs.showOnTray(
`exporting file ${index + 1} / ${files.length}`
);
}
2021-03-31 08:34:00 +00:00
this.ElectronAPIs.sendNotification(
this.abortExport
? ExportNotification.ABORT
: ExportNotification.FINISH
);
} catch (e) {
console.error(e);
2021-03-31 08:34:00 +00:00
} finally {
this.exportInProgress = null;
this.ElectronAPIs.showOnTray();
this.abortExport = false;
2021-03-29 05:15:08 +00:00
}
}
2021-03-29 11:29:19 +00:00
async downloadAndSave(file: file, path) {
const fileStream = await downloadManager.downloadFile(file);
2021-03-31 06:45:41 +00:00
this.ElectronAPIs.saveStreamToDisk(path, fileStream);
this.ElectronAPIs.saveFileToDisk(
`${path}.json`,
JSON.stringify(file.metadata, null, 2)
);
2021-03-29 11:29:19 +00:00
}
2021-04-04 08:23:00 +00:00
private sanitizeName(name) {
return name.replaceAll('/', '_').replaceAll(' ', '_');
}
2021-03-29 05:15:08 +00:00
}
export default new ExportService();