// scanner/scanCartella.js const path = require('path'); const fsp = require('fs/promises'); const processFile = require('./processFile'); const { sha256 } = require('./utils'); const { SUPPORTED_EXTS } = require('../config'); /** * Scansiona ricorsivamente una cartella e ritorna SOLO i cambiamenti * (nuovi/modificati) rispetto a previousIndex (mappa { id: meta }). */ async function scanCartella(userName, cartella, absCartella, previousIndex) { const changes = []; async function walk(currentAbs, relPath = '') { const entries = await fsp.readdir(currentAbs, { withFileTypes: true }); for (const e of entries) { const absPath = path.join(currentAbs, e.name); if (e.isDirectory()) { await walk(absPath, path.join(relPath, e.name)); continue; } const ext = path.extname(e.name).toLowerCase(); if (!SUPPORTED_EXTS.has(ext)) continue; const fileRelPath = relPath ? `${relPath}/${e.name}` : e.name; const id = sha256(`${userName}/${cartella}/${fileRelPath}`); const st = await fsp.stat(absPath); const prev = previousIndex[id]; const unchanged = prev && prev.size_bytes === st.size && prev.mtimeMs === st.mtimeMs; if (unchanged) continue; // NOTE: skip "unchanged" const meta = await processFile( userName, cartella, fileRelPath, absPath, ext, st ); meta.id = id; // id sempre presente changes.push(meta); } } await walk(absCartella); return changes; } module.exports = scanCartella;