From 73ef03a5d9842938d3310b56d158f827c16d2b6c Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 15 Apr 2024 12:14:57 +0530 Subject: [PATCH] Move read/write whole files into fs --- desktop/src/main/fs.ts | 12 +++--- desktop/src/main/ipc.ts | 44 ++++++++++---------- desktop/src/preload.ts | 36 ++++++++-------- web/apps/photos/src/services/export/index.ts | 9 ++-- web/packages/next/types/ipc.ts | 13 +++++- 5 files changed, 62 insertions(+), 52 deletions(-) diff --git a/desktop/src/main/fs.ts b/desktop/src/main/fs.ts index 11ab36049..94652d93d 100644 --- a/desktop/src/main/fs.ts +++ b/desktop/src/main/fs.ts @@ -17,6 +17,12 @@ export const fsRmdir = (path: string) => fs.rmdir(path); export const fsRm = (path: string) => fs.rm(path); +export const fsReadTextFile = async (filePath: string) => + fs.readFile(filePath, "utf-8"); + +export const fsWriteTextFile = (path: string, contents: string) => + fs.writeFile(path, contents); + /** * Write a (web) ReadableStream to a file at the given {@link filePath}. * @@ -84,12 +90,6 @@ const writeNodeStream = async ( export const saveStreamToDisk = writeStream; -export const saveFileToDisk = (path: string, contents: string) => - fs.writeFile(path, contents); - -export const readTextFile = async (filePath: string) => - fs.readFile(filePath, "utf-8"); - export const isFolder = async (dirPath: string) => { if (!existsSync(dirPath)) return false; const stats = await fs.stat(dirPath); diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts index 36e13ec60..17984c1cf 100644 --- a/desktop/src/main/ipc.ts +++ b/desktop/src/main/ipc.ts @@ -20,12 +20,12 @@ import { import { fsExists, fsMkdirIfNeeded, + fsReadTextFile, fsRename, fsRm, fsRmdir, + fsWriteTextFile, isFolder, - readTextFile, - saveFileToDisk, saveStreamToDisk, } from "./fs"; import { logToDisk } from "./log"; @@ -113,6 +113,26 @@ export const attachIPCHandlers = () => { ipcMain.on("skipAppUpdate", (_, version) => skipAppUpdate(version)); + // - FS + + ipcMain.handle("fsExists", (_, path) => fsExists(path)); + + ipcMain.handle("fsRename", (_, oldPath: string, newPath: string) => + fsRename(oldPath, newPath), + ); + + ipcMain.handle("fsMkdirIfNeeded", (_, dirPath) => fsMkdirIfNeeded(dirPath)); + + ipcMain.handle("fsRmdir", (_, path: string) => fsRmdir(path)); + + ipcMain.handle("fsRm", (_, path: string) => fsRm(path)); + + ipcMain.handle("fsReadTextFile", (_, path: string) => fsReadTextFile(path)); + + ipcMain.handle("fsWriteTextFile", (_, path: string, contents: string) => + fsWriteTextFile(path, contents), + ); + // - Conversion ipcMain.handle("convertToJPEG", (_, fileData, filename) => @@ -164,20 +184,6 @@ export const attachIPCHandlers = () => { ipcMain.handle("showUploadZipDialog", () => showUploadZipDialog()); - // - FS - - ipcMain.handle("fsExists", (_, path) => fsExists(path)); - - ipcMain.handle("fsRename", (_, oldPath: string, newPath: string) => - fsRename(oldPath, newPath), - ); - - ipcMain.handle("fsMkdirIfNeeded", (_, dirPath) => fsMkdirIfNeeded(dirPath)); - - ipcMain.handle("fsRmdir", (_, path: string) => fsRmdir(path)); - - ipcMain.handle("fsRm", (_, path: string) => fsRm(path)); - // - FS Legacy ipcMain.handle( @@ -186,12 +192,6 @@ export const attachIPCHandlers = () => { saveStreamToDisk(path, fileStream), ); - ipcMain.handle("saveFileToDisk", (_, path: string, contents: string) => - saveFileToDisk(path, contents), - ); - - ipcMain.handle("readTextFile", (_, path: string) => readTextFile(path)); - ipcMain.handle("isFolder", (_, dirPath: string) => isFolder(dirPath)); // - Upload diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index 1a344e832..057ca9ebb 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -96,6 +96,8 @@ const skipAppUpdate = (version: string) => { ipcRenderer.send("skipAppUpdate", version); }; +// - FS + const fsExists = (path: string): Promise => ipcRenderer.invoke("fsExists", path); @@ -110,6 +112,12 @@ const fsRmdir = (path: string): Promise => const fsRm = (path: string): Promise => ipcRenderer.invoke("fsRm", path); +const fsReadTextFile = (path: string): Promise => + ipcRenderer.invoke("fsReadTextFile", path); + +const fsWriteTextFile = (path: string, contents: string): Promise => + ipcRenderer.invoke("fsWriteTextFile", path, contents); + // - AUDIT below this // - Conversion @@ -234,12 +242,6 @@ const saveStreamToDisk = ( fileStream: ReadableStream, ): Promise => ipcRenderer.invoke("saveStreamToDisk", path, fileStream); -const saveFileToDisk = (path: string, contents: string): Promise => - ipcRenderer.invoke("saveFileToDisk", path, contents); - -const readTextFile = (path: string): Promise => - ipcRenderer.invoke("readTextFile", path); - const isFolder = (dirPath: string): Promise => ipcRenderer.invoke("isFolder", dirPath); @@ -316,6 +318,17 @@ contextBridge.exposeInMainWorld("electron", { updateOnNextRestart, skipAppUpdate, + // - FS + fs: { + exists: fsExists, + rename: fsRename, + mkdirIfNeeded: fsMkdirIfNeeded, + rmdir: fsRmdir, + rm: fsRm, + readTextFile: fsReadTextFile, + writeTextFile: fsWriteTextFile, + }, + // - Conversion convertToJPEG, generateImageThumbnail, @@ -341,20 +354,9 @@ contextBridge.exposeInMainWorld("electron", { updateWatchMappingSyncedFiles, updateWatchMappingIgnoredFiles, - // - FS - fs: { - exists: fsExists, - rename: fsRename, - mkdirIfNeeded: fsMkdirIfNeeded, - rmdir: fsRmdir, - rm: fsRm, - }, - // - FS legacy // TODO: Move these into fs + document + rename if needed saveStreamToDisk, - saveFileToDisk, - readTextFile, isFolder, // - Upload diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts index f7a0c3f3e..996b911de 100644 --- a/web/apps/photos/src/services/export/index.ts +++ b/web/apps/photos/src/services/export/index.ts @@ -884,7 +884,7 @@ class ExportService { try { const exportRecord = await this.getExportRecord(folder); const newRecord: ExportRecord = { ...exportRecord, ...newData }; - await ensureElectron().saveFileToDisk( + await ensureElectron().fs.writeTextFile( `${folder}/${exportRecordFileName}`, JSON.stringify(newRecord, null, 2), ); @@ -907,8 +907,7 @@ class ExportService { if (!(await fs.exists(exportRecordJSONPath))) { return this.createEmptyExportRecord(exportRecordJSONPath); } - const recordFile = - await electron.readTextFile(exportRecordJSONPath); + const recordFile = await fs.readTextFile(exportRecordJSONPath); try { return JSON.parse(recordFile); } catch (e) { @@ -1077,7 +1076,7 @@ class ExportService { fileExportName: string, file: EnteFile, ) { - await ensureElectron().saveFileToDisk( + await ensureElectron().fs.writeTextFile( getFileMetadataExportPath(collectionExportPath, fileExportName), getGoogleLikeMetadataFile(fileExportName, file), ); @@ -1106,7 +1105,7 @@ class ExportService { private createEmptyExportRecord = async (exportRecordJSONPath: string) => { const exportRecord: ExportRecord = NULL_EXPORT_RECORD; - await ensureElectron().saveFileToDisk( + await ensureElectron().fs.writeTextFile( exportRecordJSONPath, JSON.stringify(exportRecord, null, 2), ); diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts index 69b0c3593..70e21a10c 100644 --- a/web/packages/next/types/ipc.ts +++ b/web/packages/next/types/ipc.ts @@ -188,6 +188,17 @@ export interface Electron { * Delete the file at {@link path}. */ rm: (path: string) => Promise; + + /** + * Write a string to a file, replacing the file if it already exists. + * + * @param path The path of the file. + * @param contents The string contents to write. + */ + writeTextFile: (path: string, contents: string) => Promise; + + /** Read the string contents of a file at {@link path}. */ + readTextFile: (path: string) => Promise; }; /* @@ -304,8 +315,6 @@ export interface Electron { path: string, fileStream: ReadableStream, ) => Promise; - saveFileToDisk: (path: string, contents: string) => Promise; - readTextFile: (path: string) => Promise; isFolder: (dirPath: string) => Promise; // - Upload