Selaa lähdekoodia

Start migrating from promise-fs

Manav Rathi 1 vuosi sitten
vanhempi
commit
8391e7edfc

+ 14 - 12
desktop/src/preload.ts

@@ -28,10 +28,10 @@
  */
 
 import { contextBridge, ipcRenderer } from "electron";
-import { existsSync } from "fs";
+import { createWriteStream, existsSync } from "node:fs";
+import * as fs from "node:fs/promises";
+import { Readable } from "node:stream";
 import path from "path";
-import * as fs from "promise-fs";
-import { Readable } from "stream";
 import { deleteDiskCache, openDiskCache } from "./api/cache";
 import { logToDisk, openLogDirectory } from "./api/common";
 import { runFFmpegCmd } from "./api/ffmpeg";
@@ -120,7 +120,7 @@ const writeNodeStream = async (
     filePath: string,
     fileStream: NodeJS.ReadableStream,
 ) => {
-    const writeable = fs.createWriteStream(filePath);
+    const writeable = createWriteStream(filePath);
 
     fileStream.on("error", (error) => {
         writeable.destroy(error); // Close the writable stream with an error
@@ -130,7 +130,7 @@ const writeNodeStream = async (
 
     await new Promise((resolve, reject) => {
         writeable.on("finish", resolve);
-        writeable.on("error", async (e) => {
+        writeable.on("error", async (e: unknown) => {
             if (existsSync(filePath)) {
                 await fs.unlink(filePath);
             }
@@ -141,10 +141,10 @@ const writeNodeStream = async (
 
 // - Export
 
-const exists = (path: string) => fs.existsSync(path);
+const exists = (path: string) => existsSync(path);
 
 const checkExistsAndCreateDir = async (dirPath: string) => {
-    if (!fs.existsSync(dirPath)) {
+    if (!existsSync(dirPath)) {
         await fs.mkdir(dirPath);
     }
 };
@@ -204,7 +204,8 @@ async function deleteFolder(folderPath: string): Promise<void> {
     if (!existsSync(folderPath)) {
         return;
     }
-    if (!fs.statSync(folderPath).isDirectory()) {
+    const stat = await fs.stat(folderPath);
+    if (!stat.isDirectory()) {
         throw new Error("Path is not a folder");
     }
     // check if folder is empty
@@ -222,15 +223,16 @@ async function rename(oldPath: string, newPath: string) {
     await fs.rename(oldPath, newPath);
 }
 
-function deleteFile(filePath: string): void {
+const deleteFile = async (filePath: string) => {
     if (!existsSync(filePath)) {
         return;
     }
-    if (!fs.statSync(filePath).isFile()) {
+    const stat = await fs.stat(filePath);
+    if (!stat.isFile()) {
         throw new Error("Path is not a file");
     }
-    fs.rmSync(filePath);
-}
+    return fs.rm(filePath);
+};
 
 // - ML
 

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

@@ -1150,7 +1150,7 @@ class ExportService {
                     videoStream,
                 );
             } catch (e) {
-                ElectronAPIs.deleteFile(
+                await ElectronAPIs.deleteFile(
                     getFileExportPath(collectionExportPath, imageExportName),
                 );
                 throw e;

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

@@ -51,10 +51,7 @@ import {
 import { FileTypeInfo } from "types/upload";
 import { isPlaybackPossible } from "utils/photoFrame";
 
-import {
-    default as ElectronAPIs,
-    default as ElectronFSService,
-} from "@ente/shared/electron";
+import { default as ElectronAPIs } from "@ente/shared/electron";
 import { downloadUsingAnchor } from "@ente/shared/utils";
 import { t } from "i18next";
 import imageProcessor from "services/imageProcessor";
@@ -801,7 +798,7 @@ export async function downloadFileDesktop(
                 videoStream,
             );
         } catch (e) {
-            ElectronFSService.deleteFile(
+            await ElectronAPIs.deleteFile(
                 getFileExportPath(downloadPath, imageExportName),
             );
             throw e;

+ 1 - 1
web/packages/shared/electron/types.ts

@@ -96,7 +96,7 @@ export interface ElectronAPIsType {
     openDirectory: (dirPath: string) => Promise<void>;
     moveFile: (oldPath: string, newPath: string) => Promise<void>;
     deleteFolder: (path: string) => Promise<void>;
-    deleteFile: (path: string) => void;
+    deleteFile: (path: string) => Promise<void>;
     rename: (oldPath: string, newPath: string) => Promise<void>;
     computeImageEmbedding: (
         model: Model,