70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
// scanner/scanCartella.js
|
|
const path = require('path');
|
|
const fsp = require('fs/promises');
|
|
const processFile = require('./processFile');
|
|
const { sha256 } = require('./utils');
|
|
const { SUPPORTED_EXTS } = require('../config');
|
|
|
|
async function* scanCartella(userName, cartella, absCartella, previousIndexTree) {
|
|
|
|
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;
|
|
const id = sha256(`${userName}/${cartella}/${fileRelPath}`);
|
|
|
|
let st;
|
|
try { st = await fsp.stat(absPath); } catch { continue; }
|
|
|
|
const hash = sha256(`${st.size}-${st.mtimeMs}`);
|
|
const prev = previousIndexTree?.[userName]?.[cartella]?.[id];
|
|
|
|
if (prev && prev.hash === hash) {
|
|
yield {
|
|
id,
|
|
user: userName,
|
|
cartella,
|
|
path: `/photos/${userName}/original/${cartella}/${fileRelPath}`,
|
|
_indexHash: hash,
|
|
unchanged: true
|
|
};
|
|
continue;
|
|
}
|
|
|
|
const meta = await processFile(
|
|
userName,
|
|
cartella,
|
|
fileRelPath,
|
|
absPath,
|
|
ext,
|
|
st
|
|
);
|
|
|
|
meta.id = id;
|
|
meta.path = `/photos/${userName}/original/${cartella}/${fileRelPath}`;
|
|
meta._indexHash = hash;
|
|
|
|
yield meta;
|
|
}
|
|
}
|
|
|
|
yield* walk(absCartella);
|
|
}
|
|
|
|
module.exports = scanCartella;
|