diff --git a/src/api/common.ts b/src/api/common.ts new file mode 100644 index 000000000..b21a3f7c0 --- /dev/null +++ b/src/api/common.ts @@ -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'); +}; diff --git a/src/services/export.ts b/src/api/export.ts similarity index 92% rename from src/services/export.ts rename to src/api/export.ts index d7058ce48..8ba2f79c4 100644 --- a/src/services/export.ts +++ b/src/api/export.ts @@ -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'); + } +}; diff --git a/src/services/upload.ts b/src/api/upload.ts similarity index 84% rename from src/services/upload.ts rename to src/api/upload.ts index 1d5f94503..a45d3330f 100644 --- a/src/services/upload.ts +++ b/src/api/upload.ts @@ -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'); + } +}; diff --git a/src/preload.ts b/src/preload.ts index d72c1a802..c185eaa2e 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -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, diff --git a/src/utils/ipcComms.ts b/src/utils/ipcComms.ts index 1bb37e920..471a2668f 100644 --- a/src/utils/ipcComms.ts +++ b/src/utils/ipcComms.ts @@ -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, diff --git a/src/utils/preload.ts b/src/utils/preload.ts new file mode 100644 index 000000000..f29a1d543 --- /dev/null +++ b/src/utils/preload.ts @@ -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) + } + } + }) + });`); +};