mirror of
https://github.com/lifegpc/eh-downloader.git
synced 2026-06-06 05:38:44 +08:00
filter zip name
This commit is contained in:
23
utils.ts
23
utils.ts
@@ -1,4 +1,5 @@
|
||||
import { exists, existsSync } from "std/fs/exists.ts";
|
||||
import { extname } from "std/path/mod.ts";
|
||||
import { initParser } from "deno_dom/deno-dom-wasm-noinit.ts";
|
||||
|
||||
export function sleep(time: number): Promise<undefined> {
|
||||
@@ -111,3 +112,25 @@ export function addZero(n: string | number, len: number) {
|
||||
while (s.length < len) s = "0" + s;
|
||||
return s;
|
||||
}
|
||||
|
||||
export function filterFilename(p: string, maxLength = 256) {
|
||||
// strip control chars
|
||||
p = p.replace(/\p{C}/gu, "");
|
||||
// normalize newline
|
||||
p = p.replace(/[\n\r]/g, "");
|
||||
p = p.replace(/\p{Zl}/gu, "");
|
||||
p = p.replace(/\p{Zp}/gu, "");
|
||||
// normalize space
|
||||
p = p.replace(/\p{Zs}/gu, " ");
|
||||
p = p.replace(/[\\/]/g, "_");
|
||||
if (Deno.build.os == "windows") {
|
||||
p = p.replace(/[:\*\?\"<>\|]/g, "_");
|
||||
} else if (Deno.build.os == "linux") {
|
||||
p = p.replace(/[!\$\"]/g, "_");
|
||||
}
|
||||
if (p.length > maxLength) {
|
||||
const ext = extname(p);
|
||||
return p.slice(0, maxLength - ext.length) + ext;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user