photo_server_json_flutter_c.../api_v1/scanner/scanPhoto.js.ok
2026-03-11 11:31:52 +01:00

153 lines
4.6 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// scanner/scanPhoto.js
const path = require('path');
const fsp = require('fs/promises');
const { loadPreviousIndex, saveIndex } = require('./indexStore');
const scanUserRoot = require('./scanUser');
const postWithAuth = require('./postWithAuth');
const { WEB_ROOT, SEND_PHOTOS, BASE_URL, WRITE_INDEX } = require('../config');
const COMMON = 'Common';
async function scanPhoto(dir, userName) {
console.log(`[SCAN] Avvio scanPhoto per user=${userName}`);
const previousIndexTree = await loadPreviousIndex();
console.log(`[SCAN] previousIndexTree keys:`, Object.keys(previousIndexTree));
const nextIndexTree = JSON.parse(JSON.stringify(previousIndexTree || {}));
const photosRoot = path.resolve(__dirname, '..', '..', WEB_ROOT, 'photos');
console.log(`[SCAN] photosRoot = ${photosRoot}`);
let changes = [];
// ---------------------------------------------------------
// 1) ADMIN → scansiona tutti gli utenti
// ---------------------------------------------------------
if (userName === 'Admin') {
let entries = [];
try {
entries = await fsp.readdir(photosRoot, { withFileTypes: true });
} catch {
console.error(`[SCAN] photosRoot non accessibile`);
return [];
}
console.log(`[SCAN] Trovati ${entries.length} utenti/cartelle`);
for (const e of entries) {
if (!e.isDirectory()) continue;
const user = e.name;
if (user.toLowerCase() === COMMON.toLowerCase()) continue;
const userDir = path.join(photosRoot, user, 'original');
try {
const st = await fsp.stat(userDir);
if (!st.isDirectory()) continue;
} catch {
continue;
}
console.log(`[SCAN] → Scansiono utente: ${user}`);
const userChanges = await scanUserRoot(user, userDir, previousIndexTree);
for (const m of userChanges) {
nextIndexTree[m.user] ??= {};
nextIndexTree[m.user][m.cartella] ??= {};
nextIndexTree[m.user][m.cartella][m.id] = {
id: m.id,
user: m.user,
cartella: m.cartella,
path: m.path,
hash: m._indexHash
};
}
changes.push(...userChanges);
}
// Common
const commonDir = path.join(photosRoot, COMMON, 'original');
try {
const st = await fsp.stat(commonDir);
if (st.isDirectory()) {
console.log(`[SCAN] → Scansiono Common`);
const commonChanges = await scanUserRoot(COMMON, commonDir, previousIndexTree);
for (const m of commonChanges) {
nextIndexTree[COMMON] ??= {};
nextIndexTree[COMMON][m.cartella] ??= {};
nextIndexTree[COMMON][m.cartella][m.id] = {
id: m.id,
user: m.user,
cartella: m.cartella,
path: m.path,
hash: m._indexHash
};
}
changes.push(...commonChanges);
}
} catch {}
}
// ---------------------------------------------------------
// 2) NON-ADMIN → scansiona SOLO lutente richiesto
// ---------------------------------------------------------
else {
const userDir = path.join(photosRoot, userName, 'original');
try {
const st = await fsp.stat(userDir);
if (st.isDirectory()) {
console.log(`[SCAN] → Scansiono utente singolo: ${userName}`);
const userChanges = await scanUserRoot(userName, userDir, previousIndexTree);
for (const m of userChanges) {
nextIndexTree[m.user] ??= {};
nextIndexTree[m.user][m.cartella] ??= {};
nextIndexTree[m.user][m.cartella][m.id] = {
id: m.id,
user: m.user,
cartella: m.cartella,
path: m.path,
hash: m._indexHash
};
}
changes.push(...userChanges);
}
} catch {
console.log(`[SCAN] Nessuna directory per utente ${userName}`);
}
}
// ---------------------------------------------------------
// SALVA INDEX
// ---------------------------------------------------------
if (WRITE_INDEX) {
await saveIndex(nextIndexTree);
}
// ---------------------------------------------------------
// INVIA AL SERVER
// ---------------------------------------------------------
if (SEND_PHOTOS && BASE_URL && changes.length) {
for (const p of changes) {
try {
await postWithAuth(`${BASE_URL}/photos`, p);
} catch (err) {
console.error('Errore invio:', err.message);
}
}
}
console.log(`SCAN COMPLETATA: ${changes.length} file aggiornati`);
return changes;
}
module.exports = scanPhoto;