feat(processing): smistamento ricorsivo cartelle + gestione unrouted per ZIP e folder

This commit is contained in:
2026-03-16 10:05:13 +01:00
parent 771891ac4f
commit 164959acaf
4 changed files with 106 additions and 14 deletions

View File

@@ -2,23 +2,45 @@ const fs = require('fs-extra');
const path = require('path');
const { getDestinationDecision, isCadFile } = require('./router');
const { buildDestinationIndex } = require('./destinationIndex');
const { getUnroutedTarget } = require('./unrouted');
async function collectFilesRecursively(rootDir) {
const files = [];
async function walk(currentDir) {
const entries = await fs.readdir(currentDir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
await walk(fullPath);
continue;
}
if (entry.isFile()) {
files.push({ fullPath, fileName: entry.name });
}
}
}
await walk(rootDir);
return files;
}
async function processFolder(folder, config) {
const entries = await fs.readdir(folder, { withFileTypes: true });
const files = await collectFilesRecursively(folder);
const destinationIndex = await buildDestinationIndex(config?.destination);
const result = {
scanned: 0,
copied: 0,
skipped: 0,
unrouted: 0,
details: [],
};
for (const entry of entries) {
if (!entry.isFile()) {
continue;
}
const file = entry.name;
for (const { fullPath, fileName } of files) {
const file = fileName;
result.scanned += 1;
if (!isCadFile(file)) {
@@ -26,18 +48,25 @@ async function processFolder(folder, config) {
continue;
}
const src = path.join(folder, file);
const decision = getDestinationDecision(file, config, destinationIndex);
const destDir = decision.destination;
if (!destDir) {
result.skipped += 1;
result.details.push({ file, reason: decision.reason || 'Nessuna regola trovata' });
const unroutedTarget = await getUnroutedTarget(file);
await fs.copy(fullPath, unroutedTarget.destinationPath, { overwrite: false });
result.copied += 1;
result.unrouted += 1;
result.details.push({
file,
destination: unroutedTarget.destinationDir,
reason: decision.reason || 'Nessuna regola trovata',
});
continue;
}
const dest = path.join(destDir, file);
await fs.copy(src, dest, { overwrite: true });
await fs.copy(fullPath, dest, { overwrite: true });
result.copied += 1;
result.details.push({ file, destination: destDir });