photo_server_json_flutter_c.../api_v1/scanner/scanCartella.js
2026-03-23 12:31:01 +01:00

64 lines
No EOL
1.6 KiB
JavaScript

// api_v1/scanner/scanCartella.js
const path = require('path');
const fsp = require('fs/promises');
const { sha256 } = require('./utils');
const { SUPPORTED_EXTS } = require('../config');
/**
* scanCartella: genera informazioni sui file nella cartella
* Non esegue processFile né scrive sul DB: lascia la logica di confronto a scanPhoto.
*
* Parametri:
* - userName
* - cartella
* - absCartella
* - db (opzionale, mantenuto per compatibilità)
*/
async function* scanCartella(userName, cartella, absCartella, db) {
async function* walk(currentAbs, relPath = '') {
let entries = [];
try {
entries = await fsp.readdir(currentAbs, { withFileTypes: true });
} catch {
return;
}
for (const e of entries) {
const absPath = path.join(currentAbs, e.name);
if (e.isDirectory()) {
yield* 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;
// ID deterministico (stesso criterio di prima)
const id = sha256(`${userName}/${cartella}/${fileRelPath}`);
// Stat veloce
let st;
try { st = await fsp.stat(absPath); } catch { continue; }
yield {
id,
user: userName,
cartella,
name: e.name,
relPath: fileRelPath,
absPath,
ext,
stat: st,
path: `/photos/${userName}/original/${cartella}/${fileRelPath}`
};
}
}
yield* walk(absCartella);
}
module.exports = scanCartella;