ente/src/preload.ts

85 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Readable } from 'stream';
import * as fs from 'promise-fs';
import * as electron from 'electron';
2021-03-31 05:32:57 +00:00
const { ipcRenderer, contextBridge } = electron;
const EXPORT_FILE_NAME = 'export.txt';
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;
};
const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
if (!fs.existsSync(dirPath)) {
await fs.mkdir(dirPath);
}
};
const saveToDisk = (path: string, fileStream: ReadableStream<any>) => {
const writeable = fs.createWriteStream(path);
const readable = responseToReadable(fileStream);
readable.pipe(writeable);
};
const selectRootDirectory = async () => {
try {
return await ipcRenderer.sendSync('select-dir');
} catch (e) {
console.error(e);
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-27 12:35:04 +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', {
checkExistsAndCreateCollectionDir,
saveToDisk,
selectRootDirectory,
updateExportRecord,
getExportedFiles,
sendNotification,
showOnTray,
2021-03-31 05:32:57 +00:00
});