2021-03-29 05:15:08 +00:00
|
|
|
import { runningInBrowser } from 'utils/common/utilFunctions';
|
2021-03-31 05:53:21 +00:00
|
|
|
import { collection } from './collectionService';
|
2021-03-29 05:15:08 +00:00
|
|
|
import downloadManager from './downloadManager';
|
|
|
|
import { file } from './fileService';
|
|
|
|
|
2021-03-30 11:37:32 +00:00
|
|
|
enum ExportNotification {
|
|
|
|
START = 'export started',
|
|
|
|
FINISH = 'export finished',
|
|
|
|
}
|
2021-03-29 05:15:08 +00:00
|
|
|
class ExportService {
|
|
|
|
ElectronAPIs: any = runningInBrowser() && window['ElectronAPIs'];
|
2021-03-31 05:53:21 +00:00
|
|
|
async exportFiles(files: file[], collections: collection[]) {
|
2021-03-30 13:00:09 +00:00
|
|
|
try {
|
2021-03-31 05:53:21 +00:00
|
|
|
const dir = await this.ElectronAPIs.selectRootDirectory();
|
2021-03-30 13:00:09 +00:00
|
|
|
if (!dir) {
|
|
|
|
// directory selector closed
|
|
|
|
return;
|
2021-03-29 12:45:54 +00:00
|
|
|
}
|
2021-03-30 13:00:09 +00:00
|
|
|
const exportedFiles: Set<string> = await this.ElectronAPIs.getExportedFiles(
|
|
|
|
dir
|
|
|
|
);
|
2021-03-31 05:53:21 +00:00
|
|
|
const collectionIDMap = new Map<number, string>();
|
|
|
|
for (let collection of collections) {
|
2021-03-31 06:30:42 +00:00
|
|
|
let collectionFolderPath = `${dir}/${
|
|
|
|
collection.id
|
|
|
|
}_${this.sanitizeNames(collection.name)}`;
|
2021-03-31 05:53:21 +00:00
|
|
|
await this.ElectronAPIs.checkExistsAndCreateCollectionDir(
|
|
|
|
collectionFolderPath
|
|
|
|
);
|
|
|
|
collectionIDMap.set(collection.id, collectionFolderPath);
|
|
|
|
}
|
2021-03-30 13:00:09 +00:00
|
|
|
this.ElectronAPIs.sendNotification(ExportNotification.START);
|
|
|
|
for (let [index, file] of files.entries()) {
|
2021-03-31 05:53:21 +00:00
|
|
|
const uid = `${file.id}_${this.sanitizeNames(
|
|
|
|
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-30 13:00:09 +00:00
|
|
|
}
|
|
|
|
this.ElectronAPIs.showOnTray([
|
|
|
|
{ label: `${index + 1} / ${files.length} files exported` },
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
this.ElectronAPIs.sendNotification(ExportNotification.FINISH);
|
|
|
|
this.ElectronAPIs.showOnTray([]);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
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-03-31 05:53:21 +00:00
|
|
|
private sanitizeNames(name) {
|
|
|
|
return name.replaceAll('/', '_').replaceAll(' ', '_');
|
|
|
|
}
|
2021-03-29 05:15:08 +00:00
|
|
|
}
|
|
|
|
export default new ExportService();
|