This commit is contained in:
Abhinav 2022-06-15 13:52:20 +05:30
parent 5b3dca3343
commit a537ee41da
8 changed files with 121 additions and 91 deletions

View file

@ -1,11 +1,10 @@
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron/renderer';
import { logError } from '../utils/logging';
export const sendNotification = (content: string) => { export const selectRootDirectory = async () => {
ipcRenderer.send('send-notification', content); try {
}; return await ipcRenderer.invoke('select-dir');
export const showOnTray = (content: string) => { } catch (e) {
ipcRenderer.send('update-tray', content); logError(e, 'error while selecting root directory');
}; }
export const reloadWindow = () => {
ipcRenderer.send('reload-window');
}; };

View file

@ -1,30 +1,22 @@
import {} from './common';
import {
createDirectory,
doesFileExists,
readTextFile,
renameDirectory,
writeFile,
writeStream,
} from './../services/fs';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import * as fs from 'promise-fs';
import { Readable } from 'stream';
import { logError } from '../utils/logging'; 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) => { export const exists = (path: string) => {
return fs.existsSync(path); return doesFileExists(path);
}; };
export const checkExistsAndCreateCollectionDir = async (dirPath: string) => { export const checkExistsAndCreateCollectionDir = async (dirPath: string) => {
if (!fs.existsSync(dirPath)) { if (!doesFileExists(dirPath)) {
await fs.mkdir(dirPath); await createDirectory(dirPath);
} }
}; };
@ -32,22 +24,37 @@ export const checkExistsAndRename = async (
oldDirPath: string, oldDirPath: string,
newDirPath: string newDirPath: string
) => { ) => {
if (fs.existsSync(oldDirPath)) { if (doesFileExists(oldDirPath)) {
await fs.rename(oldDirPath, newDirPath); await renameDirectory(oldDirPath, newDirPath);
} }
}; };
export const saveStreamToDisk = ( export const saveStreamToDisk = (
path: string, filePath: string,
fileStream: ReadableStream<any> fileStream: ReadableStream<any>
) => { ) => {
const writeable = fs.createWriteStream(path); writeStream(filePath, fileStream);
const readable = responseToReadable(fileStream);
readable.pipe(writeable);
}; };
export const saveFileToDisk = async (path: string, file: any) => { export const saveFileToDisk = async (path: string, fileData: any) => {
await fs.writeFile(path, file); 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) => { export const registerResumeExportListener = (resumeExport: () => void) => {
@ -71,30 +78,3 @@ export const registerRetryFailedExportListener = (
ipcRenderer.removeAllListeners('retry-export'); ipcRenderer.removeAllListeners('retry-export');
ipcRenderer.on('retry-export', () => retryFailedExport()); 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
View 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');
};

View file

@ -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 () => { export const getPendingUploads = async () => {
const filePaths = getSavedPaths(FILE_PATH_TYPE.FILES); const filePaths = getSavedPaths(FILE_PATH_TYPE.FILES);
const zipPaths = getSavedPaths(FILE_PATH_TYPE.ZIPS); const zipPaths = getSavedPaths(FILE_PATH_TYPE.ZIPS);
@ -143,3 +133,13 @@ export const showUploadZipDialog = async () => {
logError(e, 'error while selecting zips'); 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;
};

View file

@ -2,7 +2,7 @@ import path from 'path';
import { watchStore } from '../services/store'; import { watchStore } from '../services/store';
import { ipcRenderer } from 'electron'; import { ipcRenderer } from 'electron';
import { ElectronFile, WatchStoreType } from '../types'; import { ElectronFile, WatchStoreType } from '../types';
import { getElectronFile, getFilesFromDir } from './upload'; import { getElectronFile, getFilesFromDir } from '../services/fs';
export async function addWatchMapping( export async function addWatchMapping(
collectionName: string, collectionName: string,
@ -65,13 +65,8 @@ export function setWatchMappings(watchMappings: WatchStoreType['mappings']) {
} }
export async function getAllFilesFromDir(dirPath: string) { export async function getAllFilesFromDir(dirPath: string) {
let files = await getFilesFromDir(dirPath); const files = await getFilesFromDir(dirPath);
files = files.map((file) => file.split(path.sep).join(path.posix.sep)); const electronFiles = await Promise.all(files.map(getElectronFile));
const electronFiles = await Promise.all(
files.map(async (filePath) => {
return await getElectronFile(filePath);
})
);
return electronFiles; return electronFiles;
} }

View file

@ -1,14 +1,8 @@
import { import { reloadWindow, sendNotification, showOnTray } from './api/system';
reloadWindow,
sendNotification,
showOnTray,
doesFolderExists,
} from './api/common';
import { import {
showUploadDirsDialog, showUploadDirsDialog,
showUploadFilesDialog, showUploadFilesDialog,
showUploadZipDialog, showUploadZipDialog,
getElectronFile,
getPendingUploads, getPendingUploads,
setToUploadFiles, setToUploadFiles,
getElectronFilesFromGoogleZip, getElectronFilesFromGoogleZip,
@ -34,8 +28,9 @@ import {
getExportRecord, getExportRecord,
setExportRecord, setExportRecord,
exists, exists,
selectRootDirectory,
} from './api/export'; } from './api/export';
import { selectRootDirectory } from './api/common';
import { getElectronFile, doesFolderExists } from './services/fs';
const windowObject: any = window; const windowObject: any = window;

View file

@ -4,6 +4,7 @@ import * as fs from 'promise-fs';
import { ElectronFile } from '../types'; import { ElectronFile } from '../types';
import { logError } from '../utils/logging'; import { logError } from '../utils/logging';
import StreamZip from 'node-stream-zip'; import StreamZip from 'node-stream-zip';
import { Readable } from 'stream';
// https://stackoverflow.com/a/63111390 // https://stackoverflow.com/a/63111390
export const getFilesFromDir = async (dirPath: string) => { export const getFilesFromDir = async (dirPath: string) => {
@ -150,3 +151,52 @@ export async function doesFolderExists(dirPath: string) {
}) })
.catch(() => false); .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');
}

View file

@ -2,9 +2,9 @@ import { BrowserWindow, dialog, ipcMain, Tray, Notification } from 'electron';
import { createWindow } from './createWindow'; import { createWindow } from './createWindow';
import { buildContextMenu } from './menuUtil'; import { buildContextMenu } from './menuUtil';
import { logErrorSentry } from './sentry'; import { logErrorSentry } from './sentry';
import { getFilesFromDir } from '../api/upload';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import path from 'path'; import path from 'path';
import { getFilesFromDir } from '../services/fs';
export default function setupIpcComs( export default function setupIpcComs(
tray: Tray, tray: Tray,