import fs from 'fs' import crypto from 'crypto' const CHUNK_SIZE = 64 * 1024 // 64 KB /** * Computes a stable partial-content fingerprint for a file. * Uses SHA-256 of the file size + first 64 KB of content. * Fast enough for large video files (~instant) and collision-resistant * for real-world media libraries. * * Returns null if the file cannot be read (missing, permission error, etc.). */ export function computeFingerprint(absolutePath: string): string | null { try { const stat = fs.statSync(absolutePath) const size = stat.size const chunkLen = Math.min(CHUNK_SIZE, size) const buf = Buffer.alloc(chunkLen) if (chunkLen > 0) { const fd = fs.openSync(absolutePath, 'r') try { fs.readSync(fd, buf, 0, chunkLen, 0) } finally { fs.closeSync(fd) } } return crypto .createHash('sha256') .update(`${size}:`) .update(buf) .digest('hex') } catch { return null } }