move file to api dir

This commit is contained in:
Abhinav 2022-06-15 11:28:28 +05:30
parent 7b174fe402
commit 82284d27f3
6 changed files with 89 additions and 81 deletions

9
src/api/common.ts Normal file
View file

@ -0,0 +1,9 @@
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

@ -90,3 +90,11 @@ 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');
}
};

View file

@ -2,9 +2,10 @@ 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 './store';
import { uploadStatusStore } from '../services/store';
import { ElectronFile, FILE_PATH_KEYS, FILE_PATH_TYPE } from '../types';
import { logError } from '../utils/logging';
import { ipcRenderer } from 'electron';
// https://stackoverflow.com/a/63111390
export const getFilesFromDir = async (dirPath: string) => {
@ -236,3 +237,47 @@ export const getElectronFilesFromGoogleZip = async (filePath: string) => {
return files;
};
export const showUploadDirsDialog = async () => {
try {
const filePaths: string[] = await ipcRenderer.invoke(
'show-upload-dirs-dialog'
);
const files = await Promise.all(filePaths.map(getElectronFile));
return files;
} catch (e) {
logError(e, 'error while selecting folders');
}
};
export const showUploadFilesDialog = async () => {
try {
const filePaths: string[] = await ipcRenderer.invoke(
'show-upload-files-dialog'
);
const files = await Promise.all(filePaths.map(getElectronFile));
return files;
} catch (e) {
logError(e, 'error while selecting files');
}
};
export const showUploadZipDialog = async () => {
try {
const filePaths: string[] = await ipcRenderer.invoke(
'show-upload-zip-dialog'
);
const files: ElectronFile[] = [];
for (const filePath of filePaths) {
files.push(...(await getElectronFilesFromGoogleZip(filePath)));
}
return {
zipPaths: filePaths,
files,
};
} catch (e) {
logError(e, 'error while selecting zips');
}
};

View file

@ -1,13 +1,14 @@
import { webFrame, ipcRenderer } from 'electron';
import { reloadWindow, sendNotification, showOnTray } from './api/common';
import {
showUploadDirsDialog,
showUploadFilesDialog,
showUploadZipDialog,
getElectronFile,
getPendingUploads,
setToUploadFiles,
getElectronFilesFromGoogleZip,
setToUploadCollection,
} from './services/upload';
import { logError } from './utils/logging';
import { ElectronFile } from './types';
} from './api/upload';
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
import { clearElectronStore } from './utils/electronStore';
import { openDiskCache, deleteDiskCache } from './utils/cache';
@ -23,85 +24,14 @@ import {
getExportRecord,
setExportRecord,
exists,
} from './services/export';
// Patch the global WebSocket constructor to use the correct DevServer url
const fixHotReloadNext12 = () => {
webFrame.executeJavaScript(`Object.defineProperty(globalThis, 'WebSocket', {
value: new Proxy(WebSocket, {
construct: (Target, [url, protocols]) => {
if (url.endsWith('/_next/webpack-hmr')) {
// Fix the Next.js hmr client url
return new Target("ws://localhost:3000/_next/webpack-hmr", protocols)
} else {
return new Target(url, protocols)
}
}
})
});`);
};
selectRootDirectory,
} from './api/export';
import { fixHotReloadNext12 } from './utils/preload';
fixHotReloadNext12();
const selectRootDirectory = async () => {
try {
return await ipcRenderer.invoke('select-dir');
} catch (e) {
logError(e, 'error while selecting root directory');
}
};
const sendNotification = (content: string) => {
ipcRenderer.send('send-notification', content);
};
const showOnTray = (content: string) => {
ipcRenderer.send('update-tray', content);
};
const reloadWindow = () => {
ipcRenderer.send('reload-window');
};
const showUploadFilesDialog = async () => {
try {
const filePaths: string[] = await ipcRenderer.invoke(
'show-upload-files-dialog'
);
const files = await Promise.all(filePaths.map(getElectronFile));
return files;
} catch (e) {
logError(e, 'error while selecting files');
}
};
const showUploadDirsDialog = async () => {
try {
const filePaths: string[] = await ipcRenderer.invoke(
'show-upload-dirs-dialog'
);
const files = await Promise.all(filePaths.map(getElectronFile));
return files;
} catch (e) {
logError(e, 'error while selecting folders');
}
};
const showUploadZipDialog = async () => {
try {
const filePaths: string[] = await ipcRenderer.invoke(
'show-upload-zip-dialog'
);
const files: ElectronFile[] = [];
for (const filePath of filePaths) {
files.push(...(await getElectronFilesFromGoogleZip(filePath)));
}
return { zipPaths: filePaths, files };
} catch (e) {
logError(e, 'error while selecting zips');
}
};
const windowObject: any = window;
windowObject['ElectronAPIs'] = {
exists,
checkExistsAndCreateCollectionDir,

View file

@ -10,7 +10,7 @@ import {
import { createWindow } from './createWindow';
import { buildContextMenu } from './menu';
import { logErrorSentry } from './sentry';
import { getFilesFromDir } from '../services/upload';
import { getFilesFromDir } from '../api/upload';
export default function setupIpcComs(
tray: Tray,

16
src/utils/preload.ts Normal file
View file

@ -0,0 +1,16 @@
import { webFrame } from 'electron';
export const fixHotReloadNext12 = () => {
webFrame.executeJavaScript(`Object.defineProperty(globalThis, 'WebSocket', {
value: new Proxy(WebSocket, {
construct: (Target, [url, protocols]) => {
if (url.endsWith('/_next/webpack-hmr')) {
// Fix the Next.js hmr client url
return new Target("ws://localhost:3000/_next/webpack-hmr", protocols)
} else {
return new Target(url, protocols)
}
}
})
});`);
};