filter zip name

This commit is contained in:
2023-05-27 19:14:13 +08:00
parent 21a17dd9d7
commit 8f0ed03117
3 changed files with 38 additions and 3 deletions

View File

@@ -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;
}