photo_server_json_con_aves22/api_v1/scanner/scanPhotoCartella.js
2026-04-18 20:14:42 +02:00

62 lines
No EOL
1.4 KiB
JavaScript

// api_v1/scanner/scanPhotoCartella.js
const scanCartella = require('./scanCartella');
const scanPhotoSingle = require('./scanPhotoSingle');
const { log } = require('./logger');
async function scanPhotoCartella(
db,
user,
cartella,
absCartella,
newFiles,
updateStatusFile,
CURRENT,
TOTAL_FILES,
start,
deleteThumbsById,
deleteFromDB,
onProgress // 🔥 AGGIUNTO
) {
log(`📁 Scan cartella: ${cartella}`);
// Recupera gli ID già presenti nel DB
const rows = await db('photos')
.where({ user, cartella })
.select('id');
const idsSet = new Set(rows.map(r => r.id));
// Scansiona i file della cartella
for await (const f of scanCartella(user, cartella, absCartella, db)) {
// Aggiorna il contatore
CURRENT.value++;
// 🔥 Aggiorna stato avanzamento (vecchio sistema)
await updateStatusFile(CURRENT, TOTAL_FILES, start);
// 🔥 Aggiorna stato avanzamento (nuovo sistema)
if (onProgress) {
await onProgress();
}
// Processa il file
const meta = await scanPhotoSingle(db, user, cartella, f, newFiles);
// Anche se identico, NON è orfano
idsSet.delete(f.id);
}
// Gestione orfani
for (const orphanId of idsSet) {
log(`🔴 Orfano: ${orphanId}`);
await deleteThumbsById(orphanId);
await deleteFromDB(orphanId, user);
}
}
module.exports = scanPhotoCartella;