BlueGit

timfa filemanager

Merge feature: video frame thumbnails

timfa Tim Falken · pushed by timfa

Authored 2026-05-15 · 6 months ago

Hash
45ac321e9e8185cd660c563d31ed7cfd7bc4c38d
5 files changed +244 −18
.github/technical-reference.md +4 −1
@@ -6,7 +6,7 @@
6 6 - GET /share/:token - Calls buildShareOpenGraphPage to serve share.html with injected OG meta tags for all visitors; social media crawlers see rich preview data, browsers get the full interactive page.
7 7
8 8 ## server/thumbnail-cache.js
9 - syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files; removes stale thumbnails and updates manifest hashes while preserving prior cache entries for temporarily unavailable roots (e.g. removable mounts).
9 - syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files and video files (frame around 10% via ffprobe/ffmpeg); removes stale thumbnails and updates manifest hashes while preserving prior cache entries for temporarily unavailable roots (e.g. removable mounts).
10 10 - startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
11 11 - stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
12 12 - getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
@@ -144,6 +144,7 @@
144 144 - openShareModal(item) - Opens the Delen (share) popup, calls POST /api/share/create, shows the 24-hour URL with a copy button and OK to dismiss.
145 145 - loadThumbnails() - Registers visible file icons for lazy thumbnail loading (both file thumbnails and folder icons); retries missing cache thumbnails during incremental refresh.
146 146 - loadOneThumbnail(iconEl) - Loads thumbnails from server cache endpoint for images, text snapshots for text files, and badges for other files; marks image icons as pending when cache returns 404 and uses direct source SVG thumbnails for SVG files under 1MB.
147 - loadOneThumbnail(iconEl) - Loads thumbnails from server cache endpoint for images and videos, text snapshots for text files, and badges for other files; marks image/video icons as pending when cache returns 404 and uses direct source SVG thumbnails for SVG files under 1MB.
147 148 - observeFolderIcon(iconEl) - Registers a folder icon element with IntersectionObserver for lazy loading.
148 149 - loadOneFolderIcon(iconEl) - Fetches /api/files/folder-icon then the thumbnail blob and overlays it inside the folder SVG; no-op if no icon available.
149 150 - applyFolderThumbnail(iconEl, srcUrl) - Injects an SVG <image> element clipped to the folder outline path into the folder's icon SVG.
@@ -161,6 +162,7 @@
161 162 ## tests/thumbnail-cache.test.js
162 163 - thumbnail cache worker / creates cached thumbnail with dimensions smaller than 100px - Verifies cached image generation and manifest write.
163 164 - thumbnail cache worker / removes cached thumbnail when source file is deleted - Verifies stale thumbnail cleanup behavior.
165 - thumbnail cache worker / creates cached thumbnail for video files using ffmpeg frame extraction - Verifies video thumbnail generation uses ffprobe duration and ffmpeg frame capture.
164 166 - afterEach() cleanup - Removes temp roots with `fs.rmSync(...maxRetries...)` to tolerate transient Windows file locks.
165 167 - detectCodeLanguage(name, mime) - Determines preview language for text/script files by MIME and extension.
166 168 - prettyPrintCode(raw, lang) - Best-effort formatter for JSON/markup/script previews before rendering.
@@ -253,6 +255,7 @@
253 255 - video preview releases the backend stream session when closed - Verifies closing a streamed video preview sends an explicit backend release call.
254 256 - rapid file reselection does not duplicate sidebar action buttons after delayed metadata - Verifies stale sidebar loads are ignored so action buttons are not duplicated.
255 257 - video tiles show centred watched overlays after watched metadata is loaded - Verifies overlays remain absent until watched metadata is fetched, then render as solid eye icons for fully and partway statuses.
258 - shows pending icon spinner when video thumbnail cache is not ready - Verifies video tiles use thumbnail-cache loading (instead of badge fallback) and pending-state UI.
256 259
257 260 ## tests/video-stream.test.js
258 261 - createVideoStreamSession resolves once ffmpeg starts writing stream assets - Verifies session creation no longer waits for the final master playlist before preview can begin.
@@ -6,7 +6,7 @@
6- GET /share/:token - Calls buildShareOpenGraphPage to serve share.html with injected OG meta tags for all visitors; social media crawlers see rich preview data, browsers get the full interactive page.
6- GET /share/:token - Calls buildShareOpenGraphPage to serve share.html with injected OG meta tags for all visitors; social media crawlers see rich preview data, browsers get the full interactive page.
7
7
8## server/thumbnail-cache.js
8## server/thumbnail-cache.js
9- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files; removes stale thumbnails and updates manifest hashes while preserving prior cache entries for temporarily unavailable roots (e.g. removable mounts).
9- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files and video files (frame around 10% via ffprobe/ffmpeg); removes stale thumbnails and updates manifest hashes while preserving prior cache entries for temporarily unavailable roots (e.g. removable mounts).
10- startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
10- startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
11- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
11- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
12- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
12- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
@@ -144,6 +144,7 @@
144- openShareModal(item) - Opens the Delen (share) popup, calls POST /api/share/create, shows the 24-hour URL with a copy button and OK to dismiss.
144- openShareModal(item) - Opens the Delen (share) popup, calls POST /api/share/create, shows the 24-hour URL with a copy button and OK to dismiss.
145- loadThumbnails() - Registers visible file icons for lazy thumbnail loading (both file thumbnails and folder icons); retries missing cache thumbnails during incremental refresh.
145- loadThumbnails() - Registers visible file icons for lazy thumbnail loading (both file thumbnails and folder icons); retries missing cache thumbnails during incremental refresh.
146- loadOneThumbnail(iconEl) - Loads thumbnails from server cache endpoint for images, text snapshots for text files, and badges for other files; marks image icons as pending when cache returns 404 and uses direct source SVG thumbnails for SVG files under 1MB.
146- loadOneThumbnail(iconEl) - Loads thumbnails from server cache endpoint for images, text snapshots for text files, and badges for other files; marks image icons as pending when cache returns 404 and uses direct source SVG thumbnails for SVG files under 1MB.
147- loadOneThumbnail(iconEl) - Loads thumbnails from server cache endpoint for images and videos, text snapshots for text files, and badges for other files; marks image/video icons as pending when cache returns 404 and uses direct source SVG thumbnails for SVG files under 1MB.
147- observeFolderIcon(iconEl) - Registers a folder icon element with IntersectionObserver for lazy loading.
148- observeFolderIcon(iconEl) - Registers a folder icon element with IntersectionObserver for lazy loading.
148- loadOneFolderIcon(iconEl) - Fetches /api/files/folder-icon then the thumbnail blob and overlays it inside the folder SVG; no-op if no icon available.
149- loadOneFolderIcon(iconEl) - Fetches /api/files/folder-icon then the thumbnail blob and overlays it inside the folder SVG; no-op if no icon available.
149- applyFolderThumbnail(iconEl, srcUrl) - Injects an SVG <image> element clipped to the folder outline path into the folder's icon SVG.
150- applyFolderThumbnail(iconEl, srcUrl) - Injects an SVG <image> element clipped to the folder outline path into the folder's icon SVG.
@@ -161,6 +162,7 @@
161## tests/thumbnail-cache.test.js
162## tests/thumbnail-cache.test.js
162- thumbnail cache worker / creates cached thumbnail with dimensions smaller than 100px - Verifies cached image generation and manifest write.
163- thumbnail cache worker / creates cached thumbnail with dimensions smaller than 100px - Verifies cached image generation and manifest write.
163- thumbnail cache worker / removes cached thumbnail when source file is deleted - Verifies stale thumbnail cleanup behavior.
164- thumbnail cache worker / removes cached thumbnail when source file is deleted - Verifies stale thumbnail cleanup behavior.
165- thumbnail cache worker / creates cached thumbnail for video files using ffmpeg frame extraction - Verifies video thumbnail generation uses ffprobe duration and ffmpeg frame capture.
164- afterEach() cleanup - Removes temp roots with `fs.rmSync(...maxRetries...)` to tolerate transient Windows file locks.
166- afterEach() cleanup - Removes temp roots with `fs.rmSync(...maxRetries...)` to tolerate transient Windows file locks.
165- detectCodeLanguage(name, mime) - Determines preview language for text/script files by MIME and extension.
167- detectCodeLanguage(name, mime) - Determines preview language for text/script files by MIME and extension.
166- prettyPrintCode(raw, lang) - Best-effort formatter for JSON/markup/script previews before rendering.
168- prettyPrintCode(raw, lang) - Best-effort formatter for JSON/markup/script previews before rendering.
@@ -253,6 +255,7 @@
253- video preview releases the backend stream session when closed - Verifies closing a streamed video preview sends an explicit backend release call.
255- video preview releases the backend stream session when closed - Verifies closing a streamed video preview sends an explicit backend release call.
254- rapid file reselection does not duplicate sidebar action buttons after delayed metadata - Verifies stale sidebar loads are ignored so action buttons are not duplicated.
256- rapid file reselection does not duplicate sidebar action buttons after delayed metadata - Verifies stale sidebar loads are ignored so action buttons are not duplicated.
255- video tiles show centred watched overlays after watched metadata is loaded - Verifies overlays remain absent until watched metadata is fetched, then render as solid eye icons for fully and partway statuses.
257- video tiles show centred watched overlays after watched metadata is loaded - Verifies overlays remain absent until watched metadata is fetched, then render as solid eye icons for fully and partway statuses.
258- shows pending icon spinner when video thumbnail cache is not ready - Verifies video tiles use thumbnail-cache loading (instead of badge fallback) and pending-state UI.
256
259
257## tests/video-stream.test.js
260## tests/video-stream.test.js
258- createVideoStreamSession resolves once ffmpeg starts writing stream assets - Verifies session creation no longer waits for the final master playlist before preview can begin.
261- createVideoStreamSession resolves once ffmpeg starts writing stream assets - Verifies session creation no longer waits for the final master playlist before preview can begin.
public/app.js +1 −1
@@ -4346,7 +4346,7 @@ function setThumbnailPending (iconEl, pending)
4346 4346 function getThumbnailKind (name)
4347 4347 {
4348 4348 const ext = extOf(name || '');
4349 if (IMAGE_EXTS.has(ext)) return 'image';
4349 if (IMAGE_EXTS.has(ext) || VIDEO_EXTS.has(ext)) return 'image';
4350 4350 if (CODE_EXTS.has(ext)) return 'text';
4351 4351 return 'badge';
4352 4352 }
@@ -4346,7 +4346,7 @@ function setThumbnailPending (iconEl, pending)
4346function getThumbnailKind (name)
4346function getThumbnailKind (name)
4347{
4347{
4348 const ext = extOf(name || '');
4348 const ext = extOf(name || '');
4349 if (IMAGE_EXTS.has(ext)) return 'image';
4349 if (IMAGE_EXTS.has(ext) || VIDEO_EXTS.has(ext)) return 'image';
4350 if (CODE_EXTS.has(ext)) return 'text';
4350 if (CODE_EXTS.has(ext)) return 'text';
4351 return 'badge';
4351 return 'badge';
4352}
4352}
server/thumbnail-cache.js +169 −16
@@ -3,6 +3,7 @@
3 3 const fs = require('fs');
4 4 const path = require('path');
5 5 const crypto = require('crypto');
6 const { spawn } = require('child_process');
6 7 const sharp = require('sharp');
7 8
8 9 const { loadConfig, getEffectivePaths } = require('./config');
@@ -16,6 +17,9 @@ const LOOP_DELAY_MS = 5000;
16 17 const IMAGE_EXTS = new Set([
17 18 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
18 19 ]);
20 const VIDEO_EXTS = new Set([
21 'mp4', 'mkv', 'mov', 'avi', 'webm', 'ogv', 'm4v', 'ts', 'mpeg', 'mpg',
22 ]);
19 23
20 24 let loopTimer = null;
21 25 let loopStarted = false;
@@ -61,6 +65,85 @@ function isImagePath (filePath)
61 65 return IMAGE_EXTS.has(ext);
62 66 }
63 67
68 function isVideoPath (filePath)
69 {
70 const ext = path.extname(filePath).slice(1).toLowerCase();
71 return VIDEO_EXTS.has(ext);
72 }
73
74 function ffmpegPath ()
75 {
76 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
77 }
78
79 function ffprobePath ()
80 {
81 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
82 }
83
84 function 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
124 async 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
64 147 async function exists (target)
65 148 {
66 149 try
@@ -205,6 +288,46 @@ async function generateImageThumb (sourceFile, thumbPath)
205 288 .toFile(thumbPath);
206 289 }
207 290
291 async 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
208 331 function effectiveRoots ()
209 332 {
210 333 const config = loadConfig();
@@ -292,7 +415,12 @@ async function syncThumbnailCacheOnce ()
292 415 },
293 416 async (filePath) =>
294 417 {
295 if (!isImagePath(filePath)) return;
418 const kind = isImagePath(filePath)
419 ? 'image'
420 : isVideoPath(filePath)
421 ? 'video'
422 : null;
423 if (!kind) return;
296 424
297 425 let stat;
298 426 try
@@ -312,40 +440,65 @@ async function syncThumbnailCacheOnce ()
312 440 prev &&
313 441 Number(prev.size) === Number(stat.size) &&
314 442 Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
315 prev.thumbRelPath === thumbRelPath;
443 prev.thumbRelPath === thumbRelPath &&
444 (prev.kind === kind || !prev.kind);
316 445
317 446 if (unchangedByStat && await exists(thumbPath))
318 447 {
319 nextManifest.files[filePath] = prev;
448 nextManifest.files[filePath] = {
449 ...prev,
450 kind,
451 };
320 452 return;
321 453 }
322 454
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)
455 if (kind === 'image')
334 456 {
457 let sha256;
335 458 try
336 459 {
337 await generateImageThumb(filePath, thumbPath);
460 sha256 = await sha256File(filePath);
338 461 } catch
339 462 {
340 463 return;
341 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;
342 494 }
343 495
344 496 nextManifest.files[filePath] = {
345 sha256,
497 sha256: null,
346 498 size: stat.size,
347 499 mtimeMs: stat.mtimeMs,
348 500 thumbRelPath,
501 kind,
349 502 };
350 503 },
351 504 );
@@ -3,6 +3,7 @@
3const fs = require('fs');
3const fs = require('fs');
4const path = require('path');
4const path = require('path');
5const crypto = require('crypto');
5const crypto = require('crypto');
6const { spawn } = require('child_process');
6const sharp = require('sharp');
7const sharp = require('sharp');
7
8
8const { loadConfig, getEffectivePaths } = require('./config');
9const { loadConfig, getEffectivePaths } = require('./config');
@@ -16,6 +17,9 @@ const LOOP_DELAY_MS = 5000;
16const IMAGE_EXTS = new Set([
17const IMAGE_EXTS = new Set([
17 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
18 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
18]);
19]);
20const VIDEO_EXTS = new Set([
21 'mp4', 'mkv', 'mov', 'avi', 'webm', 'ogv', 'm4v', 'ts', 'mpeg', 'mpg',
22]);
19
23
20let loopTimer = null;
24let loopTimer = null;
21let loopStarted = false;
25let loopStarted = false;
@@ -61,6 +65,85 @@ function isImagePath (filePath)
61 return IMAGE_EXTS.has(ext);
65 return IMAGE_EXTS.has(ext);
62}
66}
63
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
64async function exists (target)
147async function exists (target)
65{
148{
66 try
149 try
@@ -205,6 +288,46 @@ async function generateImageThumb (sourceFile, thumbPath)
205 .toFile(thumbPath);
288 .toFile(thumbPath);
206}
289}
207
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
208function effectiveRoots ()
331function effectiveRoots ()
209{
332{
210 const config = loadConfig();
333 const config = loadConfig();
@@ -292,7 +415,12 @@ async function syncThumbnailCacheOnce ()
292 },
415 },
293 async (filePath) =>
416 async (filePath) =>
294 {
417 {
295 if (!isImagePath(filePath)) return;
418 const kind = isImagePath(filePath)
419 ? 'image'
420 : isVideoPath(filePath)
421 ? 'video'
422 : null;
423 if (!kind) return;
296
424
297 let stat;
425 let stat;
298 try
426 try
@@ -312,40 +440,65 @@ async function syncThumbnailCacheOnce ()
312 prev &&
440 prev &&
313 Number(prev.size) === Number(stat.size) &&
441 Number(prev.size) === Number(stat.size) &&
314 Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
442 Number(prev.mtimeMs) === Number(stat.mtimeMs) &&
315 prev.thumbRelPath === thumbRelPath;
443 prev.thumbRelPath === thumbRelPath &&
444 (prev.kind === kind || !prev.kind);
316
445
317 if (unchangedByStat && await exists(thumbPath))
446 if (unchangedByStat && await exists(thumbPath))
318 {
447 {
319 nextManifest.files[filePath] = prev;
448 nextManifest.files[filePath] = {
449 ...prev,
450 kind,
451 };
320 return;
452 return;
321 }
453 }
322
454
323 let sha256;
455 if (kind === 'image')
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 {
456 {
457 let sha256;
335 try
458 try
336 {
459 {
337 await generateImageThumb(filePath, thumbPath);
460 sha256 = await sha256File(filePath);
338 } catch
461 } catch
339 {
462 {
340 return;
463 return;
341 }
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;
342 }
494 }
343
495
344 nextManifest.files[filePath] = {
496 nextManifest.files[filePath] = {
345 sha256,
497 sha256: null,
346 size: stat.size,
498 size: stat.size,
347 mtimeMs: stat.mtimeMs,
499 mtimeMs: stat.mtimeMs,
348 thumbRelPath,
500 thumbRelPath,
501 kind,
349 };
502 };
350 },
503 },
351 );
504 );
tests/page.load.test.js +26 −0
@@ -720,6 +720,32 @@ describe('page load smoke test', () =>
720 720 dom.window.close();
721 721 });
722 722
723 test('shows pending icon spinner when video thumbnail cache is not ready', async () =>
724 {
725 listFixture = {
726 '/data': [
727 {
728 name: 'clip.mp4',
729 path: '/data/clip.mp4',
730 isDir: false,
731 size: 5 * 1024 * 1024,
732 mtime: Date.now(),
733 },
734 ],
735 };
736 thumbnailStatusFixture['/data/clip.mp4'] = 404;
737
738 const { dom, runtimeErrors } = await bootClient();
739 expect(runtimeErrors).toEqual([]);
740
741 await new Promise(resolve => dom.window.setTimeout(resolve, 80));
742 const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/clip.mp4"]');
743 expect(icon).toBeTruthy();
744 expect(icon.classList.contains('thumb-pending')).toBe(true);
745
746 dom.window.close();
747 });
748
723 749 test('rapid file reselection does not duplicate sidebar action buttons after delayed metadata', async () =>
724 750 {
725 751 listFixture = {
@@ -720,6 +720,32 @@ describe('page load smoke test', () =>
720 dom.window.close();
720 dom.window.close();
721 });
721 });
722
722
723 test('shows pending icon spinner when video thumbnail cache is not ready', async () =>
724 {
725 listFixture = {
726 '/data': [
727 {
728 name: 'clip.mp4',
729 path: '/data/clip.mp4',
730 isDir: false,
731 size: 5 * 1024 * 1024,
732 mtime: Date.now(),
733 },
734 ],
735 };
736 thumbnailStatusFixture['/data/clip.mp4'] = 404;
737
738 const { dom, runtimeErrors } = await bootClient();
739 expect(runtimeErrors).toEqual([]);
740
741 await new Promise(resolve => dom.window.setTimeout(resolve, 80));
742 const icon = dom.window.document.querySelector('.icon[data-preview-path="/data/clip.mp4"]');
743 expect(icon).toBeTruthy();
744 expect(icon.classList.contains('thumb-pending')).toBe(true);
745
746 dom.window.close();
747 });
748
723 test('rapid file reselection does not duplicate sidebar action buttons after delayed metadata', async () =>
749 test('rapid file reselection does not duplicate sidebar action buttons after delayed metadata', async () =>
724 {
750 {
725 listFixture = {
751 listFixture = {
tests/thumbnail-cache.test.js +44 −0
@@ -37,6 +37,12 @@ function writeTinySvg (target)
37 37 fs.writeFileSync(target, svg, 'utf8');
38 38 }
39 39
40 function writeExecutable (target, content)
41 {
42 fs.writeFileSync(target, content, { encoding: 'utf8', mode: 0o755 });
43 fs.chmodSync(target, 0o755);
44 }
45
40 46 describe('thumbnail cache worker', () =>
41 47 {
42 48 let tempDir;
@@ -69,6 +75,8 @@ describe('thumbnail cache worker', () =>
69 75 {
70 76 delete process.env.FILEMANAGER_PUBLIC_DIR;
71 77 delete process.env.FILEMANAGER_THUMBS_DIR;
78 delete process.env.FFMPEG_PATH;
79 delete process.env.FFPROBE_PATH;
72 80 mockPaths = [];
73 81 try
74 82 {
@@ -200,4 +208,40 @@ describe('thumbnail cache worker', () =>
200 208
201 209 fs.renameSync(offlinePath, sourceRoot);
202 210 });
211
212 test('creates cached thumbnail for video files using ffmpeg frame extraction', async () =>
213 {
214 const mod = loadModule();
215 const toolsDir = path.join(tempDir, 'tools');
216 const ffprobeScript = path.join(toolsDir, 'ffprobe-mock.js');
217 const ffmpegScript = path.join(toolsDir, 'ffmpeg-mock.js');
218 const ffmpegArgsLog = path.join(tempDir, 'ffmpeg-args.log');
219 const sourceFramePng = path.join(tempDir, 'ffmpeg-source-frame.png');
220
221 fs.mkdirSync(toolsDir, { recursive: true });
222 await writeTinyPng(sourceFramePng);
223
224 writeExecutable(ffprobeScript, `#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({ format: { duration: '20' } }));\n`);
225 writeExecutable(ffmpegScript, `#!/usr/bin/env node\nconst fs = require('fs');\nconst args = process.argv.slice(2);\nfs.writeFileSync(${JSON.stringify(ffmpegArgsLog)}, args.join('\\n'), 'utf8');\nconst outPath = args[args.length - 1];\nfs.copyFileSync(${JSON.stringify(sourceFramePng)}, outPath);\n`);
226
227 process.env.FFPROBE_PATH = ffprobeScript;
228 process.env.FFMPEG_PATH = ffmpegScript;
229
230 const videoPath = path.join(sourceRoot, 'clips', 'sample.mp4');
231 fs.mkdirSync(path.dirname(videoPath), { recursive: true });
232 fs.writeFileSync(videoPath, 'fake-video-data');
233
234 await mod.syncThumbnailCacheOnce();
235
236 const thumbPath = mod.getThumbPathForSourceFile(videoPath);
237 expect(fs.existsSync(thumbPath)).toBe(true);
238
239 const info = await mod.getCachedThumbnailInfo(videoPath);
240 expect(info).toBeTruthy();
241 expect(info.thumbPath).toBe(thumbPath);
242
243 const ffmpegArgs = fs.readFileSync(ffmpegArgsLog, 'utf8');
244 expect(ffmpegArgs).toContain('-ss');
245 expect(ffmpegArgs).toContain('2.000');
246 });
203 247 });
@@ -37,6 +37,12 @@ function writeTinySvg (target)
37 fs.writeFileSync(target, svg, 'utf8');
37 fs.writeFileSync(target, svg, 'utf8');
38}
38}
39
39
40function writeExecutable (target, content)
41{
42 fs.writeFileSync(target, content, { encoding: 'utf8', mode: 0o755 });
43 fs.chmodSync(target, 0o755);
44}
45
40describe('thumbnail cache worker', () =>
46describe('thumbnail cache worker', () =>
41{
47{
42 let tempDir;
48 let tempDir;
@@ -69,6 +75,8 @@ describe('thumbnail cache worker', () =>
69 {
75 {
70 delete process.env.FILEMANAGER_PUBLIC_DIR;
76 delete process.env.FILEMANAGER_PUBLIC_DIR;
71 delete process.env.FILEMANAGER_THUMBS_DIR;
77 delete process.env.FILEMANAGER_THUMBS_DIR;
78 delete process.env.FFMPEG_PATH;
79 delete process.env.FFPROBE_PATH;
72 mockPaths = [];
80 mockPaths = [];
73 try
81 try
74 {
82 {
@@ -200,4 +208,40 @@ describe('thumbnail cache worker', () =>
200
208
201 fs.renameSync(offlinePath, sourceRoot);
209 fs.renameSync(offlinePath, sourceRoot);
202 });
210 });
211
212 test('creates cached thumbnail for video files using ffmpeg frame extraction', async () =>
213 {
214 const mod = loadModule();
215 const toolsDir = path.join(tempDir, 'tools');
216 const ffprobeScript = path.join(toolsDir, 'ffprobe-mock.js');
217 const ffmpegScript = path.join(toolsDir, 'ffmpeg-mock.js');
218 const ffmpegArgsLog = path.join(tempDir, 'ffmpeg-args.log');
219 const sourceFramePng = path.join(tempDir, 'ffmpeg-source-frame.png');
220
221 fs.mkdirSync(toolsDir, { recursive: true });
222 await writeTinyPng(sourceFramePng);
223
224 writeExecutable(ffprobeScript, `#!/usr/bin/env node\nprocess.stdout.write(JSON.stringify({ format: { duration: '20' } }));\n`);
225 writeExecutable(ffmpegScript, `#!/usr/bin/env node\nconst fs = require('fs');\nconst args = process.argv.slice(2);\nfs.writeFileSync(${JSON.stringify(ffmpegArgsLog)}, args.join('\\n'), 'utf8');\nconst outPath = args[args.length - 1];\nfs.copyFileSync(${JSON.stringify(sourceFramePng)}, outPath);\n`);
226
227 process.env.FFPROBE_PATH = ffprobeScript;
228 process.env.FFMPEG_PATH = ffmpegScript;
229
230 const videoPath = path.join(sourceRoot, 'clips', 'sample.mp4');
231 fs.mkdirSync(path.dirname(videoPath), { recursive: true });
232 fs.writeFileSync(videoPath, 'fake-video-data');
233
234 await mod.syncThumbnailCacheOnce();
235
236 const thumbPath = mod.getThumbPathForSourceFile(videoPath);
237 expect(fs.existsSync(thumbPath)).toBe(true);
238
239 const info = await mod.getCachedThumbnailInfo(videoPath);
240 expect(info).toBeTruthy();
241 expect(info.thumbPath).toBe(thumbPath);
242
243 const ffmpegArgs = fs.readFileSync(ffmpegArgsLog, 'utf8');
244 expect(ffmpegArgs).toContain('-ss');
245 expect(ffmpegArgs).toContain('2.000');
246 });
203});
247});