瀏覽代碼

Move mkdirIfNeeded into fs

Manav Rathi 1 年之前
父節點
當前提交
7438c3301e

+ 3 - 3
desktop/src/main/fs.ts

@@ -11,6 +11,9 @@ export const fsExists = (path: string) => existsSync(path);
 export const fsRename = (oldPath: string, newPath: string) =>
     fs.rename(oldPath, newPath);
 
+export const fsMkdirIfNeeded = (dirPath: string) =>
+    fs.mkdir(dirPath, { recursive: true });
+
 /**
  * Write a (web) ReadableStream to a file at the given {@link filePath}.
  *
@@ -76,9 +79,6 @@ const writeNodeStream = async (
 
 /* TODO: Audit below this  */
 
-export const checkExistsAndCreateDir = (dirPath: string) =>
-    fs.mkdir(dirPath, { recursive: true });
-
 export const saveStreamToDisk = writeStream;
 
 export const saveFileToDisk = (path: string, contents: string) =>

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

@@ -18,10 +18,10 @@ import {
     showUploadZipDialog,
 } from "./dialogs";
 import {
-    checkExistsAndCreateDir,
     deleteFile,
     deleteFolder,
     fsExists,
+    fsMkdirIfNeeded,
     fsRename,
     isFolder,
     moveFile,
@@ -173,11 +173,9 @@ export const attachIPCHandlers = () => {
         fsRename(oldPath, newPath),
     );
 
-    // - FS Legacy
+    ipcMain.handle("fsMkdirIfNeeded", (_, dirPath) => fsMkdirIfNeeded(dirPath));
 
-    ipcMain.handle("checkExistsAndCreateDir", (_, dirPath) =>
-        checkExistsAndCreateDir(dirPath),
-    );
+    // - FS Legacy
 
     ipcMain.handle(
         "saveStreamToDisk",

+ 4 - 4
desktop/src/preload.ts

@@ -99,6 +99,9 @@ const skipAppUpdate = (version: string) => {
 const fsExists = (path: string): Promise<boolean> =>
     ipcRenderer.invoke("fsExists", path);
 
+const fsMkdirIfNeeded = (dirPath: string): Promise<void> =>
+    ipcRenderer.invoke("fsMkdirIfNeeded", dirPath);
+
 const fsRename = (oldPath: string, newPath: string): Promise<void> =>
     ipcRenderer.invoke("fsRename", oldPath, newPath);
 
@@ -221,9 +224,6 @@ const updateWatchMappingIgnoredFiles = (
 
 // - FS Legacy
 
-const checkExistsAndCreateDir = (dirPath: string): Promise<void> =>
-    ipcRenderer.invoke("checkExistsAndCreateDir", dirPath);
-
 const saveStreamToDisk = (
     path: string,
     fileStream: ReadableStream,
@@ -349,11 +349,11 @@ contextBridge.exposeInMainWorld("electron", {
     fs: {
         exists: fsExists,
         rename: fsRename,
+        mkdirIfNeeded: fsMkdirIfNeeded,
     },
 
     // - FS legacy
     // TODO: Move these into fs + document + rename if needed
-    checkExistsAndCreateDir,
     saveStreamToDisk,
     saveFileToDisk,
     readTextFile,

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

@@ -166,13 +166,14 @@ class ExportService {
     }
 
     async changeExportDirectory() {
+        const electron = ensureElectron();
         try {
-            const newRootDir = await ensureElectron().selectDirectory();
+            const newRootDir = await electron.selectDirectory();
             if (!newRootDir) {
                 throw Error(CustomError.SELECT_FOLDER_ABORTED);
             }
             const newExportDir = `${newRootDir}/${exportDirectoryName}`;
-            await ensureElectron().checkExistsAndCreateDir(newExportDir);
+            await electron.fs.mkdirIfNeeded(newExportDir);
             return newExportDir;
         } catch (e) {
             if (e.message !== CustomError.SELECT_FOLDER_ABORTED) {
@@ -648,6 +649,7 @@ class ExportService {
         incrementFailed: () => void,
         isCanceled: CancellationStatus,
     ): Promise<void> {
+        const fs = ensureElectron().fs;
         try {
             for (const file of files) {
                 log.info(
@@ -683,10 +685,8 @@ class ExportService {
                         );
                     }
                     const collectionExportPath = `${exportDir}/${collectionExportName}`;
-                    await ensureElectron().checkExistsAndCreateDir(
-                        collectionExportPath,
-                    );
-                    await ensureElectron().checkExistsAndCreateDir(
+                    await fs.mkdirIfNeeded(collectionExportPath);
+                    await fs.mkdirIfNeeded(
                         getMetadataFolderExportPath(collectionExportPath),
                     );
                     await this.downloadAndSave(
@@ -1019,17 +1019,17 @@ class ExportService {
         collectionID: number,
         collectionIDNameMap: Map<number, string>,
     ) {
-        const electron = ensureElectron();
+        const fs = ensureElectron().fs;
         await this.verifyExportFolderExists(exportFolder);
         const collectionName = collectionIDNameMap.get(collectionID);
         const collectionExportName = await safeDirectoryName(
             exportFolder,
             collectionName,
-            electron.fs.exists,
+            fs.exists,
         );
         const collectionExportPath = `${exportFolder}/${collectionExportName}`;
-        await electron.checkExistsAndCreateDir(collectionExportPath);
-        await electron.checkExistsAndCreateDir(
+        await fs.mkdirIfNeeded(collectionExportPath);
+        await fs.mkdirIfNeeded(
             getMetadataFolderExportPath(collectionExportPath),
         );
 
@@ -1172,14 +1172,6 @@ class ExportService {
         return this.exportInProgress;
     };
 
-    rename = (oldPath: string, newPath: string) => {
-        return ensureElectron().fs.rename(oldPath, newPath);
-    };
-
-    checkExistsAndCreateDir = (path: string) => {
-        return ensureElectron().checkExistsAndCreateDir(path);
-    };
-
     exportFolderExists = async (exportFolder: string) => {
         return exportFolder && (await ensureElectron().fs.exists(exportFolder));
     };

+ 3 - 3
web/apps/photos/src/utils/collection/index.ts

@@ -31,7 +31,6 @@ import {
     updatePublicCollectionMagicMetadata,
     updateSharedCollectionMagicMetadata,
 } from "services/collectionService";
-import exportService from "services/export";
 import { getAllLocalFiles, getLocalFiles } from "services/fileService";
 import {
     COLLECTION_ROLE,
@@ -170,13 +169,14 @@ async function createCollectionDownloadFolder(
     downloadDirPath: string,
     collectionName: string,
 ) {
+    const fs = ensureElectron().fs;
     const collectionDownloadName = await safeDirectoryName(
         downloadDirPath,
         collectionName,
-        ensureElectron().fs.exists,
+        fs.exists,
     );
     const collectionDownloadPath = `${downloadDirPath}/${collectionDownloadName}`;
-    await exportService.checkExistsAndCreateDir(collectionDownloadPath);
+    await fs.mkdirIfNeeded(collectionDownloadPath);
     return collectionDownloadPath;
 }
 

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

@@ -161,6 +161,17 @@ export interface Electron {
     fs: {
         /** Return true if there is an item at the given {@link path}. */
         exists: (path: string) => Promise<boolean>;
+
+        /**
+         * mkdir -p
+         *
+         * Create a directory at the given path if it does not already exist.
+         * Any parent directories in the path that don't already exist will also
+         * be created recursively, i.e. this command is analogous to an running
+         * `mkdir -p`.
+         */
+        mkdirIfNeeded: (dirPath: string) => Promise<void>;
+
         /** Rename {@link oldPath} to {@link newPath} */
         rename: (oldPath: string, newPath: string) => Promise<void>;
     };
@@ -275,7 +286,6 @@ export interface Electron {
     ) => Promise<void>;
 
     // - FS legacy
-    checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
     saveStreamToDisk: (
         path: string,
         fileStream: ReadableStream,