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';
|
2022-03-12 06:32:47 +00:00
|
|
|
import {
|
|
|
|
getIfToUploadFilesExists,
|
2022-03-20 10:23:53 +00:00
|
|
|
getElectronFile,
|
|
|
|
showUploadFilesDialog,
|
|
|
|
showUploadDirsDialog,
|
2022-03-22 06:01:40 +00:00
|
|
|
pendingToUploadFilePaths,
|
2022-03-12 06:32:47 +00:00
|
|
|
setToUploadFiles,
|
|
|
|
} from './utils/upload';
|
2021-03-29 11:28:29 +00:00
|
|
|
|
2021-03-31 06:16:33 +00:00
|
|
|
const { ipcRenderer } = electron;
|
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-12-07 06:24:18 +00:00
|
|
|
const exists = (path: string) => {
|
|
|
|
return fs.existsSync(path);
|
|
|
|
};
|
|
|
|
|
2021-03-31 05:33:46 +00:00
|
|
|
const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
|
|
|
|
if (!fs.existsSync(dirPath)) {
|
|
|
|
await fs.mkdir(dirPath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-16 07:16:24 +00:00
|
|
|
const checkExistsAndRename = async (oldDirPath: string, newDirPath: string) => {
|
|
|
|
if (fs.existsSync(oldDirPath)) {
|
|
|
|
await fs.rename(oldDirPath, newDirPath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-31 06:44:56 +00:00
|
|
|
const saveStreamToDisk = (path: string, fileStream: ReadableStream<any>) => {
|
2021-03-29 11:28:29 +00:00
|
|
|
const writeable = fs.createWriteStream(path);
|
|
|
|
const readable = responseToReadable(fileStream);
|
|
|
|
readable.pipe(writeable);
|
|
|
|
};
|
|
|
|
|
2021-03-31 06:44:56 +00:00
|
|
|
const saveFileToDisk = async (path: string, file: any) => {
|
|
|
|
await fs.writeFile(path, file);
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-30 12:58:30 +00:00
|
|
|
const sendNotification = (content: string) => {
|
|
|
|
ipcRenderer.send('send-notification', content);
|
|
|
|
};
|
2021-03-31 08:33:17 +00:00
|
|
|
const showOnTray = (content: string) => {
|
|
|
|
ipcRenderer.send('update-tray', content);
|
|
|
|
};
|
|
|
|
|
2021-07-14 05:55:20 +00:00
|
|
|
const registerResumeExportListener = (resumeExport: () => void) => {
|
2021-11-16 07:16:24 +00:00
|
|
|
ipcRenderer.removeAllListeners('resume-export');
|
2021-07-14 05:55:20 +00:00
|
|
|
ipcRenderer.on('resume-export', () => resumeExport());
|
2021-07-09 05:16:33 +00:00
|
|
|
};
|
|
|
|
const registerStopExportListener = (abortExport: () => void) => {
|
2021-11-16 07:16:24 +00:00
|
|
|
ipcRenderer.removeAllListeners('stop-export');
|
2021-03-31 08:33:17 +00:00
|
|
|
ipcRenderer.on('stop-export', () => abortExport());
|
2021-03-30 12:58:30 +00:00
|
|
|
};
|
|
|
|
|
2021-07-09 05:16:33 +00:00
|
|
|
const registerPauseExportListener = (pauseExport: () => void) => {
|
2021-11-16 07:16:24 +00:00
|
|
|
ipcRenderer.removeAllListeners('pause-export');
|
2021-07-09 05:16:33 +00:00
|
|
|
ipcRenderer.on('pause-export', () => pauseExport());
|
|
|
|
};
|
|
|
|
|
|
|
|
const registerRetryFailedExportListener = (retryFailedExport: () => void) => {
|
2021-11-16 07:16:24 +00:00
|
|
|
ipcRenderer.removeAllListeners('retry-export');
|
2021-07-09 05:16:33 +00:00
|
|
|
ipcRenderer.on('retry-export', () => retryFailedExport());
|
|
|
|
};
|
|
|
|
|
2021-04-09 22:39:47 +00:00
|
|
|
const reloadWindow = () => {
|
|
|
|
ipcRenderer.send('reload-window');
|
|
|
|
};
|
|
|
|
|
2021-07-18 11:19:24 +00:00
|
|
|
const getExportRecord = async (filePath: string) => {
|
2021-07-13 13:50:38 +00:00
|
|
|
try {
|
2021-07-18 11:19:24 +00:00
|
|
|
const filepath = `${filePath}`;
|
2021-07-13 13:50:38 +00:00
|
|
|
const recordFile = await fs.readFile(filepath, 'utf-8');
|
|
|
|
return recordFile;
|
|
|
|
} catch (e) {
|
|
|
|
// ignore exportFile missing
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-18 11:19:24 +00:00
|
|
|
const setExportRecord = async (filePath: string, data: string) => {
|
|
|
|
const filepath = `${filePath}`;
|
2021-07-13 13:50:38 +00:00
|
|
|
await fs.writeFile(filepath, data);
|
|
|
|
};
|
|
|
|
|
2021-07-09 05:16:33 +00:00
|
|
|
const windowObject: any = window;
|
2021-03-31 06:16:33 +00:00
|
|
|
windowObject['ElectronAPIs'] = {
|
2021-12-07 06:24:18 +00:00
|
|
|
exists,
|
2021-03-31 05:33:46 +00:00
|
|
|
checkExistsAndCreateCollectionDir,
|
2021-11-16 07:16:24 +00:00
|
|
|
checkExistsAndRename,
|
2021-03-31 06:44:56 +00:00
|
|
|
saveStreamToDisk,
|
|
|
|
saveFileToDisk,
|
2021-03-31 05:33:46 +00:00
|
|
|
selectRootDirectory,
|
2021-03-30 12:58:30 +00:00
|
|
|
sendNotification,
|
|
|
|
showOnTray,
|
2021-04-09 22:39:47 +00:00
|
|
|
reloadWindow,
|
2021-07-14 05:55:20 +00:00
|
|
|
registerResumeExportListener,
|
2021-07-09 05:16:33 +00:00
|
|
|
registerStopExportListener,
|
|
|
|
registerPauseExportListener,
|
|
|
|
registerRetryFailedExportListener,
|
2021-07-13 13:50:38 +00:00
|
|
|
getExportRecord,
|
2021-11-16 07:16:24 +00:00
|
|
|
setExportRecord,
|
2022-03-12 06:32:47 +00:00
|
|
|
getIfToUploadFilesExists,
|
2022-03-20 10:23:53 +00:00
|
|
|
getElectronFile,
|
|
|
|
showUploadFilesDialog,
|
|
|
|
showUploadDirsDialog,
|
2022-03-22 06:01:40 +00:00
|
|
|
pendingToUploadFilePaths,
|
2022-03-20 10:23:53 +00:00
|
|
|
setToUploadFiles,
|
2021-03-31 06:16:33 +00:00
|
|
|
};
|