refactor: split downloader into modules
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user