BlueGit

timfa filemanager

fix: keep folder icons stable for mount roots

timfa Tim Falken · pushed by timfa

Authored 2026-05-04 · 6 months ago

Hash
e725ff07ec321398f27dec43563367f750386f0b
Parent
9c9eda85
3 files changed +70 −2
.github/technical-reference.md +1 −1
@@ -5,7 +5,7 @@
5 5 - shutdown(opts, ...servers) - Graceful teardown: stops thumbnail loop, closes DB/servers, exits with configurable code after drain/timeout.
6 6
7 7 ## server/thumbnail-cache.js
8 - syncThumbnailCacheOnce() - Mirrors configured roots into public/thumbs and generates 99x99-max WebP thumbnails for image files; removes stale thumbnails and updates manifest hashes.
8 - syncThumbnailCacheOnce() - Mirrors configured roots into public/thumbs and generates 99x99-max WebP thumbnails for image files; removes stale thumbnails and updates manifest hashes while preserving prior cache entries for temporarily unavailable roots (e.g. removable mounts).
9 9 - startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
10 10 - stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
11 11 - getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
@@ -5,7 +5,7 @@
5- shutdown(opts, ...servers) - Graceful teardown: stops thumbnail loop, closes DB/servers, exits with configurable code after drain/timeout.
5- shutdown(opts, ...servers) - Graceful teardown: stops thumbnail loop, closes DB/servers, exits with configurable code after drain/timeout.
6
6
7## server/thumbnail-cache.js
7## server/thumbnail-cache.js
8- syncThumbnailCacheOnce() - Mirrors configured roots into public/thumbs and generates 99x99-max WebP thumbnails for image files; removes stale thumbnails and updates manifest hashes.
8- syncThumbnailCacheOnce() - Mirrors configured roots into public/thumbs and generates 99x99-max WebP thumbnails for image files; removes stale thumbnails and updates manifest hashes while preserving prior cache entries for temporarily unavailable roots (e.g. removable mounts).
9- startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
9- startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
10- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
10- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
11- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
11- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
server/thumbnail-cache.js +42 −1
@@ -210,6 +210,25 @@ function effectiveRoots ()
210 210 return getEffectivePaths(loadConfig().paths || []);
211 211 }
212 212
213 function pathInsideOrEqualResolved (targetPath, rootPath)
214 {
215 const rel = path.relative(path.resolve(rootPath), path.resolve(targetPath));
216 return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
217 }
218
219 function preserveRootFromPreviousManifest (rootPath, prevManifest, nextManifest, keepThumbFiles)
220 {
221 for (const [sourcePath, meta] of Object.entries(prevManifest.files || {}))
222 {
223 if (!pathInsideOrEqualResolved(sourcePath, rootPath)) continue;
224 nextManifest.files[sourcePath] = meta;
225 if (meta && meta.thumbRelPath)
226 {
227 keepThumbFiles.add(path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath)));
228 }
229 }
230 }
231
213 232 async function syncThumbnailCacheOnce ()
214 233 {
215 234 await ensureDir(THUMBS_DIR);
@@ -222,7 +241,29 @@ async function syncThumbnailCacheOnce ()
222 241 for (const root of roots)
223 242 {
224 243 const resolvedRoot = path.resolve(root);
225 if (!(await exists(resolvedRoot))) continue;
244 if (!(await exists(resolvedRoot)))
245 {
246 // Keep prior cache for roots that are temporarily offline (e.g. removable mounts).
247 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
248 continue;
249 }
250
251 try
252 {
253 const stat = await fs.promises.stat(resolvedRoot);
254 if (!stat.isDirectory())
255 {
256 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
257 continue;
258 }
259 // Probe readability; failures should not wipe existing cache state.
260 await fs.promises.readdir(resolvedRoot);
261 }
262 catch
263 {
264 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
265 continue;
266 }
226 267
227 268 await walkDir(
228 269 resolvedRoot,
@@ -210,6 +210,25 @@ function effectiveRoots ()
210 return getEffectivePaths(loadConfig().paths || []);
210 return getEffectivePaths(loadConfig().paths || []);
211}
211}
212
212
213function pathInsideOrEqualResolved (targetPath, rootPath)
214{
215 const rel = path.relative(path.resolve(rootPath), path.resolve(targetPath));
216 return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
217}
218
219function preserveRootFromPreviousManifest (rootPath, prevManifest, nextManifest, keepThumbFiles)
220{
221 for (const [sourcePath, meta] of Object.entries(prevManifest.files || {}))
222 {
223 if (!pathInsideOrEqualResolved(sourcePath, rootPath)) continue;
224 nextManifest.files[sourcePath] = meta;
225 if (meta && meta.thumbRelPath)
226 {
227 keepThumbFiles.add(path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath)));
228 }
229 }
230}
231
213async function syncThumbnailCacheOnce ()
232async function syncThumbnailCacheOnce ()
214{
233{
215 await ensureDir(THUMBS_DIR);
234 await ensureDir(THUMBS_DIR);
@@ -222,7 +241,29 @@ async function syncThumbnailCacheOnce ()
222 for (const root of roots)
241 for (const root of roots)
223 {
242 {
224 const resolvedRoot = path.resolve(root);
243 const resolvedRoot = path.resolve(root);
225 if (!(await exists(resolvedRoot))) continue;
244 if (!(await exists(resolvedRoot)))
245 {
246 // Keep prior cache for roots that are temporarily offline (e.g. removable mounts).
247 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
248 continue;
249 }
250
251 try
252 {
253 const stat = await fs.promises.stat(resolvedRoot);
254 if (!stat.isDirectory())
255 {
256 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
257 continue;
258 }
259 // Probe readability; failures should not wipe existing cache state.
260 await fs.promises.readdir(resolvedRoot);
261 }
262 catch
263 {
264 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
265 continue;
266 }
226
267
227 await walkDir(
268 await walkDir(
228 resolvedRoot,
269 resolvedRoot,
tests/thumbnail-cache.test.js +27 −0
@@ -162,4 +162,31 @@ describe('thumbnail cache worker', () =>
162 162 const detected = await mod.getFolderIconSourceFile(publicRoot);
163 163 expect(detected).toBe(nested);
164 164 });
165
166 test('preserves assigned folder icon and thumbnail when root is temporarily unavailable', async () =>
167 {
168 const mod = loadModule();
169 const imagePath = path.join(sourceRoot, 'Pepijn', 'cover.jpg');
170 fs.mkdirSync(path.dirname(imagePath), { recursive: true });
171 await writeTinyPng(imagePath);
172
173 await mod.syncThumbnailCacheOnce();
174 await mod.setFolderIconAssignment(path.dirname(imagePath), imagePath);
175
176 const thumbPath = mod.getThumbPathForSourceFile(imagePath);
177 expect(fs.existsSync(thumbPath)).toBe(true);
178 expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath);
179
180 // Simulate mount disappearing while still configured.
181 const offlinePath = sourceRoot + '-offline';
182 fs.renameSync(sourceRoot, offlinePath);
183
184 await mod.syncThumbnailCacheOnce();
185
186 // Cache entry and assignment must survive the temporary outage.
187 expect(fs.existsSync(thumbPath)).toBe(true);
188 expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath);
189
190 fs.renameSync(offlinePath, sourceRoot);
191 });
165 192 });
@@ -162,4 +162,31 @@ describe('thumbnail cache worker', () =>
162 const detected = await mod.getFolderIconSourceFile(publicRoot);
162 const detected = await mod.getFolderIconSourceFile(publicRoot);
163 expect(detected).toBe(nested);
163 expect(detected).toBe(nested);
164 });
164 });
165
166 test('preserves assigned folder icon and thumbnail when root is temporarily unavailable', async () =>
167 {
168 const mod = loadModule();
169 const imagePath = path.join(sourceRoot, 'Pepijn', 'cover.jpg');
170 fs.mkdirSync(path.dirname(imagePath), { recursive: true });
171 await writeTinyPng(imagePath);
172
173 await mod.syncThumbnailCacheOnce();
174 await mod.setFolderIconAssignment(path.dirname(imagePath), imagePath);
175
176 const thumbPath = mod.getThumbPathForSourceFile(imagePath);
177 expect(fs.existsSync(thumbPath)).toBe(true);
178 expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath);
179
180 // Simulate mount disappearing while still configured.
181 const offlinePath = sourceRoot + '-offline';
182 fs.renameSync(sourceRoot, offlinePath);
183
184 await mod.syncThumbnailCacheOnce();
185
186 // Cache entry and assignment must survive the temporary outage.
187 expect(fs.existsSync(thumbPath)).toBe(true);
188 expect(await mod.getFolderIconSourceFile(path.dirname(imagePath))).toBe(imagePath);
189
190 fs.renameSync(offlinePath, sourceRoot);
191 });
165});
192});