ente/src/services/exportService.ts

30 lines
1,014 B
TypeScript
Raw Normal View History

2021-03-29 05:15:08 +00:00
import { runningInBrowser } from 'utils/common/utilFunctions';
import downloadManager from './downloadManager';
import { file } from './fileService';
class ExportService {
ElectronAPIs: any = runningInBrowser() && window['ElectronAPIs'];
2021-03-29 11:29:19 +00:00
async exportFiles(files: file[]) {
2021-03-29 05:15:08 +00:00
const dir = await this.ElectronAPIs.selectDirectory();
2021-03-30 11:35:47 +00:00
const exportedFiles: Set<string> = await this.ElectronAPIs.getExportedFiles(
dir
);
2021-03-29 05:15:08 +00:00
for (let file of files) {
2021-03-30 11:35:47 +00:00
const uid = `${file.id}_${file.metadata.title}`;
if (!exportedFiles.has(uid)) {
await this.downloadAndSave(file, `${dir}/${uid}`);
this.ElectronAPIs.updateExportRecord(dir, uid);
}
2021-03-29 05:15:08 +00:00
}
}
2021-03-29 11:29:19 +00:00
async downloadAndSave(file: file, path) {
console.log(this.ElectronAPIs);
const fileStream = await downloadManager.downloadFile(file);
this.ElectronAPIs.saveToDisk(path, fileStream);
}
2021-03-29 05:15:08 +00:00
}
export default new ExportService();