Handle rest of watch related functions

This commit is contained in:
Manav Rathi 2024-03-25 14:03:20 +05:30
parent 186e76c62b
commit 4261624da5
No known key found for this signature in database
8 changed files with 165 additions and 122 deletions

View file

@ -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";

View file

@ -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),
);
};

View file

@ -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,

View file

@ -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);
}

View file

@ -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);

View file

@ -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,
);

View file

@ -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) =>

View file

@ -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>;