Skip to content

armgit / ente

Public Imported from GitHub
0

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

Handle rest of watch related functions

Manav Rathi4261624da596
diff --git a/desktop/src/api/watch.ts b/desktop/src/api/watch.ts
deleted file mode 100644
index 2eed336c19..0000000000
--- a/desktop/src/api/watch.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import type { FSWatcher } from "chokidar";
-import ElectronLog from "electron-log";
-import { getWatchMappings, setWatchMappings } from "../services/watch";
-import { WatchMapping } from "../types";
-import { isMappingPresent } from "../utils/watch";
-
-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`);
- }
-
- watcher.add(folderPath);
-
- watchMappings.push({
- rootFolderName,
- uploadStrategy,
- folderPath,
- syncedFiles: [],
- ignoredFiles: [],
- });
-
- setWatchMappings(watchMappings);
-};
-
-export const removeWatchMapping = async (
- watcher: FSWatcher,
- folderPath: string,
-) => {
- let watchMappings = getWatchMappings();
- const watchMapping = watchMappings.find(
- (mapping) => mapping.folderPath === folderPath,
- );
-
- if (!watchMapping) {
- throw new Error(`Watch mapping does not exist`);
- }
-
- watcher.unwatch(watchMapping.folderPath);
-
- watchMappings = watchMappings.filter(
- (mapping) => mapping.folderPath !== watchMapping.folderPath,
- );
-
- setWatchMappings(watchMappings);
-};
-
-export function updateWatchMappingSyncedFiles(
- folderPath: string,
- files: WatchMapping["syncedFiles"],
-): void {
- const watchMappings = getWatchMappings();
- const watchMapping = watchMappings.find(
- (mapping) => mapping.folderPath === folderPath,
- );
-
- if (!watchMapping) {
- throw Error(`Watch mapping not found`);
- }
-
- watchMapping.syncedFiles = files;
- setWatchMappings(watchMappings);
-}
-
-export function updateWatchMappingIgnoredFiles(
- folderPath: string,
- files: WatchMapping["ignoredFiles"],
-): void {
- const watchMappings = getWatchMappings();
- const watchMapping = watchMappings.find(
- (mapping) => mapping.folderPath === folderPath,
- );
-
- if (!watchMapping) {
- throw Error(`Watch mapping not found`);
- }
-
- watchMapping.ignoredFiles = files;
- setWatchMappings(watchMappings);
-}
-
-export { getWatchMappings } from "../services/watch";
diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts
index f2a421cd5e..ccee028ba0 100644
--- a/desktop/src/main/ipc.ts
+++ b/desktop/src/main/ipc.ts
@@ -10,7 +10,6 @@ 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,
@@ -26,7 +25,14 @@ import {
convertToJPEG,
generateImageThumbnail,
} from "../services/imageProcessor";
-import type { ElectronFile, Model } from "../types";
+import {
+ addWatchMapping,
+ getWatchMappings,
+ removeWatchMapping,
+ updateWatchMappingIgnoredFiles,
+ updateWatchMappingSyncedFiles,
+} from "../services/watch";
+import type { ElectronFile, Model, WatchMapping } from "../types";
import {
selectDirectory,
showUploadDirsDialog,
@@ -160,4 +166,18 @@ export const attachFSWatchIPCHandlers = (watcher: FSWatcher) => {
ipcMain.handle("removeWatchMapping", (_, folderPath: string) =>
removeWatchMapping(watcher, folderPath),
);
+
+ ipcMain.handle("getWatchMappings", (_) => getWatchMappings());
+
+ ipcMain.handle(
+ "updateWatchMappingSyncedFiles",
+ (_, folderPath: string, files: WatchMapping["syncedFiles"]) =>
+ updateWatchMappingSyncedFiles(folderPath, files),
+ );
+
+ ipcMain.handle(
+ "updateWatchMappingIgnoredFiles",
+ (_, folderPath: string, files: WatchMapping["ignoredFiles"]) =>
+ updateWatchMappingIgnoredFiles(folderPath, files),
+ );
};
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index d4e9db323a..7c539a276f 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -38,11 +38,6 @@ import {
setToUploadCollection,
setToUploadFiles,
} from "./api/upload";
-import {
- getWatchMappings,
- updateWatchMappingIgnoredFiles,
- updateWatchMappingSyncedFiles,
-} from "./api/watch";
import { logErrorSentry, setupLogging } from "./main/log";
import type { ElectronFile } from "./types";
@@ -240,6 +235,37 @@ const addWatchMapping = (
const removeWatchMapping = (folderPath: string): Promise<void> =>
ipcRenderer.invoke("removeWatchMapping", folderPath);
+/* preload: duplicated WatchMappingSyncedFile */
+interface WatchMappingSyncedFile {
+ path: string;
+ uploadedFileID: number;
+ collectionID: number;
+}
+
+/* preload: duplicated WatchMapping */
+export interface WatchMapping {
+ rootFolderName: string;
+ uploadStrategy: number;
+ folderPath: string;
+ syncedFiles: WatchMappingSyncedFile[];
+ ignoredFiles: string[];
+}
+
+const getWatchMappings = (): Promise<WatchMapping[]> =>
+ ipcRenderer.invoke("getWatchMappings");
+
+const updateWatchMappingSyncedFiles = (
+ folderPath: string,
+ files: WatchMapping["syncedFiles"],
+): Promise<void> =>
+ ipcRenderer.invoke("updateWatchMappingSyncedFiles", folderPath, files);
+
+const updateWatchMappingIgnoredFiles = (
+ folderPath: string,
+ files: WatchMapping["ignoredFiles"],
+): Promise<void> =>
+ ipcRenderer.invoke("updateWatchMappingIgnoredFiles", folderPath, files);
+
// - FIXME below this
/* preload: duplicated logError */
@@ -468,6 +494,9 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
registerWatcherFunctions,
addWatchMapping,
removeWatchMapping,
+ getWatchMappings,
+ updateWatchMappingSyncedFiles,
+ updateWatchMappingIgnoredFiles,
// - FS
fs: {
@@ -488,11 +517,8 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
getElectronFilesFromGoogleZip,
setToUploadCollection,
getDirFiles,
- getWatchMappings,
isFolder,
- updateWatchMappingSyncedFiles,
- updateWatchMappingIgnoredFiles,
moveFile,
deleteFolder,
rename,
diff --git a/desktop/src/services/watch.ts b/desktop/src/services/watch.ts
index 8b77469648..ab29dbc5cc 100644
--- a/desktop/src/services/watch.ts
+++ b/desktop/src/services/watch.ts
@@ -1,11 +1,95 @@
+import type { FSWatcher } from "chokidar";
+import ElectronLog from "electron-log";
import { watchStore } from "../stores/watch.store";
-import { WatchStoreType } from "../types";
+import { WatchMapping, WatchStoreType } from "../types";
+import { isMappingPresent } from "../utils/watch";
+
+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`);
+ }
+
+ watcher.add(folderPath);
+
+ watchMappings.push({
+ rootFolderName,
+ uploadStrategy,
+ folderPath,
+ syncedFiles: [],
+ ignoredFiles: [],
+ });
+
+ setWatchMappings(watchMappings);
+};
+
+export const removeWatchMapping = async (
+ watcher: FSWatcher,
+ folderPath: string,
+) => {
+ let watchMappings = getWatchMappings();
+ const watchMapping = watchMappings.find(
+ (mapping) => mapping.folderPath === folderPath,
+ );
+
+ if (!watchMapping) {
+ throw new Error(`Watch mapping does not exist`);
+ }
+
+ watcher.unwatch(watchMapping.folderPath);
+
+ watchMappings = watchMappings.filter(
+ (mapping) => mapping.folderPath !== watchMapping.folderPath,
+ );
+
+ setWatchMappings(watchMappings);
+};
+
+export function updateWatchMappingSyncedFiles(
+ folderPath: string,
+ files: WatchMapping["syncedFiles"],
+): void {
+ const watchMappings = getWatchMappings();
+ const watchMapping = watchMappings.find(
+ (mapping) => mapping.folderPath === folderPath,
+ );
+
+ if (!watchMapping) {
+ throw Error(`Watch mapping not found`);
+ }
+
+ watchMapping.syncedFiles = files;
+ setWatchMappings(watchMappings);
+}
+
+export function updateWatchMappingIgnoredFiles(
+ folderPath: string,
+ files: WatchMapping["ignoredFiles"],
+): void {
+ const watchMappings = getWatchMappings();
+ const watchMapping = watchMappings.find(
+ (mapping) => mapping.folderPath === folderPath,
+ );
+
+ if (!watchMapping) {
+ throw Error(`Watch mapping not found`);
+ }
+
+ watchMapping.ignoredFiles = files;
+ setWatchMappings(watchMappings);
+}
export function getWatchMappings() {
const mappings = watchStore.get("mappings") ?? [];
return mappings;
}
-export function setWatchMappings(watchMappings: WatchStoreType["mappings"]) {
+function setWatchMappings(watchMappings: WatchStoreType["mappings"]) {
watchStore.set("mappings", watchMappings);
}
diff --git a/web/apps/photos/src/components/WatchFolder/index.tsx b/web/apps/photos/src/components/WatchFolder/index.tsx
index 71c8b30ff3..ee002f3dcd 100644
--- a/web/apps/photos/src/components/WatchFolder/index.tsx
+++ b/web/apps/photos/src/components/WatchFolder/index.tsx
@@ -28,7 +28,7 @@ export default function WatchFolder({ open, onClose }: Iprops) {
if (!isElectron()) {
return;
}
- setMappings(watchFolderService.getWatchMappings());
+ watchFolderService.getWatchMappings().then((m) => setMappings(m));
}, []);
useEffect(() => {
@@ -87,12 +87,12 @@ export default function WatchFolder({ open, onClose }: Iprops) {
uploadStrategy,
);
setInputFolderPath("");
- setMappings(watchFolderService.getWatchMappings());
+ setMappings(await watchFolderService.getWatchMappings());
};
const handleRemoveWatchMapping = async (mapping: WatchMapping) => {
await watchFolderService.removeWatchMapping(mapping.folderPath);
- setMappings(watchFolderService.getWatchMappings());
+ setMappings(await watchFolderService.getWatchMappings());
};
const closeChoiceModal = () => setChoiceModalOpen(false);
diff --git a/web/apps/photos/src/services/watchFolder/watchFolderEventHandlers.ts b/web/apps/photos/src/services/watchFolder/watchFolderEventHandlers.ts
index d1d8faa725..8c15559dde 100644
--- a/web/apps/photos/src/services/watchFolder/watchFolderEventHandlers.ts
+++ b/web/apps/photos/src/services/watchFolder/watchFolderEventHandlers.ts
@@ -58,7 +58,7 @@ export async function diskFileRemovedCallback(filePath: string) {
export async function diskFolderRemovedCallback(folderPath: string) {
try {
- const mappings = watchFolderService.getWatchMappings();
+ const mappings = await watchFolderService.getWatchMappings();
const mapping = mappings.find(
(mapping) => mapping.folderPath === folderPath,
);
diff --git a/web/apps/photos/src/services/watchFolder/watchFolderService.ts b/web/apps/photos/src/services/watchFolder/watchFolderService.ts
index ea298c7f50..8ebc38afda 100644
--- a/web/apps/photos/src/services/watchFolder/watchFolderService.ts
+++ b/web/apps/photos/src/services/watchFolder/watchFolderService.ts
@@ -72,7 +72,7 @@ class watchFolderService {
async getAndSyncDiffOfFiles() {
try {
- let mappings = this.getWatchMappings();
+ let mappings = await this.getWatchMappings();
if (!mappings?.length) {
return;
@@ -205,14 +205,13 @@ class watchFolderService {
}
}
- getWatchMappings(): WatchMapping[] {
+ async getWatchMappings(): Promise<WatchMapping[]> {
try {
- return ElectronAPIs.getWatchMappings() ?? [];
+ return (await ElectronAPIs.getWatchMappings()) ?? [];
} catch (e) {
logError(e, "error while getting watch mappings");
return [];
}
- return [];
}
private setIsEventRunning(isEventRunning: boolean) {
@@ -234,7 +233,7 @@ class watchFolderService {
addLogLine(
`running event type:${event.type} collectionName:${event.collectionName} folderPath:${event.folderPath} , fileCount:${event.files?.length} pathsCount: ${event.paths?.length}`,
);
- const mappings = this.getWatchMappings();
+ const mappings = await this.getWatchMappings();
const mapping = mappings.find(
(mapping) => mapping.folderPath === event.folderPath,
);
@@ -380,7 +379,7 @@ class watchFolderService {
...this.currentlySyncedMapping.syncedFiles,
...syncedFiles,
];
- ElectronAPIs.updateWatchMappingSyncedFiles(
+ await ElectronAPIs.updateWatchMappingSyncedFiles(
this.currentlySyncedMapping.folderPath,
this.currentlySyncedMapping.syncedFiles,
);
@@ -390,7 +389,7 @@ class watchFolderService {
...this.currentlySyncedMapping.ignoredFiles,
...ignoredFiles,
];
- ElectronAPIs.updateWatchMappingIgnoredFiles(
+ await ElectronAPIs.updateWatchMappingIgnoredFiles(
this.currentlySyncedMapping.folderPath,
this.currentlySyncedMapping.ignoredFiles,
);
@@ -505,7 +504,7 @@ class watchFolderService {
this.currentlySyncedMapping.syncedFiles.filter(
(file) => !filePathsToRemove.has(file.path),
);
- ElectronAPIs.updateWatchMappingSyncedFiles(
+ await ElectronAPIs.updateWatchMappingSyncedFiles(
this.currentlySyncedMapping.folderPath,
this.currentlySyncedMapping.syncedFiles,
);
@@ -561,7 +560,7 @@ class watchFolderService {
async getCollectionNameAndFolderPath(filePath: string) {
try {
- const mappings = this.getWatchMappings();
+ const mappings = await this.getWatchMappings();
const mapping = mappings.find(
(mapping) =>
diff --git a/web/packages/shared/electron/types.ts b/web/packages/shared/electron/types.ts
index fad6231930..9902481b1d 100644
--- a/web/packages/shared/electron/types.ts
+++ b/web/packages/shared/electron/types.ts
@@ -161,6 +161,17 @@ export interface ElectronAPIsType {
removeWatchMapping: (folderPath: string) => Promise<void>;
+ getWatchMappings: () => Promise<WatchMapping[]>;
+
+ updateWatchMappingSyncedFiles: (
+ folderPath: string,
+ files: WatchMapping["syncedFiles"],
+ ) => Promise<void>;
+
+ updateWatchMappingIgnoredFiles: (
+ folderPath: string,
+ files: WatchMapping["ignoredFiles"],
+ ) => Promise<void>;
// - FS legacy
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
@@ -184,15 +195,6 @@ export interface ElectronAPIsType {
) => Promise<ElectronFile[]>;
setToUploadCollection: (collectionName: string) => void;
getDirFiles: (dirPath: string) => Promise<ElectronFile[]>;
- getWatchMappings: () => WatchMapping[];
- updateWatchMappingSyncedFiles: (
- folderPath: string,
- files: WatchMapping["syncedFiles"],
- ) => void;
- updateWatchMappingIgnoredFiles: (
- folderPath: string,
- files: WatchMapping["ignoredFiles"],
- ) => void;
isFolder: (dirPath: string) => Promise<boolean>;
moveFile: (oldPath: string, newPath: string) => Promise<void>;
deleteFolder: (path: string) => Promise<void>;