'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 ACTIVE_STREAM_LOOP_DELAY_MS = toPositiveInt(process.env.THUMBNAIL_CACHE_ACTIVE_STREAM_DELAY_MS, 120000);
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',
]);
const MODEL_EXTS = new Set(['stl', 'obj']);
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 isModelPath (filePath)
{
const ext = path.extname(filePath).slice(1).toLowerCase();
return MODEL_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}.`));
});
});
}
function toPositiveInt (value, fallback)
{
const parsed = Number.parseInt(String(value || ''), 10);
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
}
function activeThumbnailLoopDelayMs ()
{
try
{
const stream = require('./video-stream');
const count = Number(stream && typeof stream.getActiveStreamCount === 'function'
? stream.getActiveStreamCount()
: 0);
if (Number.isFinite(count) && count > 0)
{
return Math.max(LOOP_DELAY_MS, ACTIVE_STREAM_LOOP_DELAY_MS);
}
}
catch
{
// Fallback to normal cadence if stream state is unavailable.
}
return LOOP_DELAY_MS;
}
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);
}
}
// ---------------------------------------------------------------------------
// 3D model geometry parsing and thumbnail generation
// ---------------------------------------------------------------------------
function projectModelVector (v)
{
const ry = Math.PI / 4;
const rx = -Math.PI / 5;
const x1 = v.x * Math.cos(ry) + v.z * Math.sin(ry);
const z1 = -v.x * Math.sin(ry) + v.z * Math.cos(ry);
const y2 = v.y * Math.cos(rx) - z1 * Math.sin(rx);
return { x: x1, y: y2, z: v.y * Math.sin(rx) + z1 * Math.cos(rx) };
}
function parseAsciiStlPolygons (text)
{
const polygons = [];
const vertices = [];
const lines = String(text || '').replace(/\r\n/g, '\n').split('\n');
for (const lineRaw of lines)
{
const line = lineRaw.trim();
if (!/^vertex\s+/i.test(line)) continue;
const parts = line.split(/\s+/);
if (parts.length < 4) continue;
const x = Number(parts[1]);
const y = Number(parts[2]);
const z = Number(parts[3]);
if (Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z))
{
vertices.push({ x, y, z });
}
}
for (let i = 0; i + 2 < vertices.length; i += 3)
{
polygons.push({ points: [vertices[i], vertices[i + 1], vertices[i + 2]] });
}
return polygons;
}
function parseBinaryStlPolygons (buf)
{
const polygons = [];
if (!buf || buf.length < 84) return polygons;
const triCount = buf.readUInt32LE(80);
let offset = 84;
for (let i = 0; i < triCount; i++)
{
if (offset + 50 > buf.length) break;
offset += 12; // skip normal
const points = [];
for (let v = 0; v < 3; v++)
{
const x = buf.readFloatLE(offset);
const y = buf.readFloatLE(offset + 4);
const z = buf.readFloatLE(offset + 8);
points.push({ x, y, z });
offset += 12;
}
offset += 2; // attribute byte count
polygons.push({ points });
}
return polygons;
}
function resolveObjIndex (token, vertexCount)
{
const raw = Number.parseInt(String(token || '').split('/')[0], 10);
if (!Number.isFinite(raw) || raw === 0) return null;
const index = raw < 0 ? vertexCount + raw : raw - 1;
if (index < 0 || index >= vertexCount) return null;
return index;
}
function parseObjPolygons (text)
{
const vertices = [];
const faces = [];
const lines = String(text || '').replace(/\r\n/g, '\n').split('\n');
for (const lineRaw of lines)
{
const line = lineRaw.trim();
if (!line || line.startsWith('#')) continue;
if (/^v\s+/i.test(line))
{
const parts = line.split(/\s+/);
if (parts.length >= 4)
{
const x = Number(parts[1]);
const y = Number(parts[2]);
const z = Number(parts[3]);
if (Number.isFinite(x) && Number.isFinite(y) && Number.isFinite(z))
{
vertices.push({ x, y, z });
}
}
}
else if (/^f\s+/i.test(line))
{
const parts = line.split(/\s+/).slice(1);
if (parts.length >= 3) faces.push(parts);
}
}
const polygons = [];
for (const face of faces)
{
const indices = face
.map((t) => resolveObjIndex(t, vertices.length))
.filter((i) => i != null);
if (indices.length < 3) continue;
const anchor = vertices[indices[0]];
for (let i = 1; i < indices.length - 1; i++)
{
const b = vertices[indices[i]];
const c = vertices[indices[i + 1]];
if (anchor && b && c) polygons.push({ points: [anchor, b, c] });
}
}
return polygons;
}
async function parseModelPolygons (sourceFile, ext)
{
const buf = await fs.promises.readFile(sourceFile);
if (ext === 'obj') return parseObjPolygons(buf.toString('utf8'));
// STL: probe first 256 bytes for ASCII keyword
const probe = buf.toString('utf8', 0, Math.min(buf.length, 256));
if (/facet/i.test(probe)) return parseAsciiStlPolygons(buf.toString('utf8'));
return parseBinaryStlPolygons(buf);
}
function createModelSvgBuffer (polygons)
{
const width = 160;
const height = 200;
const pad = 12;
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const poly of polygons)
{
for (const pt of poly.points)
{
if (pt.x < minX) minX = pt.x;
if (pt.y < minY) minY = pt.y;
if (pt.x > maxX) maxX = pt.x;
if (pt.y > maxY) maxY = pt.y;
}
}
if (!Number.isFinite(minX)) return null;
const boxW = Math.max(1, maxX - minX);
const boxH = Math.max(1, maxY - minY);
const scale = Math.min((width - pad * 2) / boxW, (height - pad * 2 - 18) / boxH);
const cx = (minX + maxX) / 2;
const cy = (minY + maxY) / 2;
const svgPolys = polygons.map((poly) =>
{
const pts = poly.points.map((pt) =>
{
const x = width / 2 + (pt.x - cx) * scale;
const y = 18 + height / 2 + (pt.y - cy) * scale;
return `${x.toFixed(1)},${y.toFixed(1)}`;
}).join(' ');
return ``;
}).join('');
return Buffer.from(
``,
'utf8',
);
}
async function generateModelThumb (sourceFile, thumbPath)
{
const ext = path.extname(sourceFile).slice(1).toLowerCase();
const rawPolygons = await parseModelPolygons(sourceFile, ext);
if (!rawPolygons.length) throw new Error('No geometry found in model file');
const polygons = rawPolygons.map((poly) =>
{
const projected = poly.points.map(projectModelVector);
return {
points: projected.map((p) => ({ x: p.x, y: p.y })),
depth: projected.reduce((s, p) => s + p.z, 0) / projected.length,
};
});
polygons.sort((a, b) => a.depth - b.depth);
const svgBuf = createModelSvgBuffer(polygons);
if (!svgBuf) throw new Error('Failed to create SVG for model');
await ensureDir(path.dirname(thumbPath));
await sharp(svgBuf, { density: 144 })
.resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true })
.webp({ quality: 80 })
.toFile(thumbPath);
}
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'
: isModelPath(filePath)
? 'model'
: 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;
}
if (kind === 'video')
{
try
{
await generateVideoThumb(filePath, thumbPath);
} catch
{
return;
}
nextManifest.files[filePath] = {
sha256: null,
size: stat.size,
mtimeMs: stat.mtimeMs,
thumbRelPath,
kind,
};
return;
}
// kind === 'model'
try
{
await generateModelThumb(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(activeThumbnailLoopDelayMs());
}
}, delayMs);
if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref();
}
function startThumbnailCacheLoop ()
{
if (loopStarted) return;
loopStarted = true;
scheduleNextRun(activeThumbnailLoopDelayMs());
}
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 files inside `dirPath` (recursive) that have a cached thumbnail,
* sorted lexicographically by absolute file path.
* Each element: { filePath: string, thumbRelPath: string }
*/
async function getFilesWithThumbnailsInDir (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;
}
// Backward-compatible alias used by existing imports/tests.
async function getImagesWithThumbnailsInDir (dirPath)
{
return getFilesWithThumbnailsInDir(dirPath);
}
/**
* Returns the source 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 files 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 files = await getFilesWithThumbnailsInDir(folderPath);
return files.length ? files[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,
getFilesWithThumbnailsInDir,
getImagesWithThumbnailsInDir,
getFolderIconSourceFile,
setFolderIconAssignment,
startThumbnailCacheLoop,
stopThumbnailCacheLoop,
syncThumbnailCacheOnce,
};