73 lines
2 KiB
Text
73 lines
2 KiB
Text
// scanner/scanUser.js
|
|
const path = require('path');
|
|
const fsp = require('fs/promises');
|
|
const scanCartella = require('./scanCartella');
|
|
const processFile = require('./processFile');
|
|
const { sha256 } = require('./utils');
|
|
const { SUPPORTED_EXTS } = require('../config');
|
|
|
|
async function scanUserRoot(userName, userDir, previousIndexTree) {
|
|
console.log(`[SCAN] scanUserRoot → user=${userName}, dir=${userDir}`);
|
|
|
|
const results = [];
|
|
const entries = await fsp.readdir(userDir, { withFileTypes: true });
|
|
|
|
// 1) File nella root
|
|
for (const e of entries) {
|
|
if (e.isDirectory()) continue;
|
|
|
|
const ext = path.extname(e.name).toLowerCase();
|
|
if (!SUPPORTED_EXTS.has(ext)) continue;
|
|
|
|
const absPath = path.join(userDir, e.name);
|
|
const st = await fsp.stat(absPath);
|
|
|
|
const fileRelPath = e.name;
|
|
const cartella = '_root';
|
|
const id = sha256(`${userName}/${cartella}/${fileRelPath}`);
|
|
|
|
const hash = sha256(`${st.size}-${st.mtimeMs}`);
|
|
const prev = previousIndexTree?.[userName]?.[cartella]?.[id];
|
|
|
|
console.log(`[SCAN] ROOT CHECK → ${fileRelPath}, hash=${hash}, prevHash=${prev?.hash}`);
|
|
|
|
if (prev && prev.hash === hash) {
|
|
console.log(`[SCAN] SKIP ROOT (unchanged) → ${fileRelPath}`);
|
|
continue;
|
|
}
|
|
|
|
console.log(`[SCAN] PROCESSING ROOT → ${fileRelPath}`);
|
|
|
|
const meta = await processFile(
|
|
userName,
|
|
cartella,
|
|
fileRelPath,
|
|
absPath,
|
|
ext,
|
|
st
|
|
);
|
|
|
|
meta.id = id;
|
|
meta.path = `/photos/${userName}/original/${cartella}/${fileRelPath}`;
|
|
meta._indexHash = hash;
|
|
|
|
results.push(meta);
|
|
}
|
|
|
|
// 2) Sottocartelle
|
|
for (const e of entries) {
|
|
if (!e.isDirectory()) continue;
|
|
|
|
const cartella = e.name;
|
|
const absCartella = path.join(userDir, cartella);
|
|
|
|
console.log(`[SCAN] → Scansiono cartella: ${cartella}`);
|
|
|
|
const files = await scanCartella(userName, cartella, absCartella, previousIndexTree);
|
|
results.push(...files);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
module.exports = scanUserRoot;
|