Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Start migrating from promise-fs

Manav Rathi8391e7edfca7
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 32a7a21a13..b66409137a 100644
--- a/desktop/src/preload.ts
+++ b/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
diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts
index 7190b5b934..df17a6e79b 100644
--- a/web/apps/photos/src/services/export/index.ts
+++ b/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;
diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts
index 576ccd3361..8081d86e99 100644
--- a/web/apps/photos/src/utils/file/index.ts
+++ b/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;
diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.ts
index 43d55faac9..304ada6c41 100644
--- a/web/packages/shared/electron/types.ts
+++ b/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,