Browse Source

Remove unnecessary checks

It is simpler for us to parallel the rm command than our bespoke variant.
Manav Rathi 1 year ago
parent
commit
eb64a00ed2

+ 2 - 12
desktop/src/main/fs.ts

@@ -16,6 +16,8 @@ export const fsMkdirIfNeeded = (dirPath: string) =>
 
 
 export const fsRmdir = (path: string) => fs.rmdir(path);
 export const fsRmdir = (path: string) => fs.rmdir(path);
 
 
+export const fsRm = (path: string) => fs.rm(path);
+
 /**
 /**
  * Write a (web) ReadableStream to a file at the given {@link filePath}.
  * Write a (web) ReadableStream to a file at the given {@link filePath}.
  *
  *
@@ -107,15 +109,3 @@ export const isFolder = async (dirPath: string) => {
     const stats = await fs.stat(dirPath);
     const stats = await fs.stat(dirPath);
     return stats.isDirectory();
     return stats.isDirectory();
 };
 };
-
-export const deleteFile = async (filePath: string) => {
-    // Ensure it exists
-    if (!existsSync(filePath)) return;
-
-    // And is a file
-    const stat = await fs.stat(filePath);
-    if (!stat.isFile()) throw new Error("Path is not a file");
-
-    // rm it
-    return fs.rm(filePath);
-};

+ 5 - 5
desktop/src/main/ipc.ts

@@ -18,7 +18,7 @@ import {
     showUploadZipDialog,
     showUploadZipDialog,
 } from "./dialogs";
 } from "./dialogs";
 import {
 import {
-    deleteFile,
+    fsRm,
     fsRmdir,
     fsRmdir,
     fsExists,
     fsExists,
     fsMkdirIfNeeded,
     fsMkdirIfNeeded,
@@ -175,6 +175,10 @@ export const attachIPCHandlers = () => {
 
 
     ipcMain.handle("fsMkdirIfNeeded", (_, dirPath) => fsMkdirIfNeeded(dirPath));
     ipcMain.handle("fsMkdirIfNeeded", (_, dirPath) => fsMkdirIfNeeded(dirPath));
 
 
+    ipcMain.handle("fsRmdir", (_, path: string) => fsRmdir(path));
+
+    ipcMain.handle("fsRm", (_, path: string) => fsRm(path));
+
     // - FS Legacy
     // - FS Legacy
 
 
     ipcMain.handle(
     ipcMain.handle(
@@ -195,10 +199,6 @@ export const attachIPCHandlers = () => {
         moveFile(oldPath, newPath),
         moveFile(oldPath, newPath),
     );
     );
 
 
-    ipcMain.handle("fsRmdir", (_, path: string) => fsRmdir(path));
-
-    ipcMain.handle("deleteFile", (_, path: string) => deleteFile(path));
-
     // - Upload
     // - Upload
 
 
     ipcMain.handle("getPendingUploads", () => getPendingUploads());
     ipcMain.handle("getPendingUploads", () => getPendingUploads());

+ 2 - 3
desktop/src/preload.ts

@@ -244,8 +244,7 @@ const moveFile = (oldPath: string, newPath: string): Promise<void> =>
 const fsRmdir = (path: string): Promise<void> =>
 const fsRmdir = (path: string): Promise<void> =>
     ipcRenderer.invoke("fsRmdir", path);
     ipcRenderer.invoke("fsRmdir", path);
 
 
-const deleteFile = (path: string): Promise<void> =>
-    ipcRenderer.invoke("deleteFile", path);
+const fsRm = (path: string): Promise<void> => ipcRenderer.invoke("fsRm", path);
 
 
 // - Upload
 // - Upload
 
 
@@ -351,6 +350,7 @@ contextBridge.exposeInMainWorld("electron", {
         rename: fsRename,
         rename: fsRename,
         mkdirIfNeeded: fsMkdirIfNeeded,
         mkdirIfNeeded: fsMkdirIfNeeded,
         rmdir: fsRmdir,
         rmdir: fsRmdir,
+        rm: fsRm,
     },
     },
 
 
     // - FS legacy
     // - FS legacy
@@ -360,7 +360,6 @@ contextBridge.exposeInMainWorld("electron", {
     readTextFile,
     readTextFile,
     isFolder,
     isFolder,
     moveFile,
     moveFile,
-    deleteFile,
 
 
     // - Upload
     // - Upload
 
 

+ 1 - 1
web/apps/photos/src/services/export/index.ts

@@ -1147,7 +1147,7 @@ class ExportService {
                     videoStream,
                     videoStream,
                 );
                 );
             } catch (e) {
             } catch (e) {
-                await electron.deleteFile(
+                await electron.fs.rm(
                     `${collectionExportPath}/${imageExportName}`,
                     `${collectionExportPath}/${imageExportName}`,
                 );
                 );
                 throw e;
                 throw e;

+ 1 - 1
web/apps/photos/src/utils/file/index.ts

@@ -834,7 +834,7 @@ async function downloadFileDesktop(
                 videoStream,
                 videoStream,
             );
             );
         } catch (e) {
         } catch (e) {
-            await electron.deleteFile(`${downloadPath}/${imageExportName}`);
+            await electron.fs.rm(`${downloadPath}/${imageExportName}`);
             throw e;
             throw e;
         }
         }
     } else {
     } else {

+ 7 - 1
web/packages/next/types/ipc.ts

@@ -181,6 +181,13 @@ export interface Electron {
          * Delete the directory at the {@link path} if it is empty.
          * Delete the directory at the {@link path} if it is empty.
          */
          */
         rmdir: (path: string) => Promise<void>;
         rmdir: (path: string) => Promise<void>;
+
+        /**
+         * Equivalent of `rm`.
+         *
+         * Delete the file at {@link path}.
+         */
+        rm: (path: string) => Promise<void>;
     };
     };
 
 
     /*
     /*
@@ -301,7 +308,6 @@ export interface Electron {
     readTextFile: (path: string) => Promise<string>;
     readTextFile: (path: string) => Promise<string>;
     isFolder: (dirPath: string) => Promise<boolean>;
     isFolder: (dirPath: string) => Promise<boolean>;
     moveFile: (oldPath: string, newPath: string) => Promise<void>;
     moveFile: (oldPath: string, newPath: string) => Promise<void>;
-    deleteFile: (path: string) => Promise<void>;
 
 
     // - Upload
     // - Upload