diff --git a/desktop/src/main/fs.ts b/desktop/src/main/fs.ts index 36de710c3..2428d3a80 100644 --- a/desktop/src/main/fs.ts +++ b/desktop/src/main/fs.ts @@ -22,10 +22,8 @@ export const fsReadTextFile = async (filePath: string) => export const fsWriteFile = (path: string, contents: string) => fs.writeFile(path, contents); -/* TODO: Audit below this */ - -export const isFolder = async (dirPath: string) => { +export const fsIsDir = async (dirPath: string) => { if (!existsSync(dirPath)) return false; - const stats = await fs.stat(dirPath); - return stats.isDirectory(); + const stat = await fs.stat(dirPath); + return stat.isDirectory(); }; diff --git a/desktop/src/main/ipc.ts b/desktop/src/main/ipc.ts index 3d4e15c99..e307ad167 100644 --- a/desktop/src/main/ipc.ts +++ b/desktop/src/main/ipc.ts @@ -19,13 +19,13 @@ import { } from "./dialogs"; import { fsExists, + fsIsDir, fsMkdirIfNeeded, fsReadTextFile, fsRename, fsRm, fsRmdir, fsWriteFile, - isFolder, } from "./fs"; import { logToDisk } from "./log"; import { @@ -55,7 +55,6 @@ import { } from "./services/upload"; import { addWatchMapping, - folderWatchesAndFilesTherein, getWatchMappings, removeWatchMapping, updateWatchMappingIgnoredFiles, @@ -133,6 +132,8 @@ export const attachIPCHandlers = () => { fsWriteFile(path, contents), ); + ipcMain.handle("fsIsDir", (_, dirPath: string) => fsIsDir(dirPath)); + // - Conversion ipcMain.handle("convertToJPEG", (_, fileData, filename) => @@ -184,10 +185,6 @@ export const attachIPCHandlers = () => { ipcMain.handle("showUploadZipDialog", () => showUploadZipDialog()); - // - FS Legacy - - ipcMain.handle("isFolder", (_, dirPath: string) => isFolder(dirPath)); - // - Upload ipcMain.handle("getPendingUploads", () => getPendingUploads()); @@ -239,10 +236,6 @@ export const attachFSWatchIPCHandlers = (watcher: FSWatcher) => { removeWatchMapping(watcher, folderPath), ); - ipcMain.handle("folderWatchesAndFilesTherein", () => - folderWatchesAndFilesTherein(watcher), - ); - ipcMain.handle("getWatchMappings", () => getWatchMappings()); ipcMain.handle( diff --git a/desktop/src/main/services/watch.ts b/desktop/src/main/services/watch.ts index 125d65bf9..1d466d415 100644 --- a/desktop/src/main/services/watch.ts +++ b/desktop/src/main/services/watch.ts @@ -1,13 +1,7 @@ import type { FSWatcher } from "chokidar"; import ElectronLog from "electron-log"; -import { - FolderWatch, - WatchStoreType, - type ElectronFile, -} from "../../types/ipc"; -import { isFolder } from "../fs"; +import { FolderWatch, WatchStoreType } from "../../types/ipc"; import { watchStore } from "../stores/watch.store"; -import { getDirFiles } from "./fs"; export const addWatchMapping = async ( watcher: FSWatcher, @@ -105,26 +99,3 @@ export function getWatchMappings() { function setWatchMappings(watchMappings: WatchStoreType["mappings"]) { watchStore.set("mappings", watchMappings); } - -export const folderWatchesAndFilesTherein = async ( - watcher: FSWatcher, -): Promise<[watch: FolderWatch, files: ElectronFile[]][]> => { - const mappings = await getWatchMappings(); - - const activeMappings = []; - for (const mapping of mappings) { - const mappingExists = await isFolder(mapping.folderPath); - if (!mappingExists) { - await removeWatchMapping(watcher, mapping.folderPath); - } else { - activeMappings.push(mapping); - } - } - - return Promise.all( - activeMappings.map(async (mapping) => [ - mapping, - await getDirFiles(mapping.folderPath), - ]), - ); -}; diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index a3deb1fae..9341f02f2 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -118,6 +118,9 @@ const fsReadTextFile = (path: string): Promise => const fsWriteFile = (path: string, contents: string): Promise => ipcRenderer.invoke("fsWriteFile", path, contents); +const fsIsDir = (dirPath: string): Promise => + ipcRenderer.invoke("fsIsDir", dirPath); + // - AUDIT below this // - Conversion @@ -220,10 +223,6 @@ const addWatchMapping = ( const removeWatchMapping = (folderPath: string): Promise => ipcRenderer.invoke("removeWatchMapping", folderPath); -const folderWatchesAndFilesTherein = (): Promise< - [watch: FolderWatch, files: ElectronFile[]][] -> => ipcRenderer.invoke("folderWatchesAndFilesTherein"); - const getWatchMappings = (): Promise => ipcRenderer.invoke("getWatchMappings"); @@ -239,11 +238,6 @@ const updateWatchMappingIgnoredFiles = ( ): Promise => ipcRenderer.invoke("updateWatchMappingIgnoredFiles", folderPath, files); -// - FS Legacy - -const isFolder = (dirPath: string): Promise => - ipcRenderer.invoke("isFolder", dirPath); - // - Upload const getPendingUploads = (): Promise<{ @@ -327,6 +321,7 @@ contextBridge.exposeInMainWorld("electron", { rm: fsRm, readTextFile: fsReadTextFile, writeFile: fsWriteFile, + isDir: fsIsDir, }, // - Conversion @@ -347,7 +342,6 @@ contextBridge.exposeInMainWorld("electron", { showUploadZipDialog, // - Watch - folderWatchesAndFilesTherein, registerWatcherFunctions, addWatchMapping, removeWatchMapping, @@ -355,10 +349,6 @@ contextBridge.exposeInMainWorld("electron", { updateWatchMappingSyncedFiles, updateWatchMappingIgnoredFiles, - // - FS legacy - // TODO: Move these into fs + document + rename if needed - isFolder, - // - Upload getPendingUploads, diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts index e6e5004ce..bb11fa67e 100644 --- a/web/apps/photos/src/services/watch.ts +++ b/web/apps/photos/src/services/watch.ts @@ -538,8 +538,7 @@ class WatchFolderService { async isFolder(folderPath: string) { try { - const isFolder = await ensureElectron().isFolder(folderPath); - return isFolder; + return await ensureElectron().fs.isDir(folderPath); } catch (e) { log.error("error while checking if folder exists", e); } @@ -675,8 +674,8 @@ const syncWithDisk = async ( const nonExistentFolderPaths: string[] = []; for (const mapping of mappings) { - const active = await electron.isFolder(mapping.folderPath); - if (!active) nonExistentFolderPaths.push(mapping.folderPath); + const valid = await electron.fs.isDir(mapping.folderPath); + if (!valid) nonExistentFolderPaths.push(mapping.folderPath); else activeMappings.push(mapping); } diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts index 800befa7f..348f815bb 100644 --- a/web/packages/next/types/ipc.ts +++ b/web/packages/next/types/ipc.ts @@ -199,6 +199,12 @@ export interface Electron { * @param contents The string contents to write. */ writeFile: (path: string, contents: string) => Promise; + + /** + * Return true if there is an item at {@link dirPath}, and it is as + * directory. + */ + isDir: (dirPath: string) => Promise; }; /* @@ -321,9 +327,6 @@ export interface Electron { files: FolderWatch["ignoredFiles"], ) => Promise; - // - FS legacy - isFolder: (dirPath: string) => Promise; - // - Upload getPendingUploads: () => Promise<{