2026-03-05 14:45:06 +01:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
|
const path = require('path');
|
2026-03-16 10:27:12 +01:00
|
|
|
const { getDestinationDecision, getCadInfo } = require('./router');
|
2026-03-05 17:35:12 +01:00
|
|
|
const { buildDestinationIndex } = require('./destinationIndex');
|
2026-03-16 10:27:12 +01:00
|
|
|
const { buildExistingCadKeyIndex, toCadKey } = require('./duplicateIndex');
|
|
|
|
|
const { getUnroutedTarget, getDuplicateTarget } = require('./unrouted');
|
2026-03-16 10:05:13 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2026-03-05 14:45:06 +01:00
|
|
|
|
|
|
|
|
async function processFolder(folder, config) {
|
2026-03-16 10:05:13 +01:00
|
|
|
const files = await collectFilesRecursively(folder);
|
2026-03-05 17:35:12 +01:00
|
|
|
const destinationIndex = await buildDestinationIndex(config?.destination);
|
2026-03-16 10:27:12 +01:00
|
|
|
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination);
|
2026-03-05 14:45:06 +01:00
|
|
|
const result = {
|
|
|
|
|
scanned: 0,
|
|
|
|
|
copied: 0,
|
|
|
|
|
skipped: 0,
|
2026-03-16 10:05:13 +01:00
|
|
|
unrouted: 0,
|
2026-03-16 10:27:12 +01:00
|
|
|
duplicates: 0,
|
2026-03-05 14:45:06 +01:00
|
|
|
details: [],
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-16 10:05:13 +01:00
|
|
|
for (const { fullPath, fileName } of files) {
|
|
|
|
|
const file = fileName;
|
2026-03-05 14:45:06 +01:00
|
|
|
result.scanned += 1;
|
|
|
|
|
|
2026-03-16 10:27:12 +01:00
|
|
|
const cadInfo = getCadInfo(file);
|
|
|
|
|
if (!cadInfo) {
|
2026-03-05 14:45:06 +01:00
|
|
|
result.skipped += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:27:12 +01:00
|
|
|
if (existingCadKeys.has(toCadKey(cadInfo))) {
|
|
|
|
|
const duplicateTarget = await getDuplicateTarget(file);
|
|
|
|
|
await fs.copy(fullPath, duplicateTarget.destinationPath, { overwrite: false });
|
|
|
|
|
|
|
|
|
|
result.copied += 1;
|
|
|
|
|
result.duplicates += 1;
|
|
|
|
|
result.details.push({
|
|
|
|
|
file,
|
|
|
|
|
destination: duplicateTarget.destinationDir,
|
|
|
|
|
reason: 'Duplicato gia presente prima dello smistamento',
|
|
|
|
|
});
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 17:35:12 +01:00
|
|
|
const decision = getDestinationDecision(file, config, destinationIndex);
|
|
|
|
|
const destDir = decision.destination;
|
2026-03-05 14:45:06 +01:00
|
|
|
|
|
|
|
|
if (!destDir) {
|
2026-03-16 10:05:13 +01:00
|
|
|
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',
|
|
|
|
|
});
|
2026-03-05 14:45:06 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dest = path.join(destDir, file);
|
2026-03-16 10:05:13 +01:00
|
|
|
await fs.copy(fullPath, dest, { overwrite: true });
|
2026-03-05 14:45:06 +01:00
|
|
|
|
|
|
|
|
result.copied += 1;
|
|
|
|
|
result.details.push({ file, destination: destDir });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { processFolder };
|