diff --git a/copyparty/ftpd.py b/copyparty/ftpd.py index fd5d0349..949a7457 100644 --- a/copyparty/ftpd.py +++ b/copyparty/ftpd.py @@ -174,7 +174,7 @@ class FtpFs(AbstractedFS): t = "Unsupported characters in [{}]" raise FSE(t.format(vpath), 1) - fn = sanitize_fn(fn or "", "") + fn = sanitize_fn(fn or "") vpath = vjoin(rd, fn) vfs, rem = self.hub.asrv.vfs.get(vpath, self.uname, r, w, m, d) if ( diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index ee0a4e94..966dd87b 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -2419,7 +2419,7 @@ class HttpCli(object): if rnd: fn = rand_name(fdir, fn, rnd) - fn = sanitize_fn(fn or "", "") + fn = sanitize_fn(fn or "") path = os.path.join(fdir, fn) @@ -2857,7 +2857,7 @@ class HttpCli(object): name = name.translate(tl) dbv, vrem = vfs.get_dbv(rem) - name = sanitize_fn(name, "") + name = sanitize_fn(name) if ( not self.can_read and self.can_write @@ -3353,7 +3353,7 @@ class HttpCli(object): if "nosub" in vfs.flags: raise Pebkac(403, "mkdir is forbidden below this folder") - rem = sanitize_vpath(rem, "/") + rem = sanitize_vpath(rem) fn = vfs.canonical(rem) if not nullwrite: @@ -3397,7 +3397,7 @@ class HttpCli(object): t = "you can only create .md files because you don't have the delete-permission" raise Pebkac(400, t) - sanitized = sanitize_fn(new_file, "") + sanitized = sanitize_fn(new_file) fdir = vfs.canonical(rem) fn = os.path.join(fdir, sanitized) @@ -3549,7 +3549,7 @@ class HttpCli(object): # fallthrough fdir = fdir_base - fname = sanitize_fn(p_file or "", "") + fname = sanitize_fn(p_file or "") abspath = os.path.join(fdir, fname) suffix = "-%.6f-%s" % (time.time(), dip) if p_file and not nullwrite: diff --git a/copyparty/szip.py b/copyparty/szip.py index 91fa2a34..12caf91f 100644 --- a/copyparty/szip.py +++ b/copyparty/szip.py @@ -8,7 +8,7 @@ import time from .authsrv import AuthSrv from .bos import bos from .sutil import StreamArc, errdesc -from .util import min_ex, sanitize_fn, spack, sunpack, yieldfile, zlib +from .util import VPTL_WIN, min_ex, sanitize_to, spack, sunpack, yieldfile, zlib if True: # pylint: disable=using-constant-test from typing import Any, Generator, Optional @@ -104,7 +104,7 @@ def gen_hdr( ret += spack(b" str: return "/".join(ret) -def sanitize_fn(fn: str, ok: str) -> str: - if "/" not in ok: - fn = fn.replace("\\", "/").split("/")[-1] - +def sanitize_fn(fn: str) -> str: + fn = fn.replace("\\", "/").split("/")[-1] if APTL_OS: - fn = fn.translate(APTL_OS) - if ANYWIN: - bad = ["con", "prn", "aux", "nul"] - for n in range(1, 10): - bad += ("com%s lpt%s" % (n, n)).split(" ") - - if fn.lower().split(".")[0] in bad: - fn = "_" + fn - + fn = sanitize_to(fn, APTL_OS) return fn.strip() -def sanitize_vpath(vp: str, ok: str) -> str: +def sanitize_to(fn: str, tl: dict[int, int]) -> str: + fn = fn.translate(tl) + if ANYWIN: + bad = ["con", "prn", "aux", "nul"] + for n in range(1, 10): + bad += ("com%s lpt%s" % (n, n)).split(" ") + + if fn.lower().split(".")[0] in bad: + fn = "_" + fn + return fn + + +def sanitize_vpath(vp: str) -> str: if not FNTL_OS: return vp parts = vp.replace(os.sep, "/").split("/") - ret = [sanitize_fn(x, ok) for x in parts] + ret = [sanitize_to(x, APTL_OS) for x in parts] return "/".join(ret)