BlueGit

timfa filemanager

Files

filemanager / server / thumbnail-cache.js

thumbnail-cache.js

thumbnail-cache.js 16.5 KiB Download Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5const crypto = require('crypto');
6const { spawn } = require('child_process');
7const sharp = require('sharp');
8
9const { loadConfig, getEffectivePaths } = require('./config');
10
11const PUBLIC_DIR = path.resolve(process.env.FILEMANAGER_PUBLIC_DIR || path.join(__dirname, '../public'));
12const THUMBS_DIR = path.resolve(process.env.FILEMANAGER_THUMBS_DIR || path.join(PUBLIC_DIR, 'thumbs'));
13const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json');
14const FOLDER_ICONS_PATH = path.join(THUMBS_DIR, '.folder-icons.json');
15const LOOP_DELAY_MS = 5000;
16
17const IMAGE_EXTS = new Set([
18 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
19]);
20const VIDEO_EXTS = new Set([
21 'mp4', 'mkv', 'mov', 'avi', 'webm', 'ogv', 'm4v', 'ts', 'mpeg', 'mpg',
22]);
23
24let loopTimer = null;
25let loopStarted = false;
26
27function toPosix (p)
28{
29 return String(p || '').split(path.sep).join('/');
30}
31
32function rootSegmentsForSourcePath (sourcePath)
33{
34 const resolved = path.resolve(sourcePath);
35 const stripped = resolved
36 .replace(/^\\+/, '')
37 .replace(/^\/+/, '')
38 .replace(/^([A-Za-z]):/, '$1');
39
40 return stripped
41 .split(/[\\/]+/)
42 .filter(Boolean)
43 .map(seg => seg.replace(/:/g, '_'));
44}
45
46function getThumbDirForSourceDir (sourceDir)
47{
48 return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(sourceDir));
49}
50
51function getThumbPathForSourceFile (sourceFile)
52{
53 const source = path.resolve(sourceFile);
54 return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(source)) + '.webp';
55}
56
57function getThumbRelPathForSourceFile (sourceFile)
58{
59 return toPosix(path.relative(PUBLIC_DIR, getThumbPathForSourceFile(sourceFile)));
60}
61
62function isImagePath (filePath)
63{
64 const ext = path.extname(filePath).slice(1).toLowerCase();
65 return IMAGE_EXTS.has(ext);
66}
67
68function isVideoPath (filePath)
69{
70 const ext = path.extname(filePath).slice(1).toLowerCase();
71 return VIDEO_EXTS.has(ext);
72}
73
74function ffmpegPath ()
75{
76 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
77}
78
79function ffprobePath ()
80{
81 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
82}
83
84function runCommand (command, args)
85{
86 return new Promise((resolve, reject) =>
87 {
88 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
89 let stdout = '';
90 let stderr = '';
91
92 child.stdout.on('data', (chunk) =>
93 {
94 stdout += chunk.toString();
95 });
96
97 child.stderr.on('data', (chunk) =>
98 {
99 stderr += chunk.toString();
100 });
101
102 child.on('error', (err) =>
103 {
104 if (err && err.code === 'ENOENT')
105 {
106 reject(new Error(`${command} is not installed or not available in PATH.`));
107 return;
108 }
109 reject(err);
110 });
111
112 child.on('close', (code) =>
113 {
114 if (code === 0)
115 {
116 resolve({ stdout, stderr });
117 return;
118 }
119 reject(new Error(stderr.trim() || `${command} exited with code ${code}.`));
120 });
121 });
122}
123
124async function probeDurationSeconds (sourceFile)
125{
126 const { stdout } = await runCommand(ffprobePath(), [
127 '-v', 'error',
128 '-print_format', 'json',
129 '-show_format',
130 sourceFile,
131 ]);
132
133 let payload;
134 try
135 {
136 payload = JSON.parse(stdout || '{}');
137 } catch
138 {
139 return null;
140 }
141
142 const duration = Number(payload && payload.format && payload.format.duration);
143 if (!Number.isFinite(duration) || duration <= 0) return null;
144 return duration;
145}
146
147async function exists (target)
148{
149 try
150 {
151 await fs.promises.access(target, fs.constants.F_OK);
152 return true;
153 } catch
154 {
155 return false;
156 }
157}
158
159async function ensureDir (dirPath)
160{
161 await fs.promises.mkdir(dirPath, { recursive: true });
162}
163
164async function readManifest ()
165{
166 try
167 {
168 const raw = await fs.promises.readFile(MANIFEST_PATH, 'utf8');
169 const parsed = JSON.parse(raw);
170 if (parsed && typeof parsed === 'object' && parsed.files && typeof parsed.files === 'object')
171 {
172 return parsed;
173 }
174 } catch
175 {
176 // Missing or malformed manifests are treated as empty.
177 }
178 return { version: 1, files: {} };
179}
180
181async function writeManifest (manifest)
182{
183 const payload = JSON.stringify(manifest, null, 2);
184 const tmp = MANIFEST_PATH + '.tmp';
185 await fs.promises.writeFile(tmp, payload, 'utf8');
186 await fs.promises.rename(tmp, MANIFEST_PATH);
187}
188
189function sha256File (target)
190{
191 return new Promise((resolve, reject) =>
192 {
193 const hash = crypto.createHash('sha256');
194 const stream = fs.createReadStream(target);
195 stream.on('error', reject);
196 stream.on('data', chunk => hash.update(chunk));
197 stream.on('end', () => resolve(hash.digest('hex')));
198 });
199}
200
201async function walkDir (dirPath, onDir, onFile)
202{
203 await onDir(dirPath);
204
205 let entries = [];
206 try
207 {
208 entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
209 } catch
210 {
211 return;
212 }
213
214 for (const entry of entries)
215 {
216 const full = path.join(dirPath, entry.name);
217 if (entry.isDirectory())
218 {
219 await walkDir(full, onDir, onFile);
220 continue;
221 }
222 if (entry.isFile())
223 {
224 await onFile(full);
225 }
226 }
227}
228
229async function removeFileIfExists (target)
230{
231 try
232 {
233 await fs.promises.unlink(target);
234 } catch (err)
235 {
236 if (err && err.code !== 'ENOENT') throw err;
237 }
238}
239
240async function pruneThumbTree (dirPath, keepFiles)
241{
242 let entries = [];
243 try
244 {
245 entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
246 } catch
247 {
248 return;
249 }
250
251 for (const entry of entries)
252 {
253 const full = path.join(dirPath, entry.name);
254 if (entry.isDirectory())
255 {
256 await pruneThumbTree(full, keepFiles);
257 continue;
258 }
259
260 if (full === MANIFEST_PATH || full === MANIFEST_PATH + '.tmp') continue;
261 if (full === FOLDER_ICONS_PATH || full === FOLDER_ICONS_PATH + '.tmp') continue;
262 if (!keepFiles.has(path.resolve(full)))
263 {
264 await removeFileIfExists(full);
265 }
266 }
267
268 // Never remove the root thumbs directory.
269 if (path.resolve(dirPath) === path.resolve(THUMBS_DIR)) return;
270
271 try
272 {
273 const leftovers = await fs.promises.readdir(dirPath);
274 if (leftovers.length === 0) await fs.promises.rmdir(dirPath);
275 } catch
276 {
277 // Ignore races while pruning.
278 }
279}
280
281async function generateImageThumb (sourceFile, thumbPath)
282{
283 await ensureDir(path.dirname(thumbPath));
284 await sharp(sourceFile)
285 .rotate()
286 .resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true })
287 .webp({ quality: 80 })
288 .toFile(thumbPath);
289}
290
291async function generateVideoThumb (sourceFile, thumbPath)
292{
293 await ensureDir(path.dirname(thumbPath));
294
295 const duration = await probeDurationSeconds(sourceFile).catch(() => null);
296 let captureAt = 1;
297 if (duration && duration > 0)
298 {
299 captureAt = duration * 0.1;
300 if (duration > 0.2) captureAt = Math.min(captureAt, duration - 0.1);
301 captureAt = Math.max(0, captureAt);
302 }
303
304 const tmpPngPath = thumbPath + '.tmp.png';
305 try
306 {
307 await runCommand(ffmpegPath(), [
308 '-hide_banner',
309 '-loglevel', 'error',
310 '-y',
311 '-ss', captureAt.toFixed(3),
312 '-i', sourceFile,
313 '-frames:v', '1',
314 '-an',
315 '-sn',
316 tmpPngPath,
317 ]);
318
319 await sharp(tmpPngPath)
320 .rotate()
321 .resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true })
322 .webp({ quality: 80 })
323 .toFile(thumbPath);
324 }
325 finally
326 {
327 await removeFileIfExists(tmpPngPath);
328 }
329}
330
331function effectiveRoots ()
332{
333 const config = loadConfig();
334 const roots = getEffectivePaths(config.paths || []);
335
336 // Include the personal-user root tree so thumbnails are generated for
337 // files stored under userPath/<username>/... as well.
338 const userPath = typeof config.userPath === 'string' ? config.userPath.trim() : '';
339 if (userPath) roots.push(userPath);
340
341 // De-duplicate roots by resolved absolute path.
342 const seen = new Set();
343 const deduped = [];
344 for (const root of roots)
345 {
346 const key = path.resolve(root);
347 if (seen.has(key)) continue;
348 seen.add(key);
349 deduped.push(root);
350 }
351
352 return deduped;
353}
354
355function pathInsideOrEqualResolved (targetPath, rootPath)
356{
357 const rel = path.relative(path.resolve(rootPath), path.resolve(targetPath));
358 return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
359}
360
361function preserveRootFromPreviousManifest (rootPath, prevManifest, nextManifest, keepThumbFiles)
362{
363 for (const [sourcePath, meta] of Object.entries(prevManifest.files || {}))
364 {
365 if (!pathInsideOrEqualResolved(sourcePath, rootPath)) continue;
366 nextManifest.files[sourcePath] = meta;
367 if (meta && meta.thumbRelPath)
368 {
369 keepThumbFiles.add(path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath)));
370 }
371 }
372}
373
374async function syncThumbnailCacheOnce ()
375{
376 await ensureDir(THUMBS_DIR);
377
378 const prevManifest = await readManifest();
379 const nextManifest = { version: 1, files: {} };
380 const keepThumbFiles = new Set();
381 const roots = effectiveRoots();
382
383 for (const root of roots)
384 {
385 const resolvedRoot = path.resolve(root);
386 if (!(await exists(resolvedRoot)))
387 {
388 // Keep prior cache for roots that are temporarily offline (e.g. removable mounts).
389 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
390 continue;
391 }
392
393 try
394 {
395 const stat = await fs.promises.stat(resolvedRoot);
396 if (!stat.isDirectory())
397 {
398 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
399 continue;
400 }
401 // Probe readability; failures should not wipe existing cache state.
402 await fs.promises.readdir(resolvedRoot);
403 }
404 catch
405 {
406 preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles);
407 continue;
408 }
409
410 await walkDir(
411 resolvedRoot,
412 async (dirPath) =>
413 {
414 await ensureDir(getThumbDirForSourceDir(dirPath));
415 },
416 async (filePath) =>
417 {
418 const kind = isImagePath(filePath)
419 ? 'image'
420 : isVideoPath(filePath)
421 ? 'video'
422 : null;
423 if (!kind) return;
424
425 let stat;
426 try
427 {
428 stat = await fs.promises.stat(filePath);
429 } catch
430 {
431 return;
432 }
433
434 const thumbPath = getThumbPathForSourceFile(filePath);
435 const thumbRelPath = getThumbRelPathForSourceFile(filePath);
436 keepThumbFiles.add(path.resolve(thumbPath));
437
438 const prev = prevManifest.files[filePath];
439 const unchangedByStat =
440 prev &&
441 Number(prev.size) === Number(stat.size) &&
442 Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
443 prev.thumbRelPath === thumbRelPath &&
444 (prev.kind === kind || !prev.kind);
445
446 if (unchangedByStat && await exists(thumbPath))
447 {
448 nextManifest.files[filePath] = {
449 ...prev,
450 kind,
451 };
452 return;
453 }
454
455 if (kind === 'image')
456 {
457 let sha256;
458 try
459 {
460 sha256 = await sha256File(filePath);
461 } catch
462 {
463 return;
464 }
465
466 const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);
467 if (!unchangedByHash)
468 {
469 try
470 {
471 await generateImageThumb(filePath, thumbPath);
472 } catch
473 {
474 return;
475 }
476 }
477
478 nextManifest.files[filePath] = {
479 sha256,
480 size: stat.size,
481 mtimeMs: stat.mtimeMs,
482 thumbRelPath,
483 kind,
484 };
485 return;
486 }
487
488 try
489 {
490 await generateVideoThumb(filePath, thumbPath);
491 } catch
492 {
493 return;
494 }
495
496 nextManifest.files[filePath] = {
497 sha256: null,
498 size: stat.size,
499 mtimeMs: stat.mtimeMs,
500 thumbRelPath,
501 kind,
502 };
503 },
504 );
505 }
506
507 for (const [sourcePath, oldMeta] of Object.entries(prevManifest.files || {}))
508 {
509 if (nextManifest.files[sourcePath]) continue;
510 const oldThumbRel = oldMeta && oldMeta.thumbRelPath;
511 if (!oldThumbRel) continue;
512 const oldThumbPath = path.resolve(path.join(PUBLIC_DIR, oldThumbRel));
513 await removeFileIfExists(oldThumbPath);
514 }
515
516 await pruneThumbTree(THUMBS_DIR, keepThumbFiles);
517 await writeManifest(nextManifest);
518
519 return {
520 rootsScanned: roots.length,
521 cachedCount: Object.keys(nextManifest.files).length,
522 };
523}
524
525function scheduleNextRun (delayMs)
526{
527 loopTimer = setTimeout(async () =>
528 {
529 loopTimer = null;
530 try
531 {
532 await syncThumbnailCacheOnce();
533 }
534 catch (err)
535 {
536 console.error('[thumb-cache] sync failed:', err && err.message ? err.message : err);
537 }
538 finally
539 {
540 if (loopStarted) scheduleNextRun(LOOP_DELAY_MS);
541 }
542 }, delayMs);
543
544 if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref();
545}
546
547function startThumbnailCacheLoop ()
548{
549 if (loopStarted) return;
550 loopStarted = true;
551 scheduleNextRun(LOOP_DELAY_MS);
552}
553
554function stopThumbnailCacheLoop ()
555{
556 loopStarted = false;
557 if (loopTimer)
558 {
559 clearTimeout(loopTimer);
560 loopTimer = null;
561 }
562}
563
564async function getCachedThumbnailInfo (sourceFile)
565{
566 const thumbPath = getThumbPathForSourceFile(sourceFile);
567 if (!(await exists(thumbPath))) return null;
568 return {
569 thumbPath,
570 contentType: 'image/webp',
571 };
572}
573
574// ---------------------------------------------------------------------------
575// Folder icon assignments — stored in .folder-icons.json
576// ---------------------------------------------------------------------------
577
578async function readFolderIcons ()
579{
580 try
581 {
582 const raw = await fs.promises.readFile(FOLDER_ICONS_PATH, 'utf8');
583 const parsed = JSON.parse(raw);
584 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return parsed;
585 } catch
586 {
587 // Missing or malformed — treat as empty.
588 }
589 return {};
590}
591
592async function writeFolderIcons (obj)
593{
594 await ensureDir(THUMBS_DIR);
595 const payload = JSON.stringify(obj, null, 2);
596 const tmp = FOLDER_ICONS_PATH + '.tmp';
597 await fs.promises.writeFile(tmp, payload, 'utf8');
598 await fs.promises.rename(tmp, FOLDER_ICONS_PATH);
599}
600
601/**
602 * Returns all images inside `dirPath` (recursive) that have a cached thumbnail,
603 * sorted lexicographically by absolute file path.
604 * Each element: { filePath: string, thumbRelPath: string }
605 */
606async function getImagesWithThumbnailsInDir (dirPath)
607{
608 const manifest = await readManifest();
609 const resolved = path.resolve(dirPath);
610 const results = [];
611
612 for (const [filePath, meta] of Object.entries(manifest.files || {}))
613 {
614 const resolvedFile = path.resolve(filePath);
615 const relative = path.relative(resolved, resolvedFile);
616 if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) continue;
617 if (!meta || !meta.thumbRelPath) continue;
618
619 // Verify the thumbnail file actually exists on disk.
620 const thumbPath = path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath));
621 if (!(await exists(thumbPath))) continue;
622
623 results.push({ filePath, thumbRelPath: meta.thumbRelPath });
624 }
625
626 results.sort((a, b) => a.filePath.localeCompare(b.filePath));
627 return results;
628}
629
630/**
631 * Returns the source image file path used as the icon for `folderPath`.
632 * Looks up the explicit assignment first; falls back to the first cached image found.
633 * Returns null when no images with cached thumbnails exist inside the folder.
634 */
635async function getFolderIconSourceFile (folderPath)
636{
637 const resolved = path.resolve(folderPath);
638 const icons = await readFolderIcons();
639
640 if (icons[resolved])
641 {
642 const assigned = icons[resolved];
643 // Verify the assigned image still has a cached thumbnail.
644 const thumbPath = getThumbPathForSourceFile(assigned);
645 if (await exists(thumbPath)) return assigned;
646 // Stale — fall through to auto-detect.
647 }
648
649 // Auto: first cached image inside the folder.
650 const images = await getImagesWithThumbnailsInDir(folderPath);
651 return images.length ? images[0].filePath : null;
652}
653
654/**
655 * Saves an explicit folder icon assignment.
656 * Pass `imagePath = null` to clear the explicit assignment (auto-detect will be used instead).
657 */
658async function setFolderIconAssignment (folderPath, imagePath)
659{
660 const resolved = path.resolve(folderPath);
661 const icons = await readFolderIcons();
662
663 if (imagePath === null || imagePath === undefined)
664 {
665 delete icons[resolved];
666 } else
667 {
668 icons[resolved] = imagePath;
669 }
670
671 await writeFolderIcons(icons);
672}
673
674module.exports = {
675 getCachedThumbnailInfo,
676 getThumbPathForSourceFile,
677 getThumbRelPathForSourceFile,
678 getImagesWithThumbnailsInDir,
679 getFolderIconSourceFile,
680 setFolderIconAssignment,
681 startThumbnailCacheLoop,
682 stopThumbnailCacheLoop,
683 syncThumbnailCacheOnce,
684};