BlueGit

timfa filemanager

Files

filemanager / server / thumbnail-cache.js

thumbnail-cache.js

userdirs
timfa Tim Falken · pushed by timfa · 6 months ago
f803c52f History
thumbnail-cache.js 13.3 KiB Download Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5const crypto = require('crypto');
6const sharp = require('sharp');
7
8const { loadConfig, getEffectivePaths } = require('./config');
9
10const PUBLIC_DIR = path.resolve(process.env.FILEMANAGER_PUBLIC_DIR || path.join(__dirname, '../public'));
11const THUMBS_DIR = path.resolve(process.env.FILEMANAGER_THUMBS_DIR || path.join(PUBLIC_DIR, 'thumbs'));
12const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json');
13const FOLDER_ICONS_PATH = path.join(THUMBS_DIR, '.folder-icons.json');
14const LOOP_DELAY_MS = 5000;
15
16const IMAGE_EXTS = new Set([
17 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
18]);
19
20let loopTimer = null;
21let loopStarted = false;
22
23function toPosix (p)
24{
25 return String(p || '').split(path.sep).join('/');
26}
27
28function rootSegmentsForSourcePath (sourcePath)
29{
30 const resolved = path.resolve(sourcePath);
31 const stripped = resolved
32 .replace(/^\\+/, '')
33 .replace(/^\/+/, '')
34 .replace(/^([A-Za-z]):/, '$1');
35
36 return stripped
37 .split(/[\\/]+/)
38 .filter(Boolean)
39 .map(seg => seg.replace(/:/g, '_'));
40}
41
42function getThumbDirForSourceDir (sourceDir)
43{
44 return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(sourceDir));
45}
46
47function getThumbPathForSourceFile (sourceFile)
48{
49 const source = path.resolve(sourceFile);
50 return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(source)) + '.webp';
51}
52
53function getThumbRelPathForSourceFile (sourceFile)
54{
55 return toPosix(path.relative(PUBLIC_DIR, getThumbPathForSourceFile(sourceFile)));
56}
57
58function isImagePath (filePath)
59{
60 const ext = path.extname(filePath).slice(1).toLowerCase();
61 return IMAGE_EXTS.has(ext);
62}
63
64async function exists (target)
65{
66 try
67 {
68 await fs.promises.access(target, fs.constants.F_OK);
69 return true;
70 } catch
71 {
72 return false;
73 }
74}
75
76async function ensureDir (dirPath)
77{
78 await fs.promises.mkdir(dirPath, { recursive: true });
79}
80
81async function readManifest ()
82{
83 try
84 {
85 const raw = await fs.promises.readFile(MANIFEST_PATH, 'utf8');
86 const parsed = JSON.parse(raw);
87 if (parsed && typeof parsed === 'object' && parsed.files && typeof parsed.files === 'object')
88 {
89 return parsed;
90 }
91 } catch
92 {
93 // Missing or malformed manifests are treated as empty.
94 }
95 return { version: 1, files: {} };
96}
97
98async function writeManifest (manifest)
99{
100 const payload = JSON.stringify(manifest, null, 2);
101 const tmp = MANIFEST_PATH + '.tmp';
102 await fs.promises.writeFile(tmp, payload, 'utf8');
103 await fs.promises.rename(tmp, MANIFEST_PATH);
104}
105
106function sha256File (target)
107{
108 return new Promise((resolve, reject) =>
109 {
110 const hash = crypto.createHash('sha256');
111 const stream = fs.createReadStream(target);
112 stream.on('error', reject);
113 stream.on('data', chunk => hash.update(chunk));
114 stream.on('end', () => resolve(hash.digest('hex')));
115 });
116}
117
118async function walkDir (dirPath, onDir, onFile)
119{
120 await onDir(dirPath);
121
122 let entries = [];
123 try
124 {
125 entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
126 } catch
127 {
128 return;
129 }
130
131 for (const entry of entries)
132 {
133 const full = path.join(dirPath, entry.name);
134 if (entry.isDirectory())
135 {
136 await walkDir(full, onDir, onFile);
137 continue;
138 }
139 if (entry.isFile())
140 {
141 await onFile(full);
142 }
143 }
144}
145
146async function removeFileIfExists (target)
147{
148 try
149 {
150 await fs.promises.unlink(target);
151 } catch (err)
152 {
153 if (err && err.code !== 'ENOENT') throw err;
154 }
155}
156
157async function pruneThumbTree (dirPath, keepFiles)
158{
159 let entries = [];
160 try
161 {
162 entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
163 } catch
164 {
165 return;
166 }
167
168 for (const entry of entries)
169 {
170 const full = path.join(dirPath, entry.name);
171 if (entry.isDirectory())
172 {
173 await pruneThumbTree(full, keepFiles);
174 continue;
175 }
176
177 if (full === MANIFEST_PATH || full === MANIFEST_PATH + '.tmp') continue;
178 if (full === FOLDER_ICONS_PATH || full === FOLDER_ICONS_PATH + '.tmp') continue;
179 if (!keepFiles.has(path.resolve(full)))
180 {
181 await removeFileIfExists(full);
182 }
183 }
184
185 // Never remove the root thumbs directory.
186 if (path.resolve(dirPath) === path.resolve(THUMBS_DIR)) return;
187
188 try
189 {
190 const leftovers = await fs.promises.readdir(dirPath);
191 if (leftovers.length === 0) await fs.promises.rmdir(dirPath);
192 } catch
193 {
194 // Ignore races while pruning.
195 }
196}
197
198async function generateImageThumb (sourceFile, thumbPath)
199{
200 await ensureDir(path.dirname(thumbPath));
201 await sharp(sourceFile)
202 .rotate()
203 .resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true })
204 .webp({ quality: 80 })
205 .toFile(thumbPath);
206}
207
208function effectiveRoots ()
209{
210 const config = loadConfig();
211 const roots = getEffectivePaths(config.paths || []);
212
213 // Include the personal-user root tree so thumbnails are generated for
214 // files stored under userPath/<username>/... as well.
215 const userPath = typeof config.userPath === 'string' ? config.userPath.trim() : '';
216 if (userPath) roots.push(userPath);
217
218 // De-duplicate roots by resolved absolute path.
219 const seen = new Set();
220 const deduped = [];
221 for (const root of roots)
222 {
223 const key = path.resolve(root);
224 if (seen.has(key)) continue;
225 seen.add(key);
226 deduped.push(root);
227 }
228
229 return deduped;
230}
231
232function pathInsideOrEqualResolved (targetPath, rootPath)
233{
234 const rel = path.relative(path.resolve(rootPath), path.resolve(targetPath));
235 return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
236}
237
238function preserveRootFromPreviousManifest (rootPath, prevManifest, nextManifest, keepThumbFiles)
239{
240 for (const [sourcePath, meta] of Object.entries(prevManifest.files || {}))
241 {
242 if (!pathInsideOrEqualResolved(sourcePath, rootPath)) continue;
243 nextManifest.files[sourcePath] = meta;
244 if (meta && meta.thumbRelPath)
245 {
246 keepThumbFiles.add(path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath)));
247 }
248 }
249}
250
251async function syncThumbnailCacheOnce ()
252{
253 await ensureDir(THUMBS_DIR);
254
255 const prevManifest = await readManifest();
256 const nextManifest = { version: 1, files: {} };
257 const keepThumbFiles = new Set();
258 const roots = effectiveRoots();
259
260 for (const root of roots)
261 {
262 const resolvedRoot = path.resolve(root);
263 if (!(await exists(resolvedRoot)))
264 {
265 // Keep prior cache for roots that are temporarily offline (e.g. removable mounts).
266 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
267 continue;
268 }
269
270 try
271 {
272 const stat = await fs.promises.stat(resolvedRoot);
273 if (!stat.isDirectory())
274 {
275 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
276 continue;
277 }
278 // Probe readability; failures should not wipe existing cache state.
279 await fs.promises.readdir(resolvedRoot);
280 }
281 catch
282 {
283 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
284 continue;
285 }
286
287 await walkDir(
288 resolvedRoot,
289 async (dirPath) =>
290 {
291 await ensureDir(getThumbDirForSourceDir(dirPath));
292 },
293 async (filePath) =>
294 {
295 if (!isImagePath(filePath)) return;
296
297 let stat;
298 try
299 {
300 stat = await fs.promises.stat(filePath);
301 } catch
302 {
303 return;
304 }
305
306 const thumbPath = getThumbPathForSourceFile(filePath);
307 const thumbRelPath = getThumbRelPathForSourceFile(filePath);
308 keepThumbFiles.add(path.resolve(thumbPath));
309
310 const prev = prevManifest.files[filePath];
311 const unchangedByStat =
312 prev &&
313 Number(prev.size) === Number(stat.size) &&
314 Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
315 prev.thumbRelPath === thumbRelPath;
316
317 if (unchangedByStat && await exists(thumbPath))
318 {
319 nextManifest.files[filePath] = prev;
320 return;
321 }
322
323 let sha256;
324 try
325 {
326 sha256 = await sha256File(filePath);
327 } catch
328 {
329 return;
330 }
331
332 const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);
333 if (!unchangedByHash)
334 {
335 try
336 {
337 await generateImageThumb(filePath, thumbPath);
338 } catch
339 {
340 return;
341 }
342 }
343
344 nextManifest.files[filePath] = {
345 sha256,
346 size: stat.size,
347 mtimeMs: stat.mtimeMs,
348 thumbRelPath,
349 };
350 },
351 );
352 }
353
354 for (const [sourcePath, oldMeta] of Object.entries(prevManifest.files || {}))
355 {
356 if (nextManifest.files[sourcePath]) continue;
357 const oldThumbRel = oldMeta && oldMeta.thumbRelPath;
358 if (!oldThumbRel) continue;
359 const oldThumbPath = path.resolve(path.join(PUBLIC_DIR, oldThumbRel));
360 await removeFileIfExists(oldThumbPath);
361 }
362
363 await pruneThumbTree(THUMBS_DIR, keepThumbFiles);
364 await writeManifest(nextManifest);
365
366 return {
367 rootsScanned: roots.length,
368 cachedCount: Object.keys(nextManifest.files).length,
369 };
370}
371
372function scheduleNextRun (delayMs)
373{
374 loopTimer = setTimeout(async () =>
375 {
376 loopTimer = null;
377 try
378 {
379 await syncThumbnailCacheOnce();
380 }
381 catch (err)
382 {
383 console.error('[thumb-cache] sync failed:', err && err.message ? err.message : err);
384 }
385 finally
386 {
387 if (loopStarted) scheduleNextRun(LOOP_DELAY_MS);
388 }
389 }, delayMs);
390
391 if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref();
392}
393
394function startThumbnailCacheLoop ()
395{
396 if (loopStarted) return;
397 loopStarted = true;
398 scheduleNextRun(LOOP_DELAY_MS);
399}
400
401function stopThumbnailCacheLoop ()
402{
403 loopStarted = false;
404 if (loopTimer)
405 {
406 clearTimeout(loopTimer);
407 loopTimer = null;
408 }
409}
410
411async function getCachedThumbnailInfo (sourceFile)
412{
413 const thumbPath = getThumbPathForSourceFile(sourceFile);
414 if (!(await exists(thumbPath))) return null;
415 return {
416 thumbPath,
417 contentType: 'image/webp',
418 };
419}
420
421// ---------------------------------------------------------------------------
422// Folder icon assignments — stored in .folder-icons.json
423// ---------------------------------------------------------------------------
424
425async function readFolderIcons ()
426{
427 try
428 {
429 const raw = await fs.promises.readFile(FOLDER_ICONS_PATH, 'utf8');
430 const parsed = JSON.parse(raw);
431 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return parsed;
432 } catch
433 {
434 // Missing or malformed — treat as empty.
435 }
436 return {};
437}
438
439async function writeFolderIcons (obj)
440{
441 await ensureDir(THUMBS_DIR);
442 const payload = JSON.stringify(obj, null, 2);
443 const tmp = FOLDER_ICONS_PATH + '.tmp';
444 await fs.promises.writeFile(tmp, payload, 'utf8');
445 await fs.promises.rename(tmp, FOLDER_ICONS_PATH);
446}
447
448/**
449 * Returns all images inside `dirPath` (recursive) that have a cached thumbnail,
450 * sorted lexicographically by absolute file path.
451 * Each element: { filePath: string, thumbRelPath: string }
452 */
453async function getImagesWithThumbnailsInDir (dirPath)
454{
455 const manifest = await readManifest();
456 const resolved = path.resolve(dirPath);
457 const results = [];
458
459 for (const [filePath, meta] of Object.entries(manifest.files || {}))
460 {
461 const resolvedFile = path.resolve(filePath);
462 const relative = path.relative(resolved, resolvedFile);
463 if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) continue;
464 if (!meta || !meta.thumbRelPath) continue;
465
466 // Verify the thumbnail file actually exists on disk.
467 const thumbPath = path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath));
468 if (!(await exists(thumbPath))) continue;
469
470 results.push({ filePath, thumbRelPath: meta.thumbRelPath });
471 }
472
473 results.sort((a, b) => a.filePath.localeCompare(b.filePath));
474 return results;
475}
476
477/**
478 * Returns the source image file path used as the icon for `folderPath`.
479 * Looks up the explicit assignment first; falls back to the first cached image found.
480 * Returns null when no images with cached thumbnails exist inside the folder.
481 */
482async function getFolderIconSourceFile (folderPath)
483{
484 const resolved = path.resolve(folderPath);
485 const icons = await readFolderIcons();
486
487 if (icons[resolved])
488 {
489 const assigned = icons[resolved];
490 // Verify the assigned image still has a cached thumbnail.
491 const thumbPath = getThumbPathForSourceFile(assigned);
492 if (await exists(thumbPath)) return assigned;
493 // Stale — fall through to auto-detect.
494 }
495
496 // Auto: first cached image inside the folder.
497 const images = await getImagesWithThumbnailsInDir(folderPath);
498 return images.length ? images[0].filePath : null;
499}
500
501/**
502 * Saves an explicit folder icon assignment.
503 * Pass `imagePath = null` to clear the explicit assignment (auto-detect will be used instead).
504 */
505async function setFolderIconAssignment (folderPath, imagePath)
506{
507 const resolved = path.resolve(folderPath);
508 const icons = await readFolderIcons();
509
510 if (imagePath === null || imagePath === undefined)
511 {
512 delete icons[resolved];
513 } else
514 {
515 icons[resolved] = imagePath;
516 }
517
518 await writeFolderIcons(icons);
519}
520
521module.exports = {
522 getCachedThumbnailInfo,
523 getThumbPathForSourceFile,
524 getThumbRelPathForSourceFile,
525 getImagesWithThumbnailsInDir,
526 getFolderIconSourceFile,
527 setFolderIconAssignment,
528 startThumbnailCacheLoop,
529 stopThumbnailCacheLoop,
530 syncThumbnailCacheOnce,
531};