π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move add/remove watch functions to preload
Manav Rathi186e76c62b72
diff --git a/desktop/src/api/watch.ts b/desktop/src/api/watch.tsindex 9b57168a86..2eed336c19 100644--- a/desktop/src/api/watch.ts+++ b/desktop/src/api/watch.ts@@ -1,23 +1,22 @@-import { ipcRenderer } from "electron";+import type { FSWatcher } from "chokidar";import ElectronLog from "electron-log";import { getWatchMappings, setWatchMappings } from "../services/watch";-import { ElectronFile, WatchMapping } from "../types";+import { WatchMapping } from "../types";import { isMappingPresent } from "../utils/watch";-export async function addWatchMapping(+export const addWatchMapping = async (+ watcher: FSWatcher,rootFolderName: string,folderPath: string,uploadStrategy: number,-) {+) => {ElectronLog.log(`Adding watch mapping: ${folderPath}`);const watchMappings = getWatchMappings();if (isMappingPresent(watchMappings, folderPath)) {throw new Error(`Watch mapping already exists`);}- await ipcRenderer.invoke("add-watcher", {- dir: folderPath,- });+ watcher.add(folderPath);watchMappings.push({rootFolderName,@@ -28,9 +27,12 @@ export async function addWatchMapping(});setWatchMappings(watchMappings);-}+};-export async function removeWatchMapping(folderPath: string) {+export const removeWatchMapping = async (+ watcher: FSWatcher,+ folderPath: string,+) => {let watchMappings = getWatchMappings();const watchMapping = watchMappings.find((mapping) => mapping.folderPath === folderPath,@@ -40,16 +42,14 @@ export async function removeWatchMapping(folderPath: string) {throw new Error(`Watch mapping does not exist`);}- await ipcRenderer.invoke("remove-watcher", {- dir: watchMapping.folderPath,- });+ watcher.unwatch(watchMapping.folderPath);watchMappings = watchMappings.filter((mapping) => mapping.folderPath !== watchMapping.folderPath,);setWatchMappings(watchMappings);-}+};export function updateWatchMappingSyncedFiles(folderPath: string,@@ -85,5 +85,4 @@ export function updateWatchMappingIgnoredFiles(setWatchMappings(watchMappings);}-export { getWatchMappings } from "../services/watch";diff --git a/desktop/src/main.ts b/desktop/src/main.tsindex ed840966ef..cb0d3f6e5b 100644--- a/desktop/src/main.ts+++ b/desktop/src/main.ts@@ -15,12 +15,11 @@ import { existsSync } from "node:fs";import * as fs from "node:fs/promises";import * as path from "node:path";import { isDev } from "./main/general";+import { attachFSWatchIPCHandlers, attachIPCHandlers } from "./main/ipc";import { logErrorSentry, setupLogging } from "./main/log";import { initWatcher } from "./services/chokidar";import { addAllowOriginHeader } from "./utils/cors";import { createWindow } from "./utils/createWindow";--import setupIpcComs from "./utils/ipcComms";import {handleDockIconHideOnAutoLaunch,handleDownloads,@@ -167,11 +166,12 @@ const main = () => {app.on("ready", async () => {logSystemInfo();mainWindow = await createWindow();- const tray = setupTrayItem(mainWindow);const watcher = initWatcher(mainWindow);+ setupTrayItem(mainWindow);setupMacWindowOnDockIconClick();setupMainMenu(mainWindow);- setupIpcComs(tray, mainWindow, watcher);+ attachIPCHandlers();+ attachFSWatchIPCHandlers(watcher);await handleUpdates(mainWindow);handleDownloads(mainWindow);handleExternalLinks(mainWindow);diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.tsindex c60219f4db..f2a421cd5e 100644--- a/desktop/src/main/ipc.ts+++ b/desktop/src/main/ipc.ts@@ -6,9 +6,11 @@* context of the main process, and can import other files from `src/`.*/+import type { FSWatcher } from "chokidar";import { ipcMain } from "electron/main";import { clearElectronStore } from "../api/electronStore";import { getEncryptionKey, setEncryptionKey } from "../api/safeStorage";+import { addWatchMapping, removeWatchMapping } from "../api/watch";import {appVersion,muteUpdateNotification,@@ -35,6 +37,10 @@ import { checkExistsAndCreateDir, fsExists } from "./fs";import { openDirectory, openLogDirectory } from "./general";import { logToDisk } from "./log";+/**+ * Listen for IPC events sent/invoked by the renderer process, and route them to+ * their correct handlers.+ */export const attachIPCHandlers = () => {// Notes://@@ -126,3 +132,32 @@ export const attachIPCHandlers = () => {checkExistsAndCreateDir(dirPath),);};++/**+ * Sibling of {@link attachIPCHandlers} that attaches handlers specific to the+ * watch folder functionality.+ *+ * It gets passed a {@link FSWatcher} instance which it can then forward to the+ * actual handlers.+ */+export const attachFSWatchIPCHandlers = (watcher: FSWatcher) => {+ ipcMain.handle(+ "addWatchMapping",+ (+ _,+ collectionName: string,+ folderPath: string,+ uploadStrategy: number,+ ) =>+ addWatchMapping(+ watcher,+ collectionName,+ folderPath,+ uploadStrategy,+ ),+ );++ ipcMain.handle("removeWatchMapping", (_, folderPath: string) =>+ removeWatchMapping(watcher, folderPath),+ );+};diff --git a/desktop/src/preload.ts b/desktop/src/preload.tsindex 0db9f463bb..d4e9db323a 100644--- a/desktop/src/preload.ts+++ b/desktop/src/preload.ts@@ -39,9 +39,7 @@ import {setToUploadFiles,} from "./api/upload";import {- addWatchMapping,getWatchMappings,- removeWatchMapping,updateWatchMappingIgnoredFiles,updateWatchMappingSyncedFiles,} from "./api/watch";@@ -227,6 +225,21 @@ const registerWatcherFunctions = ();};+const addWatchMapping = (+ collectionName: string,+ folderPath: string,+ uploadStrategy: number,+): Promise<void> =>+ ipcRenderer.invoke(+ "addWatchMapping",+ collectionName,+ folderPath,+ uploadStrategy,+ );++const removeWatchMapping = (folderPath: string): Promise<void> =>+ ipcRenderer.invoke("removeWatchMapping", folderPath);+// - FIXME below this/* preload: duplicated logError */@@ -453,6 +466,8 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {// - WatchregisterWatcherFunctions,+ addWatchMapping,+ removeWatchMapping,// - FSfs: {@@ -474,8 +489,6 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {setToUploadCollection,getDirFiles,getWatchMappings,- addWatchMapping,- removeWatchMapping,isFolder,updateWatchMappingSyncedFiles,diff --git a/desktop/src/utils/ipcComms.ts b/desktop/src/utils/ipcComms.tsdeleted file mode 100644index bd0c5da447..0000000000--- a/desktop/src/utils/ipcComms.ts+++ /dev/null@@ -1,19 +0,0 @@-import chokidar from "chokidar";-import { BrowserWindow, ipcMain, Tray } from "electron";-import { attachIPCHandlers } from "../main/ipc";--export default function setupIpcComs(- tray: Tray,- mainWindow: BrowserWindow,- watcher: chokidar.FSWatcher,-): void {- attachIPCHandlers();-- ipcMain.handle("add-watcher", async (_, args: { dir: string }) => {- watcher.add(args.dir);- });-- ipcMain.handle("remove-watcher", async (_, args: { dir: string }) => {- watcher.unwatch(args.dir);- });-}diff --git a/desktop/src/utils/main.ts b/desktop/src/utils/main.tsindex 3a73cca00b..2152478253 100644--- a/desktop/src/utils/main.ts+++ b/desktop/src/utils/main.ts@@ -5,10 +5,10 @@ import os from "os";import path from "path";import util from "util";import { rendererURL } from "../main";+import { isDev } from "../main/general";import { setupAutoUpdater } from "../services/appUpdater";import autoLauncher from "../services/autoLauncher";import { getHideDockIconPreference } from "../services/userPreference";-import { isDev } from "../main/general";import { isPlatform } from "./common/platform";import { buildContextMenu, buildMenuBar } from "./menu";const execAsync = util.promisify(require("child_process").exec);@@ -19,7 +19,8 @@ export async function handleUpdates(mainWindow: BrowserWindow) {setupAutoUpdater(mainWindow);}}-export function setupTrayItem(mainWindow: BrowserWindow) {++export const setupTrayItem = (mainWindow: BrowserWindow) => {const iconName = isPlatform("mac")? "taskbar-icon-Template.png": "taskbar-icon.png";@@ -31,8 +32,7 @@ export function setupTrayItem(mainWindow: BrowserWindow) {const tray = new Tray(trayIcon);tray.setToolTip("ente");tray.setContextMenu(buildContextMenu(mainWindow));- return tray;-}+};export function handleDownloads(mainWindow: BrowserWindow) {mainWindow.webContents.session.on("will-download", (_, item) => {diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.tsindex 8882123ecc..fad6231930 100644--- a/web/packages/shared/electron/types.ts+++ b/web/packages/shared/electron/types.ts@@ -153,6 +153,14 @@ export interface ElectronAPIsType {removeFolder: (folderPath: string) => Promise<void>,) => void;+ addWatchMapping: (+ collectionName: string,+ folderPath: string,+ uploadStrategy: number,+ ) => Promise<void>;++ removeWatchMapping: (folderPath: string) => Promise<void>;+// - FS legacycheckExistsAndCreateDir: (dirPath: string) => Promise<void>;@@ -185,12 +193,6 @@ export interface ElectronAPIsType {folderPath: string,files: WatchMapping["ignoredFiles"],) => void;- addWatchMapping: (- collectionName: string,- folderPath: string,- uploadStrategy: number,- ) => Promise<void>;- removeWatchMapping: (folderPath: string) => Promise<void>;isFolder: (dirPath: string) => Promise<boolean>;moveFile: (oldPath: string, newPath: string) => Promise<void>;deleteFolder: (path: string) => Promise<void>;