2021-03-29 11:28:29 +00:00
|
|
|
import { Readable } from 'stream';
|
2021-03-30 12:57:40 +00:00
|
|
|
import * as fs from 'promise-fs';
|
2021-03-29 11:28:29 +00:00
|
|
|
import * as electron from 'electron';
|
|
|
|
|
2021-03-31 05:32:57 +00:00
|
|
|
const { ipcRenderer, contextBridge } = electron;
|
2021-03-29 11:28:29 +00:00
|
|
|
|
2021-03-30 12:57:40 +00:00
|
|
|
const EXPORT_FILE_NAME = 'export.txt';
|
|
|
|
|
2021-03-29 11:28:29 +00:00
|
|
|
const responseToReadable = (fileStream: any) => {
|
|
|
|
const reader = fileStream.getReader();
|
|
|
|
const rs = new Readable();
|
|
|
|
rs._read = async () => {
|
|
|
|
const result = await reader.read();
|
|
|
|
if (!result.done) {
|
|
|
|
rs.push(Buffer.from(result.value));
|
|
|
|
} else {
|
|
|
|
rs.push(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return rs;
|
|
|
|
};
|
|
|
|
|
2021-03-31 05:33:46 +00:00
|
|
|
const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
|
|
|
|
if (!fs.existsSync(dirPath)) {
|
|
|
|
await fs.mkdir(dirPath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-29 11:28:29 +00:00
|
|
|
const saveToDisk = (path: string, fileStream: ReadableStream<any>) => {
|
|
|
|
const writeable = fs.createWriteStream(path);
|
|
|
|
const readable = responseToReadable(fileStream);
|
|
|
|
readable.pipe(writeable);
|
|
|
|
};
|
|
|
|
|
2021-03-31 05:33:46 +00:00
|
|
|
const selectRootDirectory = async () => {
|
2021-03-29 11:28:29 +00:00
|
|
|
try {
|
|
|
|
return await ipcRenderer.sendSync('select-dir');
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2021-03-30 12:57:40 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateExportRecord = async (dir: string, dataToAppend: string) => {
|
|
|
|
const filepath = `${dir}/${EXPORT_FILE_NAME}`;
|
|
|
|
let file = null;
|
|
|
|
try {
|
|
|
|
file = await fs.readFile(filepath, 'utf-8');
|
|
|
|
} catch (e) {
|
|
|
|
file = '';
|
|
|
|
}
|
|
|
|
file = file + `${dataToAppend}\n`;
|
|
|
|
await fs.writeFile(filepath, file);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getExportedFiles = async (dir: string) => {
|
|
|
|
try {
|
|
|
|
const filepath = `${dir}/${EXPORT_FILE_NAME}`;
|
|
|
|
|
|
|
|
let fileList = (await fs.readFile(filepath, 'utf-8')).split('\n');
|
|
|
|
|
|
|
|
return new Set<string>(fileList);
|
|
|
|
} catch (e) {
|
|
|
|
return new Set<string>();
|
2021-03-27 12:35:04 +00:00
|
|
|
}
|
2021-03-29 11:28:29 +00:00
|
|
|
};
|
2021-03-27 12:35:04 +00:00
|
|
|
|
2021-03-30 12:58:30 +00:00
|
|
|
const sendNotification = (content: string) => {
|
|
|
|
ipcRenderer.send('send-notification', content);
|
|
|
|
};
|
|
|
|
const showOnTray = (item: any[]) => {
|
|
|
|
ipcRenderer.send('update-tray', item);
|
|
|
|
};
|
|
|
|
|
2021-03-31 05:32:57 +00:00
|
|
|
contextBridge.exposeInMainWorld('ElectronAPIs', {
|
2021-03-31 05:33:46 +00:00
|
|
|
checkExistsAndCreateCollectionDir,
|
2021-03-29 11:28:29 +00:00
|
|
|
saveToDisk,
|
2021-03-31 05:33:46 +00:00
|
|
|
selectRootDirectory,
|
2021-03-30 12:57:40 +00:00
|
|
|
updateExportRecord,
|
|
|
|
getExportedFiles,
|
2021-03-30 12:58:30 +00:00
|
|
|
sendNotification,
|
|
|
|
showOnTray,
|
2021-03-31 05:32:57 +00:00
|
|
|
});
|