Skip to content

armgit / ente

Public Imported from GitHub
0

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

preload / watch - part 1

Manav Rathi96ea53face77
diff --git a/desktop/src/api/watch.ts b/desktop/src/api/watch.ts
index 1b7a4ac3cf..9b57168a86 100644
--- a/desktop/src/api/watch.ts
+++ b/desktop/src/api/watch.ts
@@ -1,7 +1,5 @@
import { ipcRenderer } from "electron";
import ElectronLog from "electron-log";
-import path from "path";
-import { getElectronFile } from "../services/fs";
import { getWatchMappings, setWatchMappings } from "../services/watch";
import { ElectronFile, WatchMapping } from "../types";
import { isMappingPresent } from "../utils/watch";
@@ -87,28 +85,5 @@ export function updateWatchMappingIgnoredFiles(
setWatchMappings(watchMappings);
}
-export function registerWatcherFunctions(
- addFile: (file: ElectronFile) => Promise<void>,
- removeFile: (path: string) => Promise<void>,
- removeFolder: (folderPath: string) => Promise<void>,
-) {
- ipcRenderer.removeAllListeners("watch-add");
- ipcRenderer.removeAllListeners("watch-change");
- ipcRenderer.removeAllListeners("watch-unlink-dir");
- ipcRenderer.on("watch-add", async (_, filePath: string) => {
- filePath = filePath.split(path.sep).join(path.posix.sep);
-
- await addFile(await getElectronFile(filePath));
- });
- ipcRenderer.on("watch-unlink", async (_, filePath: string) => {
- filePath = filePath.split(path.sep).join(path.posix.sep);
-
- await removeFile(filePath);
- });
- ipcRenderer.on("watch-unlink-dir", async (_, folderPath: string) => {
- folderPath = folderPath.split(path.sep).join(path.posix.sep);
- await removeFolder(folderPath);
- });
-}
export { getWatchMappings } from "../services/watch";
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index eb8a59fdc7..0db9f463bb 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -31,7 +31,6 @@ import { createWriteStream, existsSync } from "node:fs";
import * as fs from "node:fs/promises";
import { Readable } from "node:stream";
import path from "path";
-import type { ElectronFile } from "./types";
import { getDirFiles } from "./api/fs";
import {
getElectronFilesFromGoogleZip,
@@ -42,12 +41,12 @@ import {
import {
addWatchMapping,
getWatchMappings,
- registerWatcherFunctions,
removeWatchMapping,
updateWatchMappingIgnoredFiles,
updateWatchMappingSyncedFiles,
} from "./api/watch";
import { logErrorSentry, setupLogging } from "./main/log";
+import type { ElectronFile } from "./types";
setupLogging();
@@ -209,6 +208,25 @@ const showUploadZipDialog = (): Promise<{
files: ElectronFile[];
}> => ipcRenderer.invoke("showUploadZipDialog");
+// - Watch
+
+const registerWatcherFunctions = (
+ addFile: (file: ElectronFile) => Promise<void>,
+ removeFile: (path: string) => Promise<void>,
+ removeFolder: (folderPath: string) => Promise<void>,
+) => {
+ ipcRenderer.removeAllListeners("watch-add");
+ ipcRenderer.removeAllListeners("watch-unlink");
+ ipcRenderer.removeAllListeners("watch-unlink-dir");
+ ipcRenderer.on("watch-add", (_, file: ElectronFile) => addFile(file));
+ ipcRenderer.on("watch-unlink", (_, filePath: string) =>
+ removeFile(filePath),
+ );
+ ipcRenderer.on("watch-unlink-dir", (_, folderPath: string) =>
+ removeFolder(folderPath),
+ );
+};
+
// - FIXME below this
/* preload: duplicated logError */
@@ -433,6 +451,9 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
showUploadDirsDialog,
showUploadZipDialog,
+ // - Watch
+ registerWatcherFunctions,
+
// - FS
fs: {
exists: fsExists,
@@ -455,7 +476,7 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
getWatchMappings,
addWatchMapping,
removeWatchMapping,
- registerWatcherFunctions,
+
isFolder,
updateWatchMappingSyncedFiles,
updateWatchMappingIgnoredFiles,
diff --git a/desktop/src/services/chokidar.ts b/desktop/src/services/chokidar.ts
index c52ad82670..253c278c4a 100644
--- a/desktop/src/services/chokidar.ts
+++ b/desktop/src/services/chokidar.ts
@@ -1,7 +1,16 @@
import chokidar from "chokidar";
import { BrowserWindow } from "electron";
+import * as path from "path";
import { getWatchMappings } from "../api/watch";
import { logError } from "../main/log";
+import { getElectronFile } from "./fs";
+
+/**
+ * Convert a file system {@link filePath} that uses the local system specific
+ * path separators into a path that uses POSIX file separators.
+ */
+const normalizeToPOSIX = (filePath: string) =>
+ filePath.split(path.sep).join(path.posix.sep);
export function initWatcher(mainWindow: BrowserWindow) {
const mappings = getWatchMappings();
@@ -13,17 +22,20 @@ export function initWatcher(mainWindow: BrowserWindow) {
awaitWriteFinish: true,
});
watcher
- .on("add", (path) => {
- mainWindow.webContents.send("watch-add", path);
- })
- .on("change", (path) => {
- mainWindow.webContents.send("watch-change", path);
+ .on("add", async (path) => {
+ mainWindow.webContents.send(
+ "watch-add",
+ await getElectronFile(normalizeToPOSIX(path)),
+ );
})
.on("unlink", (path) => {
- mainWindow.webContents.send("watch-unlink", path);
+ mainWindow.webContents.send("watch-unlink", normalizeToPOSIX(path));
})
.on("unlinkDir", (path) => {
- mainWindow.webContents.send("watch-unlink-dir", path);
+ mainWindow.webContents.send(
+ "watch-unlink-dir",
+ normalizeToPOSIX(path),
+ );
})
.on("error", (error) => {
logError(error, "error while watching files");
diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.ts
index 6115e23e5f..8882123ecc 100644
--- a/web/packages/shared/electron/types.ts
+++ b/web/packages/shared/electron/types.ts
@@ -145,6 +145,15 @@ export interface ElectronAPIsType {
files: ElectronFile[];
}>;
+ // - Watch
+
+ registerWatcherFunctions: (
+ addFile: (file: ElectronFile) => Promise<void>,
+ removeFile: (path: string) => Promise<void>,
+ removeFolder: (folderPath: string) => Promise<void>,
+ ) => void;
+
+
// - FS legacy
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
@@ -182,11 +191,6 @@ export interface ElectronAPIsType {
uploadStrategy: number,
) => Promise<void>;
removeWatchMapping: (folderPath: string) => Promise<void>;
- registerWatcherFunctions: (
- addFile: (file: ElectronFile) => Promise<void>,
- removeFile: (path: string) => Promise<void>,
- removeFolder: (folderPath: string) => Promise<void>,
- ) => void;
isFolder: (dirPath: string) => Promise<boolean>;
moveFile: (oldPath: string, newPath: string) => Promise<void>;
deleteFolder: (path: string) => Promise<void>;