BlueGit

timfa filemanager

Files

filemanager / server / thumbnail-cache.js

thumbnail-cache.js

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