feat(router): rilevamento duplicati pre-smistamento e instradamento in duplicati

This commit is contained in:
2026-03-16 10:27:12 +01:00
parent 3a567c390c
commit 5e4e03a103
4 changed files with 180 additions and 23 deletions

View File

@@ -1,8 +1,9 @@
const fs = require('fs-extra');
const path = require('path');
const { getDestinationDecision, isCadFile } = require('./router');
const { getDestinationDecision, getCadInfo } = require('./router');
const { buildDestinationIndex } = require('./destinationIndex');
const { getUnroutedTarget } = require('./unrouted');
const { buildExistingCadKeyIndex, toCadKey } = require('./duplicateIndex');
const { getUnroutedTarget, getDuplicateTarget } = require('./unrouted');
async function collectFilesRecursively(rootDir) {
const files = [];
@@ -31,11 +32,13 @@ async function collectFilesRecursively(rootDir) {
async function processFolder(folder, config) {
const files = await collectFilesRecursively(folder);
const destinationIndex = await buildDestinationIndex(config?.destination);
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination);
const result = {
scanned: 0,
copied: 0,
skipped: 0,
unrouted: 0,
duplicates: 0,
details: [],
};
@@ -43,11 +46,26 @@ async function processFolder(folder, config) {
const file = fileName;
result.scanned += 1;
if (!isCadFile(file)) {
const cadInfo = getCadInfo(file);
if (!cadInfo) {
result.skipped += 1;
continue;
}
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;
}
const decision = getDestinationDecision(file, config, destinationIndex);
const destDir = decision.destination;