// 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'); async function scanCartella(userName, cartella, absCartella, previousIndexTree) { const changes = []; async function walk(currentAbs, relPath = '') { console.log(`[SCAN] Entrato in cartella: ${currentAbs}`); let entries = []; try { entries = await fsp.readdir(currentAbs, { withFileTypes: true }); } catch (err) { console.error(`[SCAN] Impossibile leggere: ${currentAbs} - ${err.message}`); return; } 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; console.log(`[SCAN] FILE TROVATO → ${fileRelPath}`); const id = sha256(`${userName}/${cartella}/${fileRelPath}`); let st; try { st = await fsp.stat(absPath); } catch { continue; } const hash = sha256(`${st.size}-${st.mtimeMs}`); const prev = previousIndexTree?.[userName]?.[cartella]?.[id]; console.log(`[SCAN] CHECK → id=${id}, hash=${hash}, prevHash=${prev?.hash}`); // 🔥 NON facciamo più SKIP qui. // Anche se il file è invariato, lo passiamo a scanPhoto.js // così può rimuovere l'ID da idsIndex. if (prev && prev.hash === hash) { console.log(`[SCAN] FILE INVARIATO (passato a scanPhoto) → ${fileRelPath}`); const meta = { id, user: userName, cartella, path: `/photos/${userName}/original/${cartella}/${fileRelPath}`, _indexHash: hash, unchanged: true }; changes.push(meta); continue; } console.log(`[SCAN] PROCESSING → ${userName}/${cartella}/${fileRelPath}`); const meta = await processFile( userName, cartella, fileRelPath, absPath, ext, st ); meta.id = id; meta.path = `/photos/${userName}/original/${cartella}/${fileRelPath}`; meta._indexHash = hash; changes.push(meta); } } await walk(absCartella); return changes; } module.exports = scanCartella;