Перенос на сервер работы с именами файлов при скачивании

This commit is contained in:
Book Pauk
2022-11-06 18:03:03 +07:00
parent d9f1912ea2
commit 55239159ba
6 changed files with 69 additions and 106 deletions

View File

@@ -119,6 +119,27 @@ function toUnixPath(dir) {
return dir.replace(/\\/g, '/');
}
function makeValidFileName(fileName, repl = '_') {
let f = fileName.replace(/[\x00\\/:*"<>|]/g, repl); // eslint-disable-line no-control-regex
f = f.trim();
while (f.length && (f[f.length - 1] == '.' || f[f.length - 1] == '_')) {
f = f.substring(0, f.length - 1);
}
if (f)
return f;
else
throw new Error('Invalid filename');
}
function makeValidFileNameOrEmpty(fileName) {
try {
return makeValidFileName(fileName);
} catch(e) {
return '';
}
}
module.exports = {
sleep,
processLoop,
@@ -133,4 +154,6 @@ module.exports = {
randomHexString,
gzipFile,
toUnixPath,
makeValidFileName,
makeValidFileNameOrEmpty,
};