170 lines
3.8 KiB
JavaScript
170 lines
3.8 KiB
JavaScript
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
const PRIMARY_UNROUTED_DIR = '/cadroute/__NON_SMISTATI';
|
|
const HOME_UNROUTED_DIR = path.join(os.homedir(), '.cadroute', '__NON_SMISTATI');
|
|
const PRIMARY_DUPLICATES_DIR = '/cadroute/duplicati';
|
|
const HOME_DUPLICATES_DIR = path.join(os.homedir(), '.cadroute', 'duplicati');
|
|
|
|
const SPECIAL_TARGETS = {
|
|
unrouted: {
|
|
primary: PRIMARY_UNROUTED_DIR,
|
|
fallback: HOME_UNROUTED_DIR,
|
|
},
|
|
duplicates: {
|
|
primary: PRIMARY_DUPLICATES_DIR,
|
|
fallback: HOME_DUPLICATES_DIR,
|
|
},
|
|
};
|
|
|
|
const resolvedDirs = new Map();
|
|
|
|
async function resolveTargetDir(kind) {
|
|
const target = SPECIAL_TARGETS[kind];
|
|
if (!target) {
|
|
throw new Error(`Target speciale non supportato: ${kind}`);
|
|
}
|
|
|
|
if (!resolvedDirs.has(kind)) {
|
|
resolvedDirs.set(
|
|
kind,
|
|
(async () => {
|
|
try {
|
|
await fs.ensureDir(target.primary);
|
|
return target.primary;
|
|
} catch {
|
|
await fs.ensureDir(target.fallback);
|
|
return target.fallback;
|
|
}
|
|
})()
|
|
);
|
|
}
|
|
|
|
return resolvedDirs.get(kind);
|
|
}
|
|
|
|
async function resolveUnroutedDir() {
|
|
return resolveTargetDir('unrouted');
|
|
}
|
|
|
|
async function resolveDuplicatesDir() {
|
|
return resolveTargetDir('duplicates');
|
|
}
|
|
|
|
async function getUniquePath(destinationDir, fileName) {
|
|
const parsed = path.parse(fileName);
|
|
let candidate = path.join(destinationDir, fileName);
|
|
let counter = 1;
|
|
|
|
while (await fs.pathExists(candidate)) {
|
|
candidate = path.join(destinationDir, `${parsed.name}__${counter}${parsed.ext}`);
|
|
counter += 1;
|
|
}
|
|
|
|
return candidate;
|
|
}
|
|
|
|
async function getTarget(kind, fileName) {
|
|
const destinationDir = await resolveTargetDir(kind);
|
|
const destinationPath = await getUniquePath(destinationDir, fileName);
|
|
|
|
return {
|
|
destinationDir,
|
|
destinationPath,
|
|
};
|
|
}
|
|
|
|
async function getUnroutedTarget(fileName) {
|
|
return getTarget('unrouted', fileName);
|
|
}
|
|
|
|
async function getDuplicateTarget(fileName) {
|
|
return getTarget('duplicates', fileName);
|
|
}
|
|
|
|
async function listFilesRecursively(rootDir) {
|
|
const files = [];
|
|
|
|
async function walk(currentDir) {
|
|
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);
|
|
continue;
|
|
}
|
|
|
|
if (!entry.isFile()) {
|
|
continue;
|
|
}
|
|
|
|
const stats = await fs.stat(fullPath).catch(() => null);
|
|
if (!stats) {
|
|
continue;
|
|
}
|
|
|
|
files.push({
|
|
name: entry.name,
|
|
relativePath: path.relative(rootDir, fullPath),
|
|
size: stats.size,
|
|
updatedAt: stats.mtime.toISOString(),
|
|
});
|
|
}
|
|
}
|
|
|
|
await walk(rootDir);
|
|
files.sort((a, b) => a.relativePath.localeCompare(b.relativePath, 'it'));
|
|
return files;
|
|
}
|
|
|
|
async function listTargetFiles(kind) {
|
|
const directory = await resolveTargetDir(kind);
|
|
const files = await listFilesRecursively(directory);
|
|
return { directory, files };
|
|
}
|
|
|
|
async function clearTargetFiles(kind) {
|
|
const directory = await resolveTargetDir(kind);
|
|
await fs.emptyDir(directory);
|
|
return { directory };
|
|
}
|
|
|
|
async function listUnroutedFiles() {
|
|
return listTargetFiles('unrouted');
|
|
}
|
|
|
|
async function listDuplicateFiles() {
|
|
return listTargetFiles('duplicates');
|
|
}
|
|
|
|
async function clearUnroutedFiles() {
|
|
return clearTargetFiles('unrouted');
|
|
}
|
|
|
|
async function clearDuplicateFiles() {
|
|
return clearTargetFiles('duplicates');
|
|
}
|
|
|
|
module.exports = {
|
|
getUnroutedTarget,
|
|
getDuplicateTarget,
|
|
resolveUnroutedDir,
|
|
resolveDuplicatesDir,
|
|
listUnroutedFiles,
|
|
listDuplicateFiles,
|
|
clearUnroutedFiles,
|
|
clearDuplicateFiles,
|
|
PRIMARY_UNROUTED_DIR,
|
|
HOME_UNROUTED_DIR,
|
|
PRIMARY_DUPLICATES_DIR,
|
|
HOME_DUPLICATES_DIR,
|
|
};
|