'use strict'; const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const { getDb } = require('./db'); const MAX_QUEUE_LENGTH = toPositiveInt(process.env.FILE_HASH_QUEUE_MAX_LENGTH, 4096); const BACKLOG_BATCH_SIZE = toPositiveInt(process.env.FILE_HASH_BACKLOG_BATCH_SIZE, 500); const ACTIVE_STREAM_DELAY_MS = toPositiveInt(process.env.FILE_HASH_ACTIVE_STREAM_DELAY_MS, 15000); const IDLE_TICK_MS = toPositiveInt(process.env.FILE_HASH_IDLE_TICK_MS, 250); const queue = []; const queued = new Set(); let loopStarted = false; let loopTimer = null; let workerRunning = false; function toPositiveInt (value, fallback) { const parsed = Number.parseInt(String(value || ''), 10); return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback; } function delay (ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } /** * Computes the SHA-256 hex digest of the file at `filePath`. * Streams the file so it works for large files without loading them into memory. */ function computeFileSha256 (filePath) { return new Promise((resolve, reject) => { const hash = crypto.createHash('sha256'); const stream = fs.createReadStream(filePath); stream.on('error', reject); stream.on('data', (chunk) => hash.update(chunk)); stream.on('end', () => resolve(hash.digest('hex'))); }); } function hasActiveVideoStreams () { try { const { getActiveStreamCount } = require('./video-stream'); return Number(getActiveStreamCount()) > 0; } catch { return false; } } function loopDelayMs () { return hasActiveVideoStreams() ? ACTIVE_STREAM_DELAY_MS : IDLE_TICK_MS; } function enqueueFileHash (filePath) { const resolved = path.resolve(String(filePath || '')); if (!resolved) return false; if (queued.has(resolved)) return false; if (queue.length >= MAX_QUEUE_LENGTH) { const dropped = queue.shift(); if (dropped) queued.delete(dropped); } queued.add(resolved); queue.push(resolved); scheduleWorker(); return true; } function persistFileHash (filePath, sha256) { const db = getDb(); const existing = db.prepare('SELECT file_path FROM file_meta WHERE file_path = ?').get([filePath]); if (existing) { db.prepare('UPDATE file_meta SET sha256 = ? WHERE file_path = ?').run([sha256, filePath]); return; } let uploadedAt = null; try { const stat = fs.statSync(filePath); const birthtimeMs = Number(stat.birthtimeMs); const ctimeMs = Number(stat.ctimeMs); const mtimeMs = Number(stat.mtimeMs); const chosenMs = (Number.isFinite(birthtimeMs) && birthtimeMs > 0) ? birthtimeMs : (Number.isFinite(ctimeMs) && ctimeMs > 0) ? ctimeMs : (Number.isFinite(mtimeMs) && mtimeMs > 0) ? mtimeMs : null; if (Number.isFinite(chosenMs) && chosenMs > 0) { uploadedAt = new Date(chosenMs).toISOString(); } } catch { // Ignore missing files. } db.prepare(` INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT(file_path) DO UPDATE SET sha256 = excluded.sha256 `).run([filePath, '', '[]', '{}', uploadedAt, sha256]); } async function processQueuedFile (filePath) { let stat; try { stat = await fs.promises.stat(filePath); } catch { return; } if (!stat.isFile()) return; const sha256 = await computeFileSha256(filePath); persistFileHash(filePath, sha256); } async function processQueueBatch (respectStreamDelay) { while (queue.length) { const filePath = queue.shift(); queued.delete(filePath); if (!filePath) continue; try { await processQueuedFile(filePath); } catch (err) { console.error('[file-hash] hash failed:', filePath, err && err.message ? err.message : err); } if (queue.length && respectStreamDelay) await delay(loopDelayMs()); } } async function runWorkerPass () { if (workerRunning) return; workerRunning = true; try { await processQueueBatch(true); } finally { workerRunning = false; } } function scheduleWorker () { if (workerRunning || !queue.length) return; void runWorkerPass().finally(() => { if (queue.length) scheduleWorker(); }); } function enqueueBacklogFromDb () { const db = getDb(); const rows = db.prepare(` SELECT file_path FROM file_meta WHERE sha256 IS NULL OR sha256 = '' LIMIT ? `).all([BACKLOG_BATCH_SIZE]); for (const row of rows) { enqueueFileHash(row.file_path); } } function startFileHashQueue () { if (loopStarted) return; loopStarted = true; enqueueBacklogFromDb(); scheduleWorker(); loopTimer = setInterval(() => { enqueueBacklogFromDb(); scheduleWorker(); }, 60 * 1000); if (loopTimer && typeof loopTimer.unref === 'function') loopTimer.unref(); } function stopFileHashQueue () { loopStarted = false; if (loopTimer) { clearInterval(loopTimer); loopTimer = null; } queue.length = 0; queued.clear(); } /** * Processes all queued hashes. Intended for tests and graceful shutdown drains. */ async function drainFileHashQueue () { while (workerRunning) await delay(5); workerRunning = true; try { await processQueueBatch(false); } finally { workerRunning = false; } } function getFileHashQueueSnapshot () { return { loopStarted, queueLength: queue.length, workerRunning, hasActiveVideoStreams: hasActiveVideoStreams(), }; } module.exports = { computeFileSha256, enqueueFileHash, drainFileHashQueue, startFileHashQueue, stopFileHashQueue, getFileHashQueueSnapshot, };