Files
163-music-dl/lib/utils.js
T
2026-07-22 17:52:55 +08:00

30 lines
783 B
JavaScript

const fs = require('fs');
const path = require('path');
function sanitize(name) {
return name.replace(/[\/\\:*?"<>|]/g, '_').trim();
}
function sanitizePathSegment(name, fallback) {
const value = sanitize(String(name || '')).replace(/[. ]+$/g, '');
return value && value !== '.' && value !== '..' ? value : fallback;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getDirSize(dirPath) {
let size = 0;
try {
for (const item of fs.readdirSync(dirPath)) {
const itemPath = path.join(dirPath, item);
const stat = fs.statSync(itemPath);
size += stat.isDirectory() ? getDirSize(itemPath) : stat.size;
}
} catch {}
return size;
}
module.exports = { sanitize, sanitizePathSegment, sleep, getDirSize };