Commit iniziale

This commit is contained in:
2026-03-05 14:45:06 +01:00
commit 7008f57119
13 changed files with 5162 additions and 0 deletions

20
services/configService.js Normal file
View File

@@ -0,0 +1,20 @@
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const configPath = path.join(os.homedir(), '.cad-router-config.json');
const defaultConfigPath = path.join(__dirname, '..', 'config', 'defaultConfig.json');
async function loadConfig() {
if (await fs.pathExists(configPath)) {
return fs.readJson(configPath);
}
const defaultConfig = await fs.readJson(defaultConfigPath);
await fs.writeJson(configPath, defaultConfig, { spaces: 2 });
return defaultConfig;
}
loadConfig.getConfigPath = () => configPath;
module.exports = { loadConfig };

View File

@@ -0,0 +1,47 @@
const fs = require('fs-extra');
const path = require('path');
const { findDestination, isCadFile } = require('./router');
async function processFolder(folder, config) {
const entries = await fs.readdir(folder, { withFileTypes: true });
const result = {
scanned: 0,
copied: 0,
skipped: 0,
details: [],
};
for (const entry of entries) {
if (!entry.isFile()) {
continue;
}
const file = entry.name;
result.scanned += 1;
if (!isCadFile(file)) {
result.skipped += 1;
continue;
}
const src = path.join(folder, file);
const destDir = findDestination(file, config);
if (!destDir) {
result.skipped += 1;
result.details.push({ file, reason: 'Nessuna regola trovata' });
continue;
}
await fs.ensureDir(destDir);
const dest = path.join(destDir, file);
await fs.copy(src, dest, { overwrite: true });
result.copied += 1;
result.details.push({ file, destination: destDir });
}
return result;
}
module.exports = { processFolder };

29
services/router.js Normal file
View File

@@ -0,0 +1,29 @@
const path = require('path');
function findDestination(filename, config) {
const ext = path.extname(filename).slice(1).toLowerCase();
for (const rule of config.rules || []) {
if ((rule.ext || '').toLowerCase() !== ext) {
continue;
}
if (!rule.pattern) {
return rule.destination;
}
const regex = new RegExp(rule.pattern);
if (regex.test(path.basename(filename))) {
return rule.destination;
}
}
return null;
}
function isCadFile(filename) {
const ext = path.extname(filename).slice(1).toLowerCase();
return ['prt', 'asm', 'drw'].includes(ext);
}
module.exports = { findDestination, isCadFile };

52
services/zipProcessor.js Normal file
View File

@@ -0,0 +1,52 @@
const unzipper = require('unzipper');
const fs = require('fs-extra');
const path = require('path');
const { pipeline } = require('stream/promises');
const { findDestination, isCadFile } = require('./router');
async function processZip(zipPath, config) {
const stream = fs.createReadStream(zipPath).pipe(unzipper.Parse({ forceStream: true }));
const result = {
scanned: 0,
copied: 0,
skipped: 0,
details: [],
};
for await (const entry of stream) {
if (entry.type !== 'File') {
entry.autodrain();
continue;
}
const file = entry.path;
const baseName = path.basename(file);
result.scanned += 1;
if (!isCadFile(baseName)) {
result.skipped += 1;
entry.autodrain();
continue;
}
const destDir = findDestination(baseName, config);
if (!destDir) {
result.skipped += 1;
result.details.push({ file: baseName, reason: 'Nessuna regola trovata' });
entry.autodrain();
continue;
}
await fs.ensureDir(destDir);
const dest = path.join(destDir, baseName);
await pipeline(entry, fs.createWriteStream(dest));
result.copied += 1;
result.details.push({ file: baseName, destination: destDir });
}
return result;
}
module.exports = { processZip };