refactor
This commit is contained in:
parent
fe0b9617f0
commit
0da45931c8
|
@ -1,11 +1,10 @@
|
|||
import { ipcRenderer } from 'electron';
|
||||
import { ipcRenderer } from 'electron/renderer';
|
||||
import { logError } from '../utils/logging';
|
||||
|
||||
export const sendNotification = (content: string) => {
|
||||
ipcRenderer.send('send-notification', content);
|
||||
};
|
||||
export const showOnTray = (content: string) => {
|
||||
ipcRenderer.send('update-tray', content);
|
||||
};
|
||||
export const reloadWindow = () => {
|
||||
ipcRenderer.send('reload-window');
|
||||
export const selectRootDirectory = async () => {
|
||||
try {
|
||||
return await ipcRenderer.invoke('select-dir');
|
||||
} catch (e) {
|
||||
logError(e, 'error while selecting root directory');
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,30 +1,22 @@
|
|||
import {} from './common';
|
||||
import {
|
||||
createDirectory,
|
||||
doesFileExists,
|
||||
readTextFile,
|
||||
renameDirectory,
|
||||
writeFile,
|
||||
writeStream,
|
||||
} from './../services/fs';
|
||||
import { ipcRenderer } from 'electron';
|
||||
import * as fs from 'promise-fs';
|
||||
import { Readable } from 'stream';
|
||||
import { logError } from '../utils/logging';
|
||||
|
||||
export 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;
|
||||
};
|
||||
|
||||
export const exists = (path: string) => {
|
||||
return fs.existsSync(path);
|
||||
return doesFileExists(path);
|
||||
};
|
||||
|
||||
export const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
await fs.mkdir(dirPath);
|
||||
if (!doesFileExists(dirPath)) {
|
||||
await createDirectory(dirPath);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -32,22 +24,37 @@ export const checkExistsAndRename = async (
|
|||
oldDirPath: string,
|
||||
newDirPath: string
|
||||
) => {
|
||||
if (fs.existsSync(oldDirPath)) {
|
||||
await fs.rename(oldDirPath, newDirPath);
|
||||
if (doesFileExists(oldDirPath)) {
|
||||
await renameDirectory(oldDirPath, newDirPath);
|
||||
}
|
||||
};
|
||||
|
||||
export const saveStreamToDisk = (
|
||||
path: string,
|
||||
filePath: string,
|
||||
fileStream: ReadableStream<any>
|
||||
) => {
|
||||
const writeable = fs.createWriteStream(path);
|
||||
const readable = responseToReadable(fileStream);
|
||||
readable.pipe(writeable);
|
||||
writeStream(filePath, fileStream);
|
||||
};
|
||||
|
||||
export const saveFileToDisk = async (path: string, file: any) => {
|
||||
await fs.writeFile(path, file);
|
||||
export const saveFileToDisk = async (path: string, fileData: any) => {
|
||||
await writeFile(path, fileData);
|
||||
};
|
||||
|
||||
export const getExportRecord = async (filePath: string) => {
|
||||
try {
|
||||
if (!(await doesFileExists(filePath))) {
|
||||
return null;
|
||||
}
|
||||
const recordFile = await readTextFile(filePath);
|
||||
return recordFile;
|
||||
} catch (e) {
|
||||
// ignore exportFile missing
|
||||
logError(e, 'error while selecting files');
|
||||
}
|
||||
};
|
||||
|
||||
export const setExportRecord = async (filePath: string, data: string) => {
|
||||
await writeFile(filePath, data);
|
||||
};
|
||||
|
||||
export const registerResumeExportListener = (resumeExport: () => void) => {
|
||||
|
@ -71,30 +78,3 @@ export const registerRetryFailedExportListener = (
|
|||
ipcRenderer.removeAllListeners('retry-export');
|
||||
ipcRenderer.on('retry-export', () => retryFailedExport());
|
||||
};
|
||||
|
||||
export const getExportRecord = async (filePath: string) => {
|
||||
try {
|
||||
const filepath = `${filePath}`;
|
||||
if (!(await fs.stat(filePath)).isFile()) {
|
||||
return null;
|
||||
}
|
||||
const recordFile = await fs.readFile(filepath, 'utf-8');
|
||||
return recordFile;
|
||||
} catch (e) {
|
||||
// ignore exportFile missing
|
||||
logError(e, 'error while selecting files');
|
||||
}
|
||||
};
|
||||
|
||||
export const setExportRecord = async (filePath: string, data: string) => {
|
||||
const filepath = `${filePath}`;
|
||||
await fs.writeFile(filepath, data);
|
||||
};
|
||||
|
||||
export const selectRootDirectory = async () => {
|
||||
try {
|
||||
return await ipcRenderer.invoke('select-dir');
|
||||
} catch (e) {
|
||||
logError(e, 'error while selecting root directory');
|
||||
}
|
||||
};
|
||||
|
|
11
src/api/system.ts
Normal file
11
src/api/system.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { ipcRenderer } from 'electron';
|
||||
|
||||
export const sendNotification = (content: string) => {
|
||||
ipcRenderer.send('send-notification', content);
|
||||
};
|
||||
export const showOnTray = (content: string) => {
|
||||
ipcRenderer.send('update-tray', content);
|
||||
};
|
||||
export const reloadWindow = () => {
|
||||
ipcRenderer.send('reload-window');
|
||||
};
|
|
@ -47,16 +47,6 @@ export const setToUploadCollection = (collectionName: string) => {
|
|||
}
|
||||
};
|
||||
|
||||
export const getSavedPaths = (type: FILE_PATH_TYPE) => {
|
||||
const paths =
|
||||
getValidPaths(
|
||||
uploadStatusStore.get(FILE_PATH_KEYS[type]) as string[]
|
||||
) ?? [];
|
||||
|
||||
setToUploadFiles(type, paths);
|
||||
return paths;
|
||||
};
|
||||
|
||||
export const getPendingUploads = async () => {
|
||||
const filePaths = getSavedPaths(FILE_PATH_TYPE.FILES);
|
||||
const zipPaths = getSavedPaths(FILE_PATH_TYPE.ZIPS);
|
||||
|
@ -143,3 +133,13 @@ export const showUploadZipDialog = async () => {
|
|||
logError(e, 'error while selecting zips');
|
||||
}
|
||||
};
|
||||
|
||||
const getSavedPaths = (type: FILE_PATH_TYPE) => {
|
||||
const paths =
|
||||
getValidPaths(
|
||||
uploadStatusStore.get(FILE_PATH_KEYS[type]) as string[]
|
||||
) ?? [];
|
||||
|
||||
setToUploadFiles(type, paths);
|
||||
return paths;
|
||||
};
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import { reloadWindow, sendNotification, showOnTray } from './api/common';
|
||||
import { reloadWindow, sendNotification, showOnTray } from './api/system';
|
||||
import {
|
||||
showUploadDirsDialog,
|
||||
showUploadFilesDialog,
|
||||
showUploadZipDialog,
|
||||
getElectronFile,
|
||||
getPendingUploads,
|
||||
setToUploadFiles,
|
||||
getElectronFilesFromGoogleZip,
|
||||
|
@ -24,11 +23,12 @@ import {
|
|||
getExportRecord,
|
||||
setExportRecord,
|
||||
exists,
|
||||
selectRootDirectory,
|
||||
} from './api/export';
|
||||
import { fixHotReloadNext12 } from './utils/preload';
|
||||
|
||||
fixHotReloadNext12();
|
||||
import { selectRootDirectory } from './api/common';
|
||||
import { getElectronFile } from './services/fs';
|
||||
|
||||
const windowObject: any = window;
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import * as fs from 'promise-fs';
|
|||
import { ElectronFile } from '../types';
|
||||
import { logError } from '../utils/logging';
|
||||
import StreamZip from 'node-stream-zip';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
// https://stackoverflow.com/a/63111390
|
||||
export const getFilesFromDir = async (dirPath: string) => {
|
||||
|
@ -150,3 +151,52 @@ export async function doesFolderExists(dirPath: string) {
|
|||
})
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
export async function doesFileExists(dirPath: string) {
|
||||
return await fs
|
||||
.stat(dirPath)
|
||||
.then((stats) => {
|
||||
return stats.isFile();
|
||||
})
|
||||
.catch(() => false);
|
||||
}
|
||||
|
||||
export const convertBrowserStreamToNode = (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;
|
||||
};
|
||||
|
||||
export async function createDirectory(dirPath: string) {
|
||||
await fs.mkdir(dirPath);
|
||||
}
|
||||
|
||||
export async function renameDirectory(oldDirPath: string, newDirPath: string) {
|
||||
await fs.rename(oldDirPath, newDirPath);
|
||||
}
|
||||
|
||||
export async function writeFile(filePath: string, fileData: any) {
|
||||
await fs.writeFile(filePath, fileData);
|
||||
}
|
||||
|
||||
export function writeStream(filePath: string, fileStream: any) {
|
||||
const writeable = fs.createWriteStream(filePath);
|
||||
const readable = convertBrowserStreamToNode(fileStream);
|
||||
readable.pipe(writeable);
|
||||
}
|
||||
|
||||
export async function readTextFile(filePath: string) {
|
||||
await fs.readFile(filePath, 'utf-8');
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import {
|
|||
import { createWindow } from './createWindow';
|
||||
import { buildContextMenu } from './menu';
|
||||
import { logErrorSentry } from './sentry';
|
||||
import { getFilesFromDir } from '../api/upload';
|
||||
import { getFilesFromDir } from '../services/fs';
|
||||
|
||||
export default function setupIpcComs(
|
||||
tray: Tray,
|
||||
|
|
Loading…
Reference in a new issue