68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
// api_v1/scanner/scanCartella.js
|
|
const path = require('path');
|
|
const fsp = require('fs/promises');
|
|
const fs = require('fs');
|
|
const { sha256 } = require('./utils');
|
|
const { SUPPORTED_EXTS } = require('../config');
|
|
const { log } = require('./logger');
|
|
|
|
/**
|
|
* Scansiona ricorsivamente la cartella:
|
|
* /photos/<user>/original/<cartella>/
|
|
*
|
|
* Restituisce (yield) i metadati dei file trovati.
|
|
*/
|
|
async function* scanCartella(userName, cartella, absCartella, db) {
|
|
|
|
async function* walk(currentAbs, relPath = '') {
|
|
let entries = [];
|
|
try {
|
|
entries = await fsp.readdir(currentAbs, { withFileTypes: true });
|
|
} catch {
|
|
return;
|
|
}
|
|
console.log(entries);
|
|
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 basato sul percorso
|
|
const id = sha256(`${userName}/${cartella}/${fileRelPath}`);
|
|
|
|
let st;
|
|
try {
|
|
st = await fsp.stat(absPath);
|
|
} catch {
|
|
continue;
|
|
}
|
|
|
|
log(`📂 scanCartella → user=${userName} cartella=${cartella} relPath=${fileRelPath} id=${id}`);
|
|
|
|
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;
|
|
|