BlueGit

timfa filemanager

Files

filemanager / server / thumbnail-cache.js

thumbnail-cache.js

thumbnail-cache.js 12.7 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 return getEffectivePaths(loadConfig().paths || []);
211}
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
232async function syncThumbnailCacheOnce ()
233{
234 await ensureDir(THUMBS_DIR);
235
236 const prevManifest = await readManifest();
237 const nextManifest = { version: 1, files: {} };
238 const keepThumbFiles = new Set();
239 const roots = effectiveRoots();
240
241 for (const root of roots)
242 {
243 const resolvedRoot = path.resolve(root);
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 }
267
268 await walkDir(
269 resolvedRoot,
270 async (dirPath) =>
271 {
272 await ensureDir(getThumbDirForSourceDir(dirPath));
273 },
274 async (filePath) =>
275 {
276 if (!isImagePath(filePath)) return;
277
278 let stat;
279 try
280 {
281 stat = await fs.promises.stat(filePath);
282 } catch
283 {
284 return;
285 }
286
287 const thumbPath = getThumbPathForSourceFile(filePath);
288 const thumbRelPath = getThumbRelPathForSourceFile(filePath);
289 keepThumbFiles.add(path.resolve(thumbPath));
290
291 const prev = prevManifest.files[filePath];
292 const unchangedByStat =
293 prev &&
294 Number(prev.size) === Number(stat.size) &&
295 Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
296 prev.thumbRelPath === thumbRelPath;
297
298 if (unchangedByStat && await exists(thumbPath))
299 {
300 nextManifest.files[filePath] = prev;
301 return;
302 }
303
304 let sha256;
305 try
306 {
307 sha256 = await sha256File(filePath);
308 } catch
309 {
310 return;
311 }
312
313 const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath);
314 if (!unchangedByHash)
315 {
316 try
317 {
318 await generateImageThumb(filePath, thumbPath);
319 } catch
320 {
321 return;
322 }
323 }
324
325 nextManifest.files[filePath] = {
326 sha256,
327 size: stat.size,
328 mtimeMs: stat.mtimeMs,
329 thumbRelPath,
330 };
331 },
332 );
333 }
334
335 for (const [sourcePath, oldMeta] of Object.entries(prevManifest.files || {}))
336 {
337 if (nextManifest.files[sourcePath]) continue;
338 const oldThumbRel = oldMeta && oldMeta.thumbRelPath;
339 if (!oldThumbRel) continue;
340 const oldThumbPath = path.resolve(path.join(PUBLIC_DIR, oldThumbRel));
341 await removeFileIfExists(oldThumbPath);
342 }
343
344 await pruneThumbTree(THUMBS_DIR, keepThumbFiles);
345 await writeManifest(nextManifest);
346
347 return {
348 rootsScanned: roots.length,
349 cachedCount: Object.keys(nextManifest.files).length,
350 };
351}
352
353function scheduleNextRun (delayMs)
354{
355 loopTimer = setTimeout(async () =>
356 {
357 loopTimer = null;
358 try
359 {
360 await syncThumbnailCacheOnce();
361 }
362 catch (err)
363 {
364 console.error('[thumb-cache] sync failed:', err && err.message ? err.message : err);
365 }
366 finally
367 {
368 if (loopStarted) scheduleNextRun(LOOP_DELAY_MS);
369 }
370 }, delayMs);
371
372 if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref();
373}
374
375function startThumbnailCacheLoop ()
376{
377 if (loopStarted) return;
378 loopStarted = true;
379 scheduleNextRun(LOOP_DELAY_MS);
380}
381
382function stopThumbnailCacheLoop ()
383{
384 loopStarted = false;
385 if (loopTimer)
386 {
387 clearTimeout(loopTimer);
388 loopTimer = null;
389 }
390}
391
392async function getCachedThumbnailInfo (sourceFile)
393{
394 const thumbPath = getThumbPathForSourceFile(sourceFile);
395 if (!(await exists(thumbPath))) return null;
396 return {
397 thumbPath,
398 contentType: 'image/webp',
399 };
400}
401
402// ---------------------------------------------------------------------------
403// Folder icon assignments — stored in .folder-icons.json
404// ---------------------------------------------------------------------------
405
406async function readFolderIcons ()
407{
408 try
409 {
410 const raw = await fs.promises.readFile(FOLDER_ICONS_PATH, 'utf8');
411 const parsed = JSON.parse(raw);
412 if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return parsed;
413 } catch
414 {
415 // Missing or malformed — treat as empty.
416 }
417 return {};
418}
419
420async function writeFolderIcons (obj)
421{
422 await ensureDir(THUMBS_DIR);
423 const payload = JSON.stringify(obj, null, 2);
424 const tmp = FOLDER_ICONS_PATH + '.tmp';
425 await fs.promises.writeFile(tmp, payload, 'utf8');
426 await fs.promises.rename(tmp, FOLDER_ICONS_PATH);
427}
428
429/**
430 * Returns all images inside `dirPath` (recursive) that have a cached thumbnail,
431 * sorted lexicographically by absolute file path.
432 * Each element: { filePath: string, thumbRelPath: string }
433 */
434async function getImagesWithThumbnailsInDir (dirPath)
435{
436 const manifest = await readManifest();
437 const resolved = path.resolve(dirPath);
438 const results = [];
439
440 for (const [filePath, meta] of Object.entries(manifest.files || {}))
441 {
442 const resolvedFile = path.resolve(filePath);
443 const relative = path.relative(resolved, resolvedFile);
444 if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) continue;
445 if (!meta || !meta.thumbRelPath) continue;
446
447 // Verify the thumbnail file actually exists on disk.
448 const thumbPath = path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath));
449 if (!(await exists(thumbPath))) continue;
450
451 results.push({ filePath, thumbRelPath: meta.thumbRelPath });
452 }
453
454 results.sort((a, b) => a.filePath.localeCompare(b.filePath));
455 return results;
456}
457
458/**
459 * Returns the source image file path used as the icon for `folderPath`.
460 * Looks up the explicit assignment first; falls back to the first cached image found.
461 * Returns null when no images with cached thumbnails exist inside the folder.
462 */
463async function getFolderIconSourceFile (folderPath)
464{
465 const resolved = path.resolve(folderPath);
466 const icons = await readFolderIcons();
467
468 if (icons[resolved])
469 {
470 const assigned = icons[resolved];
471 // Verify the assigned image still has a cached thumbnail.
472 const thumbPath = getThumbPathForSourceFile(assigned);
473 if (await exists(thumbPath)) return assigned;
474 // Stale — fall through to auto-detect.
475 }
476
477 // Auto: first cached image inside the folder.
478 const images = await getImagesWithThumbnailsInDir(folderPath);
479 return images.length ? images[0].filePath : null;
480}
481
482/**
483 * Saves an explicit folder icon assignment.
484 * Pass `imagePath = null` to clear the explicit assignment (auto-detect will be used instead).
485 */
486async function setFolderIconAssignment (folderPath, imagePath)
487{
488 const resolved = path.resolve(folderPath);
489 const icons = await readFolderIcons();
490
491 if (imagePath === null || imagePath === undefined)
492 {
493 delete icons[resolved];
494 } else
495 {
496 icons[resolved] = imagePath;
497 }
498
499 await writeFolderIcons(icons);
500}
501
502module.exports = {
503 getCachedThumbnailInfo,
504 getThumbPathForSourceFile,
505 getThumbRelPathForSourceFile,
506 getImagesWithThumbnailsInDir,
507 getFolderIconSourceFile,
508 setFolderIconAssignment,
509 startThumbnailCacheLoop,
510 stopThumbnailCacheLoop,
511 syncThumbnailCacheOnce,
512};