BlueGit

timfa filemanager

Files

filemanager / server / thumbnail-cache.js

thumbnail-cache.js

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