Skip to content

armgit / ente

Public Imported from GitHub
0

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

Keep watch folders as inaccessible instead of removing them

Anand Baburajanf417be21c115
diff --git a/desktop/src/main/services/watch.ts b/desktop/src/main/services/watch.ts
index 4066a91c89..15f0daccda 100644
--- a/desktop/src/main/services/watch.ts
+++ b/desktop/src/main/services/watch.ts
@@ -59,17 +59,26 @@ const eventData = (platformPath: string): [string, FolderWatch] => {
};
export const watchGet = async (watcher: FSWatcher): Promise<FolderWatch[]> => {
- const valid: FolderWatch[] = [];
- const deletedPaths: string[] = [];
+ const result: FolderWatch[] = [];
+ const watched = watcher.getWatched();
+
for (const watch of folderWatches()) {
- if (await fsIsDir(watch.folderPath)) valid.push(watch);
- else deletedPaths.push(watch.folderPath);
- }
- if (deletedPaths.length) {
- await Promise.all(deletedPaths.map((p) => watchRemove(watcher, p)));
- setFolderWatches(valid);
+ const isAccessible = await fsIsDir(watch.folderPath);
+ result.push({ ...watch, isAccessible });
+
+ // Update the file system watcher based on accessibility.
+ if (isAccessible) {
+ // Add to watcher if not already being watched.
+ if (!watched[watch.folderPath]) {
+ watcher.add(watch.folderPath);
+ }
+ } else {
+ // Stop watching inaccessible folders.
+ watcher.unwatch(watch.folderPath);
+ }
}
- return valid;
+
+ return result;
};
const folderWatches = (): FolderWatch[] => watchStore.get("mappings") ?? [];
@@ -105,10 +114,17 @@ export const watchAdd = async (
watcher.add(folderPath);
- return watches;
+ // Return watches with isAccessible set. The newly added folder is
+ // accessible (we just verified it exists), and we check the others.
+ return await Promise.all(
+ watches.map(async (watch) => ({
+ ...watch,
+ isAccessible: await fsIsDir(watch.folderPath),
+ })),
+ );
};
-export const watchRemove = (watcher: FSWatcher, folderPath: string) => {
+export const watchRemove = async (watcher: FSWatcher, folderPath: string) => {
const watches = folderWatches();
const filtered = watches.filter((watch) => watch.folderPath != folderPath);
if (watches.length == filtered.length)
@@ -117,7 +133,13 @@ export const watchRemove = (watcher: FSWatcher, folderPath: string) => {
);
setFolderWatches(filtered);
watcher.unwatch(folderPath);
- return filtered;
+ // Return watches with isAccessible set.
+ return await Promise.all(
+ filtered.map(async (watch) => ({
+ ...watch,
+ isAccessible: await fsIsDir(watch.folderPath),
+ })),
+ );
};
export const watchUpdateSyncedFiles = (
diff --git a/desktop/src/types/ipc.ts b/desktop/src/types/ipc.ts
index db1d009f8b..caa1645fc4 100644
--- a/desktop/src/types/ipc.ts
+++ b/desktop/src/types/ipc.ts
@@ -17,6 +17,14 @@ export interface FolderWatch {
folderPath: string;
syncedFiles: FolderWatchSyncedFile[];
ignoredFiles: string[];
+ /**
+ * Whether the folder is currently accessible on disk.
+ *
+ * Folders on ejected external drives will have this set to false. This
+ * property is not persisted and is computed each time the watch list is
+ * fetched.
+ */
+ isAccessible?: boolean;
}
export type CollectionMapping = "root" | "parent";
diff --git a/web/apps/photos/src/components/WatchFolder.tsx b/web/apps/photos/src/components/WatchFolder.tsx
index fd7627b30a..afc51fdee9 100644
--- a/web/apps/photos/src/components/WatchFolder.tsx
+++ b/web/apps/photos/src/components/WatchFolder.tsx
@@ -3,6 +3,8 @@ import CheckIcon from "@mui/icons-material/Check";
import DoNotDisturbOutlinedIcon from "@mui/icons-material/DoNotDisturbOutlined";
import FolderCopyOutlinedIcon from "@mui/icons-material/FolderCopyOutlined";
import FolderOpenIcon from "@mui/icons-material/FolderOpen";
+import RefreshIcon from "@mui/icons-material/Refresh";
+import WarningAmberIcon from "@mui/icons-material/WarningAmber";
import {
CircularProgress,
Dialog,
@@ -30,7 +32,7 @@ import { basename, dirname } from "ente-base/file-name";
import type { CollectionMapping, FolderWatch } from "ente-base/types/ipc";
import { CollectionMappingChoice } from "ente-new/photos/components/CollectionMappingChoice";
import { t } from "i18next";
-import React, { useEffect, useState } from "react";
+import React, { useEffect, useRef, useState } from "react";
import watcher from "services/watch";
/**
@@ -52,10 +54,25 @@ export const WatchFolder: React.FC<ModalVisibilityProps> = ({
const { show: showMappingChoice, props: mappingChoiceVisibilityProps } =
useModalVisibility();
+ const refreshWatches = async () => {
+ const ws = await watcher.getWatches();
+ setWatches(ws);
+ };
+
useEffect(() => {
- void watcher.getWatches().then((ws) => setWatches(ws));
+ void refreshWatches();
}, []);
+ // Refresh watches when window gains focus while dialog is open.
+ // This ensures the UI updates when a folder becomes accessible again.
+ useEffect(() => {
+ if (!open) return;
+
+ const handleFocus = () => void refreshWatches();
+ window.addEventListener("focus", handleFocus);
+ return () => window.removeEventListener("focus", handleFocus);
+ }, [open]);
+
useEffect(() => {
const handleWatchFolderDrop = (e: DragEvent) => {
if (!open) return;
@@ -132,7 +149,9 @@ export const WatchFolder: React.FC<ModalVisibilityProps> = ({
</SpacedRow>
<DialogContent sx={{ flex: 1 }}>
<Stack sx={{ gap: 1, p: 1.5, height: "100%" }}>
- <WatchList {...{ watches, removeWatch }} />
+ <WatchList
+ {...{ watches, removeWatch, refreshWatches }}
+ />
<FocusVisibleButton
fullWidth
color="accent"
@@ -152,12 +171,17 @@ export const WatchFolder: React.FC<ModalVisibilityProps> = ({
);
};
-interface WatchList {
+interface WatchListProps {
watches: FolderWatch[] | undefined;
removeWatch: (watch: FolderWatch) => Promise<void>;
+ refreshWatches: () => Promise<void>;
}
-const WatchList: React.FC<WatchList> = ({ watches, removeWatch }) =>
+const WatchList: React.FC<WatchListProps> = ({
+ watches,
+ removeWatch,
+ refreshWatches,
+}) =>
watches?.length ? (
<Stack sx={{ gap: 2, flex: 1, overflowY: "auto", pb: 2, pr: 1 }}>
{watches.map((watch) => (
@@ -165,6 +189,7 @@ const WatchList: React.FC<WatchList> = ({ watches, removeWatch }) =>
key={watch.folderPath}
watch={watch}
removeWatch={removeWatch}
+ onRetry={refreshWatches}
/>
))}
</Stack>
@@ -204,10 +229,25 @@ const Check: React.FC = () => (
interface WatchEntryProps {
watch: FolderWatch;
removeWatch: (watch: FolderWatch) => Promise<void>;
+ onRetry: () => Promise<void>;
}
-const WatchEntry: React.FC<WatchEntryProps> = ({ watch, removeWatch }) => {
+const WatchEntry: React.FC<WatchEntryProps> = ({
+ watch,
+ removeWatch,
+ onRetry,
+}) => {
const { showMiniDialog } = useBaseContext();
+ const [isRetrying, setIsRetrying] = useState(false);
+ const isAccessible = watch.isAccessible !== false;
+ const retryTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+
+ // Cleanup timer on unmount to avoid state update on unmounted component.
+ useEffect(() => {
+ return () => {
+ if (retryTimerRef.current) clearTimeout(retryTimerRef.current);
+ };
+ }, []);
const confirmStopWatching = () =>
showMiniDialog({
@@ -220,10 +260,33 @@ const WatchEntry: React.FC<WatchEntryProps> = ({ watch, removeWatch }) => {
},
});
+ const handleRetry = async () => {
+ if (isRetrying) return; // Prevent multiple rapid clicks.
+ setIsRetrying(true);
+ await onRetry();
+ // Keep spinner visible briefly so user sees feedback.
+ retryTimerRef.current = setTimeout(() => setIsRetrying(false), 1000);
+ };
+
return (
- <SpacedRow sx={{ overflow: "hidden", flexShrink: 0 }}>
+ <SpacedRow
+ sx={{
+ overflow: "hidden",
+ flexShrink: 0,
+ opacity: isAccessible ? 1 : 0.7,
+ }}
+ >
<Stack direction="row" sx={{ overflow: "hidden", gap: 1.5 }}>
- {watch.collectionMapping == "root" ? (
+ {isRetrying ? (
+ <CircularProgress
+ size={24}
+ sx={{ color: "stroke.muted" }}
+ />
+ ) : !isAccessible ? (
+ <Tooltip title={t("folder_not_accessible")}>
+ <WarningAmberIcon color="warning" />
+ </Tooltip>
+ ) : watch.collectionMapping == "root" ? (
<Tooltip title={t("uploaded_to_single_collection")}>
<FolderOpenIcon color="secondary" />
</Tooltip>
@@ -237,7 +300,11 @@ const WatchEntry: React.FC<WatchEntryProps> = ({ watch, removeWatch }) => {
<FolderPath>{watch.folderPath}</FolderPath>
</Stack>
</Stack>
- <EntryOptions {...{ confirmStopWatching }} />
+ <EntryOptions
+ confirmStopWatching={confirmStopWatching}
+ isAccessible={isAccessible}
+ onRetry={handleRetry}
+ />
</SpacedRow>
);
};
@@ -271,13 +338,24 @@ const FolderPath: React.FC<React.PropsWithChildren> = ({ children }) => (
interface EntryOptionsProps {
confirmStopWatching: () => void;
+ isAccessible: boolean;
+ onRetry: () => void;
}
-const EntryOptions: React.FC<EntryOptionsProps> = ({ confirmStopWatching }) => (
+const EntryOptions: React.FC<EntryOptionsProps> = ({
+ confirmStopWatching,
+ isAccessible,
+ onRetry,
+}) => (
<OverflowMenu
ariaID={"watch-mapping-option"}
menuPaperSxProps={{ backgroundColor: "background.paper2" }}
>
+ {!isAccessible && (
+ <OverflowMenuOption onClick={onRetry} startIcon={<RefreshIcon />}>
+ {t("retry_watching")}
+ </OverflowMenuOption>
+ )}
<OverflowMenuOption
color="critical"
onClick={confirmStopWatching}
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index fa4b2bd24c..7fbf7448fc 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -136,6 +136,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { FileWithPath } from "react-dropzone";
import { Trans } from "react-i18next";
import { uploadManager } from "services/upload-manager";
+import watcher from "services/watch";
import {
getSelectedFiles,
performFileOp,
@@ -452,7 +453,10 @@ const Page: React.FC = () => {
);
if (electron) {
- electron.onMainWindowFocus(() => remotePull({ silent: true }));
+ electron.onMainWindowFocus(() => {
+ remotePull({ silent: true });
+ void watcher.checkAccessibility();
+ });
if (await shouldShowWhatsNew(electron)) showWhatsNew();
}
})();
diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts
index 45a66c9bbf..d2566efe66 100644
--- a/web/apps/photos/src/services/watch.ts
+++ b/web/apps/photos/src/services/watch.ts
@@ -134,6 +134,31 @@ class FolderWatcher {
return await ensureElectron().watch.get();
}
+ /**
+ * Check accessibility of all watch folders and trigger sync if any
+ * previously inaccessible folder has become accessible.
+ *
+ * This is meant to be called when the app gains focus, allowing us to
+ * detect when an external drive has been reconnected.
+ */
+ async checkAccessibility(): Promise<void> {
+ try {
+ const watches = await this.getWatches();
+ const accessibleWatches = watches.filter(
+ (w) => w.isAccessible !== false,
+ );
+
+ if (accessibleWatches.length > 0 && !this.isPaused) {
+ log.info(
+ `Folder watch: ${accessibleWatches.length} accessible folder(s), triggering sync`,
+ );
+ this.triggerSyncWithDisk();
+ }
+ } catch (e) {
+ log.error("Error checking watch folder accessibility", e);
+ }
+ }
+
/**
* Return true if we are currently syncing files that belong to the given
* {@link folderPath}.
@@ -250,6 +275,11 @@ class FolderWatcher {
return;
}
+ if (watch.isAccessible === false) {
+ skip(`folder ${event.folderPath} is inaccessible`);
+ return;
+ }
+
if (event.action == "upload") {
const paths = pathsToUpload(event.filePaths, watch);
if (paths.length == 0) {
@@ -553,6 +583,14 @@ const deduceEvents = async (watches: FolderWatch[]): Promise<WatchEvent[]> => {
const events: WatchEvent[] = [];
for (const watch of watches) {
+ // Skip inaccessible folders (e.g., ejected external drives).
+ if (watch.isAccessible === false) {
+ log.info(
+ `Skipping sync for inaccessible folder ${watch.folderPath}`,
+ );
+ continue;
+ }
+
const folderPath = watch.folderPath;
const filePaths = await electron.fs.findFiles(folderPath);
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index 9675263bf4..1bcaf1acb5 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -560,6 +560,8 @@
"stop_watching_folder_title": "Stop watching folder?",
"stop_watching_folder_message": "Your existing files will not be deleted, but Ente will stop automatically updating the linked Ente album on changes in this folder.",
"yes_stop": "Yes, stop",
+ "folder_not_accessible": "This folder is inaccessible. Syncing will resume when it's accessible. You can stop watching it if it no longer exists.",
+ "retry_watching": "Retry watching",
"change_folder": "Change Folder",
"view_logs": "View logs",
"view_logs_message": "<p>This will show debug logs, which you can email to us to help debug your issue.</p><p>Note that file names will be included to help track issues with specific files.</p>",
diff --git a/web/packages/base/types/ipc.ts b/web/packages/base/types/ipc.ts
index ed41ac0453..2aeb233b0c 100644
--- a/web/packages/base/types/ipc.ts
+++ b/web/packages/base/types/ipc.ts
@@ -727,6 +727,14 @@ export interface FolderWatch {
* Files (paths) that should be ignored when uploading.
*/
ignoredFiles: string[];
+ /**
+ * Whether the folder is currently accessible on disk.
+ *
+ * Folders on ejected external drives will have this set to false. This
+ * property is not persisted and is computed each time the watch list is
+ * fetched.
+ */
+ isAccessible?: boolean;
}
/**