From b1f45c8602171870fc4c6f081e6dd8b784d8480c Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Fri, 22 Mar 2024 09:38:50 +0530 Subject: [PATCH] Move export functions to preload --- desktop/src/api/export.ts | 23 -------------- desktop/src/preload.ts | 66 +++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 40 deletions(-) delete mode 100644 desktop/src/api/export.ts diff --git a/desktop/src/api/export.ts b/desktop/src/api/export.ts deleted file mode 100644 index 8adaa236f..000000000 --- a/desktop/src/api/export.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as fs from "promise-fs"; -import { writeStream } from "./../services/fs"; - -export const exists = (path: string) => { - return fs.existsSync(path); -}; - -export const checkExistsAndCreateDir = async (dirPath: string) => { - if (!fs.existsSync(dirPath)) { - await fs.mkdir(dirPath); - } -}; - -export const saveStreamToDisk = async ( - filePath: string, - fileStream: ReadableStream, -) => { - await writeStream(filePath, fileStream); -}; - -export const saveFileToDisk = async (path: string, fileData: string) => { - await fs.writeFile(path, fileData); -}; diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index 72ba5c61b..d2740eb5c 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -34,12 +34,6 @@ import * as fs from "promise-fs"; import { Readable } from "stream"; import { deleteDiskCache, openDiskCache } from "./api/cache"; import { logToDisk, openLogDirectory } from "./api/common"; -import { - checkExistsAndCreateDir, - exists, - saveFileToDisk, - saveStreamToDisk, -} from "./api/export"; import { runFFmpegCmd } from "./api/ffmpeg"; import { getDirFiles } from "./api/fs"; import { convertToJPEG, generateImageThumbnail } from "./api/imageProcessor"; @@ -67,8 +61,12 @@ import { } from "./api/watch"; import { setupLogging } from "./utils/logging"; -/* Some of the code below has been duplicated to make this file self contained. -Enhancement: consider alternatives */ +/* + Some of the code below has been duplicated to make this file self contained + (see the documentation at the top of why it needs to be a single file). + + Enhancement: consider alternatives +*/ /* preload: duplicated logError */ export function logError(error: Error, message: string, info?: string): void { @@ -133,6 +131,29 @@ export async function writeStream( await writeNodeStream(filePath, readable); } +// - Export + +export const exists = (path: string) => { + return fs.existsSync(path); +}; + +export const checkExistsAndCreateDir = async (dirPath: string) => { + if (!fs.existsSync(dirPath)) { + await fs.mkdir(dirPath); + } +}; + +export const saveStreamToDisk = async ( + filePath: string, + fileStream: ReadableStream, +) => { + await writeStream(filePath, fileStream); +}; + +export const saveFileToDisk = async (path: string, fileData: string) => { + await fs.writeFile(path, fileData); +}; + // - async function readTextFile(filePath: string) { @@ -211,7 +232,7 @@ function deleteFile(filePath: string): void { fs.rmSync(filePath); } -// - +// - ML /* preload: duplicated Model */ export enum Model { @@ -315,7 +336,7 @@ const parseExecError = (err: any) => { } }; -// - +// - General const selectDirectory = async (): Promise => { try { @@ -349,7 +370,7 @@ const clearElectronStore = () => { ipcRenderer.send("clear-electron-store"); }; -// - +// - App update const updateAndRestart = () => { ipcRenderer.send("update-and-restart"); @@ -410,10 +431,12 @@ setupLogging(); // running out of memory, causing the app to crash as it copies it over across // the processes. contextBridge.exposeInMainWorld("ElectronAPIs", { + // - Export exists, checkExistsAndCreateDir, saveStreamToDisk, saveFileToDisk, + selectDirectory, clearElectronStore, readTextFile, @@ -438,20 +461,29 @@ contextBridge.exposeInMainWorld("ElectronAPIs", { updateWatchMappingIgnoredFiles, logToDisk, convertToJPEG, - openLogDirectory, registerUpdateEventListener, - updateAndRestart, - skipAppUpdate, - getAppVersion, + runFFmpegCmd, - muteUpdateNotification, generateImageThumbnail, registerForegroundEventListener, - openDirectory, moveFile, deleteFolder, rename, deleteFile, + + // General + getAppVersion, + openDirectory, + + // Logging + openLogDirectory, + + // - App update + updateAndRestart, + skipAppUpdate, + muteUpdateNotification, + + // - ML computeImageEmbedding, computeTextEmbedding, });