| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const crypto = require('crypto'); |
| 6 | const sharp = require('sharp'); |
| 7 | |
| 8 | const { loadConfig, getEffectivePaths } = require('./config'); |
| 9 | |
| 10 | const PUBLIC_DIR = path.resolve(process.env.FILEMANAGER_PUBLIC_DIR || path.join(__dirname, '../public')); |
| 11 | const THUMBS_DIR = path.resolve(process.env.FILEMANAGER_THUMBS_DIR || path.join(PUBLIC_DIR, 'thumbs')); |
| 12 | const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json'); |
| 13 | const LOOP_DELAY_MS = 5000; |
| 14 | |
| 15 | const IMAGE_EXTS = new Set([ |
| 16 | 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', |
| 17 | ]); |
| 18 | |
| 19 | let loopTimer = null; |
| 20 | let loopStarted = false; |
| 21 | |
| 22 | function toPosix (p) |
| 23 | { |
| 24 | return String(p || '').split(path.sep).join('/'); |
| 25 | } |
| 26 | |
| 27 | function rootSegmentsForSourcePath (sourcePath) |
| 28 | { |
| 29 | const resolved = path.resolve(sourcePath); |
| 30 | const stripped = resolved |
| 31 | .replace(/^\\+/, '') |
| 32 | .replace(/^\/+/, '') |
| 33 | .replace(/^([A-Za-z]):/, '$1'); |
| 34 | |
| 35 | return stripped |
| 36 | .split(/[\\/]+/) |
| 37 | .filter(Boolean) |
| 38 | .map(seg => seg.replace(/:/g, '_')); |
| 39 | } |
| 40 | |
| 41 | function getThumbDirForSourceDir (sourceDir) |
| 42 | { |
| 43 | return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(sourceDir)); |
| 44 | } |
| 45 | |
| 46 | function getThumbPathForSourceFile (sourceFile) |
| 47 | { |
| 48 | const source = path.resolve(sourceFile); |
| 49 | return path.join(THUMBS_DIR, ...rootSegmentsForSourcePath(source)) + '.webp'; |
| 50 | } |
| 51 | |
| 52 | function getThumbRelPathForSourceFile (sourceFile) |
| 53 | { |
| 54 | return toPosix(path.relative(PUBLIC_DIR, getThumbPathForSourceFile(sourceFile))); |
| 55 | } |
| 56 | |
| 57 | function isImagePath (filePath) |
| 58 | { |
| 59 | const ext = path.extname(filePath).slice(1).toLowerCase(); |
| 60 | return IMAGE_EXTS.has(ext); |
| 61 | } |
| 62 | |
| 63 | async function exists (target) |
| 64 | { |
| 65 | try |
| 66 | { |
| 67 | await fs.promises.access(target, fs.constants.F_OK); |
| 68 | return true; |
| 69 | } catch |
| 70 | { |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | async function ensureDir (dirPath) |
| 76 | { |
| 77 | await fs.promises.mkdir(dirPath, { recursive: true }); |
| 78 | } |
| 79 | |
| 80 | async function readManifest () |
| 81 | { |
| 82 | try |
| 83 | { |
| 84 | const raw = await fs.promises.readFile(MANIFEST_PATH, 'utf8'); |
| 85 | const parsed = JSON.parse(raw); |
| 86 | if (parsed && typeof parsed === 'object' && parsed.files && typeof parsed.files === 'object') |
| 87 | { |
| 88 | return parsed; |
| 89 | } |
| 90 | } catch |
| 91 | { |
| 92 | // Missing or malformed manifests are treated as empty. |
| 93 | } |
| 94 | return { version: 1, files: {} }; |
| 95 | } |
| 96 | |
| 97 | async function writeManifest (manifest) |
| 98 | { |
| 99 | const payload = JSON.stringify(manifest, null, 2); |
| 100 | const tmp = MANIFEST_PATH + '.tmp'; |
| 101 | await fs.promises.writeFile(tmp, payload, 'utf8'); |
| 102 | await fs.promises.rename(tmp, MANIFEST_PATH); |
| 103 | } |
| 104 | |
| 105 | function sha256File (target) |
| 106 | { |
| 107 | return new Promise((resolve, reject) => |
| 108 | { |
| 109 | const hash = crypto.createHash('sha256'); |
| 110 | const stream = fs.createReadStream(target); |
| 111 | stream.on('error', reject); |
| 112 | stream.on('data', chunk => hash.update(chunk)); |
| 113 | stream.on('end', () => resolve(hash.digest('hex'))); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | async function walkDir (dirPath, onDir, onFile) |
| 118 | { |
| 119 | await onDir(dirPath); |
| 120 | |
| 121 | let entries = []; |
| 122 | try |
| 123 | { |
| 124 | entries = await fs.promises.readdir(dirPath, { withFileTypes: true }); |
| 125 | } catch |
| 126 | { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | for (const entry of entries) |
| 131 | { |
| 132 | const full = path.join(dirPath, entry.name); |
| 133 | if (entry.isDirectory()) |
| 134 | { |
| 135 | await walkDir(full, onDir, onFile); |
| 136 | continue; |
| 137 | } |
| 138 | if (entry.isFile()) |
| 139 | { |
| 140 | await onFile(full); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | async function removeFileIfExists (target) |
| 146 | { |
| 147 | try |
| 148 | { |
| 149 | await fs.promises.unlink(target); |
| 150 | } catch (err) |
| 151 | { |
| 152 | if (err && err.code !== 'ENOENT') throw err; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | async function pruneThumbTree (dirPath, keepFiles) |
| 157 | { |
| 158 | let entries = []; |
| 159 | try |
| 160 | { |
| 161 | entries = await fs.promises.readdir(dirPath, { withFileTypes: true }); |
| 162 | } catch |
| 163 | { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | for (const entry of entries) |
| 168 | { |
| 169 | const full = path.join(dirPath, entry.name); |
| 170 | if (entry.isDirectory()) |
| 171 | { |
| 172 | await pruneThumbTree(full, keepFiles); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | if (full === MANIFEST_PATH || full === MANIFEST_PATH + '.tmp') continue; |
| 177 | if (!keepFiles.has(path.resolve(full))) |
| 178 | { |
| 179 | await removeFileIfExists(full); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Never remove the root thumbs directory. |
| 184 | if (path.resolve(dirPath) === path.resolve(THUMBS_DIR)) return; |
| 185 | |
| 186 | try |
| 187 | { |
| 188 | const leftovers = await fs.promises.readdir(dirPath); |
| 189 | if (leftovers.length === 0) await fs.promises.rmdir(dirPath); |
| 190 | } catch |
| 191 | { |
| 192 | // Ignore races while pruning. |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | async function generateImageThumb (sourceFile, thumbPath) |
| 197 | { |
| 198 | await ensureDir(path.dirname(thumbPath)); |
| 199 | await sharp(sourceFile) |
| 200 | .rotate() |
| 201 | .resize({ width: 99, height: 99, fit: 'inside', withoutEnlargement: true }) |
| 202 | .webp({ quality: 80 }) |
| 203 | .toFile(thumbPath); |
| 204 | } |
| 205 | |
| 206 | function effectiveRoots () |
| 207 | { |
| 208 | return getEffectivePaths(loadConfig().paths || []); |
| 209 | } |
| 210 | |
| 211 | async function syncThumbnailCacheOnce () |
| 212 | { |
| 213 | await ensureDir(THUMBS_DIR); |
| 214 | |
| 215 | const prevManifest = await readManifest(); |
| 216 | const nextManifest = { version: 1, files: {} }; |
| 217 | const keepThumbFiles = new Set(); |
| 218 | const roots = effectiveRoots(); |
| 219 | |
| 220 | for (const root of roots) |
| 221 | { |
| 222 | const resolvedRoot = path.resolve(root); |
| 223 | if (!(await exists(resolvedRoot))) continue; |
| 224 | |
| 225 | await walkDir( |
| 226 | resolvedRoot, |
| 227 | async (dirPath) => |
| 228 | { |
| 229 | await ensureDir(getThumbDirForSourceDir(dirPath)); |
| 230 | }, |
| 231 | async (filePath) => |
| 232 | { |
| 233 | if (!isImagePath(filePath)) return; |
| 234 | |
| 235 | let stat; |
| 236 | try |
| 237 | { |
| 238 | stat = await fs.promises.stat(filePath); |
| 239 | } catch |
| 240 | { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | const thumbPath = getThumbPathForSourceFile(filePath); |
| 245 | const thumbRelPath = getThumbRelPathForSourceFile(filePath); |
| 246 | keepThumbFiles.add(path.resolve(thumbPath)); |
| 247 | |
| 248 | const prev = prevManifest.files[filePath]; |
| 249 | const unchangedByStat = |
| 250 | prev && |
| 251 | Number(prev.size) === Number(stat.size) && |
| 252 | Number(prev.mtimeMs) === Number(stat.mtimeMs) && |
| 253 | prev.thumbRelPath === thumbRelPath; |
| 254 | |
| 255 | if (unchangedByStat && await exists(thumbPath)) |
| 256 | { |
| 257 | nextManifest.files[filePath] = prev; |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | let sha256; |
| 262 | try |
| 263 | { |
| 264 | sha256 = await sha256File(filePath); |
| 265 | } catch |
| 266 | { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | const unchangedByHash = prev && prev.sha256 === sha256 && await exists(thumbPath); |
| 271 | if (!unchangedByHash) |
| 272 | { |
| 273 | try |
| 274 | { |
| 275 | await generateImageThumb(filePath, thumbPath); |
| 276 | } catch |
| 277 | { |
| 278 | return; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | nextManifest.files[filePath] = { |
| 283 | sha256, |
| 284 | size: stat.size, |
| 285 | mtimeMs: stat.mtimeMs, |
| 286 | thumbRelPath, |
| 287 | }; |
| 288 | }, |
| 289 | ); |
| 290 | } |
| 291 | |
| 292 | for (const [sourcePath, oldMeta] of Object.entries(prevManifest.files || {})) |
| 293 | { |
| 294 | if (nextManifest.files[sourcePath]) continue; |
| 295 | const oldThumbRel = oldMeta && oldMeta.thumbRelPath; |
| 296 | if (!oldThumbRel) continue; |
| 297 | const oldThumbPath = path.resolve(path.join(PUBLIC_DIR, oldThumbRel)); |
| 298 | await removeFileIfExists(oldThumbPath); |
| 299 | } |
| 300 | |
| 301 | await pruneThumbTree(THUMBS_DIR, keepThumbFiles); |
| 302 | await writeManifest(nextManifest); |
| 303 | |
| 304 | return { |
| 305 | rootsScanned: roots.length, |
| 306 | cachedCount: Object.keys(nextManifest.files).length, |
| 307 | }; |
| 308 | } |
| 309 | |
| 310 | function scheduleNextRun (delayMs) |
| 311 | { |
| 312 | loopTimer = setTimeout(async () => |
| 313 | { |
| 314 | loopTimer = null; |
| 315 | try |
| 316 | { |
| 317 | await syncThumbnailCacheOnce(); |
| 318 | } |
| 319 | catch (err) |
| 320 | { |
| 321 | console.error('[thumb-cache] sync failed:', err && err.message ? err.message : err); |
| 322 | } |
| 323 | finally |
| 324 | { |
| 325 | if (loopStarted) scheduleNextRun(LOOP_DELAY_MS); |
| 326 | } |
| 327 | }, delayMs); |
| 328 | |
| 329 | if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref(); |
| 330 | } |
| 331 | |
| 332 | function startThumbnailCacheLoop () |
| 333 | { |
| 334 | if (loopStarted) return; |
| 335 | loopStarted = true; |
| 336 | scheduleNextRun(LOOP_DELAY_MS); |
| 337 | } |
| 338 | |
| 339 | function stopThumbnailCacheLoop () |
| 340 | { |
| 341 | loopStarted = false; |
| 342 | if (loopTimer) |
| 343 | { |
| 344 | clearTimeout(loopTimer); |
| 345 | loopTimer = null; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | async function getCachedThumbnailInfo (sourceFile) |
| 350 | { |
| 351 | const thumbPath = getThumbPathForSourceFile(sourceFile); |
| 352 | if (!(await exists(thumbPath))) return null; |
| 353 | return { |
| 354 | thumbPath, |
| 355 | contentType: 'image/webp', |
| 356 | }; |
| 357 | } |
| 358 | |
| 359 | module.exports = { |
| 360 | getCachedThumbnailInfo, |
| 361 | getThumbPathForSourceFile, |
| 362 | getThumbRelPathForSourceFile, |
| 363 | startThumbnailCacheLoop, |
| 364 | stopThumbnailCacheLoop, |
| 365 | syncThumbnailCacheOnce, |
| 366 | }; |