move files to service dir
This commit is contained in:
parent
066c1d30e8
commit
7b174fe402
|
@ -1,5 +1,3 @@
|
|||
import { Readable } from 'stream';
|
||||
import * as fs from 'promise-fs';
|
||||
import { webFrame, ipcRenderer } from 'electron';
|
||||
import {
|
||||
getElectronFile,
|
||||
|
@ -7,12 +5,25 @@ import {
|
|||
setToUploadFiles,
|
||||
getElectronFilesFromGoogleZip,
|
||||
setToUploadCollection,
|
||||
} from './utils/upload';
|
||||
} from './services/upload';
|
||||
import { logError } from './utils/logging';
|
||||
import { ElectronFile } from './types';
|
||||
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
|
||||
import { clearElectronStore } from './utils/electronStore';
|
||||
import { openDiskCache, deleteDiskCache } from './utils/cache';
|
||||
import {
|
||||
checkExistsAndCreateCollectionDir,
|
||||
checkExistsAndRename,
|
||||
saveStreamToDisk,
|
||||
saveFileToDisk,
|
||||
registerResumeExportListener,
|
||||
registerStopExportListener,
|
||||
registerPauseExportListener,
|
||||
registerRetryFailedExportListener,
|
||||
getExportRecord,
|
||||
setExportRecord,
|
||||
exists,
|
||||
} from './services/export';
|
||||
|
||||
// Patch the global WebSocket constructor to use the correct DevServer url
|
||||
const fixHotReloadNext12 = () => {
|
||||
|
@ -32,47 +43,6 @@ const fixHotReloadNext12 = () => {
|
|||
|
||||
fixHotReloadNext12();
|
||||
|
||||
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 exists = (path: string) => {
|
||||
return fs.existsSync(path);
|
||||
};
|
||||
|
||||
const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
await fs.mkdir(dirPath);
|
||||
}
|
||||
};
|
||||
|
||||
const checkExistsAndRename = async (oldDirPath: string, newDirPath: string) => {
|
||||
if (fs.existsSync(oldDirPath)) {
|
||||
await fs.rename(oldDirPath, newDirPath);
|
||||
}
|
||||
};
|
||||
|
||||
const saveStreamToDisk = (path: string, fileStream: ReadableStream<any>) => {
|
||||
const writeable = fs.createWriteStream(path);
|
||||
const readable = responseToReadable(fileStream);
|
||||
readable.pipe(writeable);
|
||||
};
|
||||
|
||||
const saveFileToDisk = async (path: string, file: any) => {
|
||||
await fs.writeFile(path, file);
|
||||
};
|
||||
|
||||
const selectRootDirectory = async () => {
|
||||
try {
|
||||
return await ipcRenderer.invoke('select-dir');
|
||||
|
@ -88,45 +58,10 @@ const showOnTray = (content: string) => {
|
|||
ipcRenderer.send('update-tray', content);
|
||||
};
|
||||
|
||||
const registerResumeExportListener = (resumeExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('resume-export');
|
||||
ipcRenderer.on('resume-export', () => resumeExport());
|
||||
};
|
||||
const registerStopExportListener = (abortExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('stop-export');
|
||||
ipcRenderer.on('stop-export', () => abortExport());
|
||||
};
|
||||
|
||||
const registerPauseExportListener = (pauseExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('pause-export');
|
||||
ipcRenderer.on('pause-export', () => pauseExport());
|
||||
};
|
||||
|
||||
const registerRetryFailedExportListener = (retryFailedExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('retry-export');
|
||||
ipcRenderer.on('retry-export', () => retryFailedExport());
|
||||
};
|
||||
|
||||
const reloadWindow = () => {
|
||||
ipcRenderer.send('reload-window');
|
||||
};
|
||||
|
||||
const getExportRecord = async (filePath: string) => {
|
||||
try {
|
||||
const filepath = `${filePath}`;
|
||||
const recordFile = await fs.readFile(filepath, 'utf-8');
|
||||
return recordFile;
|
||||
} catch (e) {
|
||||
// ignore exportFile missing
|
||||
logError(e, 'error while selecting files');
|
||||
}
|
||||
};
|
||||
|
||||
const setExportRecord = async (filePath: string, data: string) => {
|
||||
const filepath = `${filePath}`;
|
||||
await fs.writeFile(filepath, data);
|
||||
};
|
||||
|
||||
const showUploadFilesDialog = async () => {
|
||||
try {
|
||||
const filePaths: string[] = await ipcRenderer.invoke(
|
||||
|
|
92
src/services/export.ts
Normal file
92
src/services/export.ts
Normal file
|
@ -0,0 +1,92 @@
|
|||
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);
|
||||
};
|
||||
|
||||
export const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
await fs.mkdir(dirPath);
|
||||
}
|
||||
};
|
||||
|
||||
export const checkExistsAndRename = async (
|
||||
oldDirPath: string,
|
||||
newDirPath: string
|
||||
) => {
|
||||
if (fs.existsSync(oldDirPath)) {
|
||||
await fs.rename(oldDirPath, newDirPath);
|
||||
}
|
||||
};
|
||||
|
||||
export const saveStreamToDisk = (
|
||||
path: string,
|
||||
fileStream: ReadableStream<any>
|
||||
) => {
|
||||
const writeable = fs.createWriteStream(path);
|
||||
const readable = responseToReadable(fileStream);
|
||||
readable.pipe(writeable);
|
||||
};
|
||||
|
||||
export const saveFileToDisk = async (path: string, file: any) => {
|
||||
await fs.writeFile(path, file);
|
||||
};
|
||||
|
||||
export const registerResumeExportListener = (resumeExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('resume-export');
|
||||
ipcRenderer.on('resume-export', () => resumeExport());
|
||||
};
|
||||
|
||||
export const registerStopExportListener = (abortExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('stop-export');
|
||||
ipcRenderer.on('stop-export', () => abortExport());
|
||||
};
|
||||
|
||||
export const registerPauseExportListener = (pauseExport: () => void) => {
|
||||
ipcRenderer.removeAllListeners('pause-export');
|
||||
ipcRenderer.on('pause-export', () => pauseExport());
|
||||
};
|
||||
|
||||
export const registerRetryFailedExportListener = (
|
||||
retryFailedExport: () => void
|
||||
) => {
|
||||
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);
|
||||
};
|
|
@ -2,9 +2,9 @@ import path from 'path';
|
|||
import StreamZip from 'node-stream-zip';
|
||||
import * as fs from 'promise-fs';
|
||||
import { FILE_STREAM_CHUNK_SIZE } from '../config';
|
||||
import { uploadStatusStore } from '../services/store';
|
||||
import { uploadStatusStore } from './store';
|
||||
import { ElectronFile, FILE_PATH_KEYS, FILE_PATH_TYPE } from '../types';
|
||||
import { logError } from './logging';
|
||||
import { logError } from '../utils/logging';
|
||||
|
||||
// https://stackoverflow.com/a/63111390
|
||||
export const getFilesFromDir = async (dirPath: string) => {
|
|
@ -10,7 +10,7 @@ import {
|
|||
import { createWindow } from './createWindow';
|
||||
import { buildContextMenu } from './menu';
|
||||
import { logErrorSentry } from './sentry';
|
||||
import { getFilesFromDir } from './upload';
|
||||
import { getFilesFromDir } from '../services/upload';
|
||||
|
||||
export default function setupIpcComs(
|
||||
tray: Tray,
|
||||
|
@ -38,6 +38,7 @@ export default function setupIpcComs(
|
|||
};
|
||||
new Notification(notification).show();
|
||||
});
|
||||
|
||||
ipcMain.on('reload-window', () => {
|
||||
const secondWindow = createWindow();
|
||||
mainWindow.destroy();
|
||||
|
|
Loading…
Reference in a new issue