'use strict'; const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const { spawn } = require('child_process'); const sharp = require('sharp'); const { loadConfig, getEffectivePaths } = require('./config'); const PUBLIC_DIR = path.resolve(process.env.FILEMANAGER_PUBLIC_DIR || path.join(__dirname, '../public')); const THUMBS_DIR = path.resolve(process.env.FILEMANAGER_THUMBS_DIR || path.join(PUBLIC_DIR, 'thumbs')); const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json'); const FOLDER_ICONS_PATH = path.join(THUMBS_DIR, '.folder-icons.json'); const LOOP_DELAY_MS = 5000; const IMAGE_EXTS = new Set([ 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico', ]); const VIDEO_EXTS = new Set([ 'mp4', 'mkv', 'mov', 'avi', 'webm', 'ogv', 'm4v', 'ts', 'mpeg', 'mpg', ]); let loopTimer = null; let loopStarted = false; function toPosix (p) { return String(p || '').split(path.sep).join('/'); } function rootSegmentsForSourcePath (sourcePath) { const resolved = path.resolve(sourcePath); const stripped = resolved .replace(/^\\+/, '') .replace(/^\/+/, '') .replace(/^([A-Za-z]):/, '$1'); return stripped .split(/[\\/]+/) .filter(Boolean) .map(seg => seg.replace(/:/g, '_')); } function getThumbDirForSourceDir (sourceDir) { return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(sourceDir)); } function getThumbPathForSourceFile (sourceFile) { const source = path.resolve(sourceFile); return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(source)) + '.webp'; } function getThumbRelPathForSourceFile (sourceFile) { return toPosix(path.relative(PUBLIC_DIR, getThumbPathForSourceFile(sourceFile))); } function isImagePath (filePath) { const ext = path.extname(filePath).slice(1).toLowerCase(); return IMAGE_EXTS.has(ext); } function isVideoPath (filePath) { const ext = path.extname(filePath).slice(1).toLowerCase(); return VIDEO_EXTS.has(ext); } function ffmpegPath () { return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg'; } function ffprobePath () { return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe'; } function runCommand (command, args) { return new Promise((resolve, reject) => { const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] }); let stdout = ''; let stderr = ''; child.stdout.on('data', (chunk) => { stdout += chunk.toString(); }); child.stderr.on('data', (chunk) => { stderr += chunk.toString(); }); child.on('error', (err) => { if (err && err.code === 'ENOENT') { reject(new Error(`${command} is not installed or not available in PATH.`)); return; } reject(err); }); child.on('close', (code) => { if (code === 0) { resolve({ stdout, stderr }); return; } reject(new Error(stderr.trim() || `${command} exited with code ${code}.`)); }); }); } async function probeDurationSeconds (sourceFile) { const { stdout } = await runCommand(ffprobePath(), [ '-v', 'error', '-print_format', 'json', '-show_format', sourceFile, ]); let payload; try { payload = JSON.parse(stdout || '{}'); } catch { return null; } const duration = Number(payload && payload.format && payload.format.duration); if (!Number.isFinite(duration) || duration <= 0) return null; return duration; } async function exists (target) { try { await fs.promises.access(target, fs.constants.F_OK); return true; } catch { return false; } } async function ensureDir (dirPath) { await fs.promises.mkdir(dirPath, { recursive: true }); } async function readManifest () { try { const raw = await fs.promises.readFile(MANIFEST_PATH, 'utf8'); const parsed = JSON.parse(raw); if (parsed && typeof parsed === 'object' && parsed.files && typeof parsed.files === 'object') { return parsed; } } catch { // Missing or malformed manifests are treated as empty. } return { version: 1, files: {} }; } async function writeManifest (manifest) { const payload = JSON.stringify(manifest, null, 2); const tmp = MANIFEST_PATH + '.tmp'; await fs.promises.writeFile(tmp, payload, 'utf8'); await fs.promises.rename(tmp, MANIFEST_PATH); } function sha256File (target) { return new Promise((resolve, reject) => { const hash = crypto.createHash('sha256'); const stream = fs.createReadStream(target); stream.on('error', reject); stream.on('data', chunk => hash.update(chunk)); stream.on('end', () => resolve(hash.digest('hex'))); }); } async function walkDir (dirPath, onDir, onFile) { await onDir(dirPath); let entries = []; try { entries = await fs.promises.readdir(dirPath, { withFileTypes: true }); } catch { return; } for (const entry of entries) { const full = path.join(dirPath, entry.name); if (entry.isDirectory()) { await walkDir(full, onDir, onFile); continue; } if (entry.isFile()) { await onFile(full); } } } async function removeFileIfExists (target) { try { await fs.promises.unlink(target); } catch (err) { if (err && err.code !== 'ENOENT') throw err; } } async function pruneThumbTree (dirPath, keepFiles) { let entries = []; try { entries = await fs.promises.readdir(dirPath, { withFileTypes: true }); } catch { return; } for (const entry of entries) { const full = path.join(dirPath, entry.name); if (entry.isDirectory()) { await pruneThumbTree(full, keepFiles); continue; } if (full === MANIFEST_PATH || full === MANIFEST_PATH + '.tmp') continue; if (full === FOLDER_ICONS_PATH || full === FOLDER_ICONS_PATH + '.tmp') continue; if (!keepFiles.has(path.resolve(full))) { await removeFileIfExists(full); } } // Never remove the root thumbs directory. if (path.resolve(dirPath) === path.resolve(THUMBS_DIR)) return; try { const leftovers = await fs.promises.readdir(dirPath); if (leftovers.length === 0) await fs.promises.rmdir(dirPath); } catch { // Ignore races while pruning. } } async function generateImageThumb (sourceFile, thumbPath) { await ensureDir(path.dirname(thumbPath)); await sharp(sourceFile) .rotate() .resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true }) .webp({ quality: 80 }) .toFile(thumbPath); } async function generateVideoThumb (sourceFile, thumbPath) { await ensureDir(path.dirname(thumbPath)); const duration = await probeDurationSeconds(sourceFile).catch(() => null); let captureAt = 1; if (duration && duration > 0) { captureAt = duration * 0.1; if (duration > 0.2) captureAt = Math.min(captureAt, duration - 0.1); captureAt = Math.max(0, captureAt); } const tmpPngPath = thumbPath + '.tmp.png'; try { await runCommand(ffmpegPath(), [ '-hide_banner', '-loglevel', 'error', '-y', '-ss', captureAt.toFixed(3), '-i', sourceFile, '-frames:v', '1', '-an', '-sn', tmpPngPath, ]); await sharp(tmpPngPath) .rotate() .resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true }) .webp({ quality: 80 }) .toFile(thumbPath); } finally { await removeFileIfExists(tmpPngPath); } } function effectiveRoots () { const config = loadConfig(); const roots = getEffectivePaths(config.paths || []); // Include the personal-user root tree so thumbnails are generated for // files stored under userPath//... as well. const userPath = typeof config.userPath === 'string' ? config.userPath.trim() : ''; if (userPath) roots.push(userPath); // De-duplicate roots by resolved absolute path. const seen = new Set(); const deduped = []; for (const root of roots) { const key = path.resolve(root); if (seen.has(key)) continue; seen.add(key); deduped.push(root); } return deduped; } function pathInsideOrEqualResolved (targetPath, rootPath) { const rel = path.relative(path.resolve(rootPath), path.resolve(targetPath)); return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel)); } function preserveRootFromPreviousManifest (rootPath, prevManifest, nextManifest, keepThumbFiles) { for (const [sourcePath, meta] of Object.entries(prevManifest.files || {})) { if (!pathInsideOrEqualResolved(sourcePath, rootPath)) continue; nextManifest.files[sourcePath] = meta; if (meta && meta.thumbRelPath) { keepThumbFiles.add(path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath))); } } } async function syncThumbnailCacheOnce () { await ensureDir(THUMBS_DIR); const prevManifest = await readManifest(); const nextManifest = { version: 1, files: {} }; const keepThumbFiles = new Set(); const roots = effectiveRoots(); for (const root of roots) { const resolvedRoot = path.resolve(root); if (!(await exists(resolvedRoot))) { // Keep prior cache for roots that are temporarily offline (e.g. removable mounts). preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles); continue; } try { const stat = await fs.promises.stat(resolvedRoot); if (!stat.isDirectory()) { preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles); continue; } // Probe readability; failures should not wipe existing cache state. await fs.promises.readdir(resolvedRoot); } catch { preserveRootFromPreviousManifest(resolvedRoot, prevManifest, nextManifest, keepThumbFiles); continue; } await walkDir( resolvedRoot, async (dirPath) => { await ensureDir(getThumbDirForSourceDir(dirPath)); }, async (filePath) => { const kind = isImagePath(filePath) ? 'image' : isVideoPath(filePath) ? 'video' : null; if (!kind) return; let stat; try { stat = await fs.promises.stat(filePath); } catch { return; } const thumbPath = getThumbPathForSourceFile(filePath); const thumbRelPath = getThumbRelPathForSourceFile(filePath); keepThumbFiles.add(path.resolve(thumbPath)); const prev = prevManifest.files[filePath]; const unchangedByStat = prev && Number(prev.size) === Number(stat.size) && Number(prev.mtimeMs) === Number(stat.mtimeMs) && prev.thumbRelPath === thumbRelPath && (prev.kind === kind || !prev.kind); if (unchangedByStat && await exists(thumbPath)) { nextManifest.files[filePath] = { ...prev, kind, }; return; } if (kind === 'image') { let sha256; try { sha256 = await sha256File(filePath); } catch { return; } const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath); if (!unchangedByHash) { try { await generateImageThumb(filePath, thumbPath); } catch { return; } } nextManifest.files[filePath] = { sha256, size: stat.size, mtimeMs: stat.mtimeMs, thumbRelPath, kind, }; return; } try { await generateVideoThumb(filePath, thumbPath); } catch { return; } nextManifest.files[filePath] = { sha256: null, size: stat.size, mtimeMs: stat.mtimeMs, thumbRelPath, kind, }; }, ); } for (const [sourcePath, oldMeta] of Object.entries(prevManifest.files || {})) { if (nextManifest.files[sourcePath]) continue; const oldThumbRel = oldMeta && oldMeta.thumbRelPath; if (!oldThumbRel) continue; const oldThumbPath = path.resolve(path.join(PUBLIC_DIR, oldThumbRel)); await removeFileIfExists(oldThumbPath); } await pruneThumbTree(THUMBS_DIR, keepThumbFiles); await writeManifest(nextManifest); return { rootsScanned: roots.length, cachedCount: Object.keys(nextManifest.files).length, }; } function scheduleNextRun (delayMs) { loopTimer = setTimeout(async () => { loopTimer = null; try { await syncThumbnailCacheOnce(); } catch (err) { console.error('[thumb-cache] sync failed:', err && err.message ? err.message : err); } finally { if (loopStarted) scheduleNextRun(LOOP_DELAY_MS); } }, delayMs); if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref(); } function startThumbnailCacheLoop () { if (loopStarted) return; loopStarted = true; scheduleNextRun(LOOP_DELAY_MS); } function stopThumbnailCacheLoop () { loopStarted = false; if (loopTimer) { clearTimeout(loopTimer); loopTimer = null; } } async function getCachedThumbnailInfo (sourceFile) { const thumbPath = getThumbPathForSourceFile(sourceFile); if (!(await exists(thumbPath))) return null; return { thumbPath, contentType: 'image/webp', }; } // --------------------------------------------------------------------------- // Folder icon assignments — stored in .folder-icons.json // --------------------------------------------------------------------------- async function readFolderIcons () { try { const raw = await fs.promises.readFile(FOLDER_ICONS_PATH, 'utf8'); const parsed = JSON.parse(raw); if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return parsed; } catch { // Missing or malformed — treat as empty. } return {}; } async function writeFolderIcons (obj) { await ensureDir(THUMBS_DIR); const payload = JSON.stringify(obj, null, 2); const tmp = FOLDER_ICONS_PATH + '.tmp'; await fs.promises.writeFile(tmp, payload, 'utf8'); await fs.promises.rename(tmp, FOLDER_ICONS_PATH); } /** * Returns all images inside `dirPath` (recursive) that have a cached thumbnail, * sorted lexicographically by absolute file path. * Each element: { filePath: string, thumbRelPath: string } */ async function getImagesWithThumbnailsInDir (dirPath) { const manifest = await readManifest(); const resolved = path.resolve(dirPath); const results = []; for (const [filePath, meta] of Object.entries(manifest.files || {})) { const resolvedFile = path.resolve(filePath); const relative = path.relative(resolved, resolvedFile); if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) continue; if (!meta || !meta.thumbRelPath) continue; // Verify the thumbnail file actually exists on disk. const thumbPath = path.resolve(path.join(PUBLIC_DIR, meta.thumbRelPath)); if (!(await exists(thumbPath))) continue; results.push({ filePath, thumbRelPath: meta.thumbRelPath }); } results.sort((a, b) => a.filePath.localeCompare(b.filePath)); return results; } /** * Returns the source image file path used as the icon for `folderPath`. * Looks up the explicit assignment first; falls back to the first cached image found. * Returns null when no images with cached thumbnails exist inside the folder. */ async function getFolderIconSourceFile (folderPath) { const resolved = path.resolve(folderPath); const icons = await readFolderIcons(); if (icons[resolved]) { const assigned = icons[resolved]; // Verify the assigned image still has a cached thumbnail. const thumbPath = getThumbPathForSourceFile(assigned); if (await exists(thumbPath)) return assigned; // Stale — fall through to auto-detect. } // Auto: first cached image inside the folder. const images = await getImagesWithThumbnailsInDir(folderPath); return images.length ? images[0].filePath : null; } /** * Saves an explicit folder icon assignment. * Pass `imagePath = null` to clear the explicit assignment (auto-detect will be used instead). */ async function setFolderIconAssignment (folderPath, imagePath) { const resolved = path.resolve(folderPath); const icons = await readFolderIcons(); if (imagePath === null || imagePath === undefined) { delete icons[resolved]; } else { icons[resolved] = imagePath; } await writeFolderIcons(icons); } module.exports = { getCachedThumbnailInfo, getThumbPathForSourceFile, getThumbRelPathForSourceFile, getImagesWithThumbnailsInDir, getFolderIconSourceFile, setFolderIconAssignment, startThumbnailCacheLoop, stopThumbnailCacheLoop, syncThumbnailCacheOnce, };