feat(router): rilevamento duplicati pre-smistamento e instradamento in duplicati
This commit is contained in:
55
services/duplicateIndex.js
Normal file
55
services/duplicateIndex.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { getCadInfo } = require('./router');
|
||||
|
||||
const MAX_SCAN_DEPTH = 8;
|
||||
|
||||
function toCadKey(cadInfo) {
|
||||
return String(cadInfo?.key || '').toLowerCase();
|
||||
}
|
||||
|
||||
async function buildExistingCadKeyIndex(destinationRoot) {
|
||||
const keys = new Set();
|
||||
|
||||
if (!destinationRoot || !(await fs.pathExists(destinationRoot))) {
|
||||
return keys;
|
||||
}
|
||||
|
||||
async function walk(currentDir, depth) {
|
||||
if (depth > MAX_SCAN_DEPTH) {
|
||||
return;
|
||||
}
|
||||
|
||||
let entries;
|
||||
try {
|
||||
entries = await fs.readdir(currentDir, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(currentDir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
await walk(fullPath, depth + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!entry.isFile()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const cadInfo = getCadInfo(entry.name);
|
||||
if (!cadInfo) {
|
||||
continue;
|
||||
}
|
||||
|
||||
keys.add(toCadKey(cadInfo));
|
||||
}
|
||||
}
|
||||
|
||||
await walk(destinationRoot, 0);
|
||||
return keys;
|
||||
}
|
||||
|
||||
module.exports = { buildExistingCadKeyIndex, toCadKey };
|
||||
Reference in New Issue
Block a user