Skip to content

armgit / ente

Public Imported from GitHub
0

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

Expose an fs.exists over the context bridge

Manav Rathia5c3aff54bed
diff --git a/desktop/src/main/fs.ts b/desktop/src/main/fs.ts
new file mode 100644
index 0000000000..50a9ddc61a
--- /dev/null
+++ b/desktop/src/main/fs.ts
@@ -0,0 +1,6 @@
+/**
+ * @file file system related functions exposed over the context bridge.
+ */
+import { existsSync } from "node:fs";
+
+export const fsExists = (path: string) => existsSync(path);
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index 2e2b3c0040..83726fc8d0 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -10,6 +10,7 @@ import { ipcMain } from "electron/main";
import { appVersion } from "../services/appUpdater";
import { openDirectory, openLogDirectory } from "./general";
import { logToDisk } from "./log";
+import { fsExists } from "./fs";
// - General
@@ -40,4 +41,6 @@ export const attachIPCHandlers = () => {
// See: [Note: Catching exception during .send/.on]
ipcMain.on("logToDisk", (_, msg) => logToDisk(msg));
+
+ ipcMain.handle("fsExists", (_, path) => fsExists(path));
};
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index 1f7792bf3e..1dff6a8e9c 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -88,6 +88,13 @@ const openLogDirectory = (): Promise<void> =>
const logToDisk = (message: string): void =>
ipcRenderer.send("logToDisk", message);
+/**
+ * Return true if there is a file or directory at the given
+ * {@link path}.
+ */
+const fsExists = (path: string): Promise<boolean> =>
+ ipcRenderer.invoke("fsExists", path);
+
// - FIXME below this
/* preload: duplicated logError */
@@ -429,6 +436,11 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
skipAppUpdate,
muteUpdateNotification,
+ // - FS
+ fs: {
+ exists: fsExists,
+ },
+
// - Export
exists,
checkExistsAndCreateDir,
diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.ts
index 11db095c25..5eed393709 100644
--- a/web/packages/shared/electron/types.ts
+++ b/web/packages/shared/electron/types.ts
@@ -50,6 +50,34 @@ export interface ElectronAPIsType {
*/
logToDisk: (message: string) => void;
+ /**
+ * A subset of filesystem access APIs.
+ *
+ * The renderer process, being a web process, does not have full access to
+ * the local filesystem apart from files explicitly dragged and dropped (or
+ * selected by the user in a native file open dialog).
+ *
+ * The main process, however, has full filesystem access (limited only be an
+ * OS level sandbox on the entire process).
+ *
+ * When we're running in the desktop app, we want to better utilize the
+ * local filesystem access to provide more integrated features to the user -
+ * things that are not currently possible using web technologies. For
+ * example, continuous exports to an arbitrary user chosen location on disk,
+ * or watching some folders for changes and syncing them automatically.
+ *
+ * Towards this end, this fs object provides some generic file system access
+ * functions that are needed for such features. In addition, there are other
+ * feature specific methods too in the top level electron object.
+ */
+ fs: {
+ /**
+ * Return true if there is a file or directory at the given
+ * {@link path}.
+ */
+ exists: (path: string) => Promise<boolean>;
+ };
+
exists: (path: string) => boolean;
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
saveStreamToDisk: (