From 5ad65450c49ace640cf9661b9b92ce85900443c5 Mon Sep 17 00:00:00 2001 From: ed Date: Sat, 1 Jun 2024 01:15:34 +0000 Subject: [PATCH] more intuitive df option/volflag, closes #83 --- copyparty/__main__.py | 2 +- copyparty/authsrv.py | 11 +++++++---- copyparty/util.py | 21 ++++++++++++++------- tests/util.py | 6 +++--- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index e4f96aa3..e8eed38c 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -912,7 +912,7 @@ def add_upload(ap): ap2.add_argument("--rand", action="store_true", help="force randomized filenames, \033[33m--nrand\033[0m chars long (volflag=rand)") ap2.add_argument("--nrand", metavar="NUM", type=int, default=9, help="randomized filenames length (volflag=nrand)") ap2.add_argument("--magic", action="store_true", help="enable filetype detection on nameless uploads (volflag=magic)") - ap2.add_argument("--df", metavar="GiB", type=float, default=0, help="ensure \033[33mGiB\033[0m free disk space by rejecting upload requests") + ap2.add_argument("--df", metavar="GiB", type=u, default="0", help="ensure \033[33mGiB\033[0m free disk space by rejecting upload requests; assumes gigabytes unless a unit suffix is given: [\033[32m256m\033[0m], [\033[32m4\033[0m], [\033[32m2T\033[0m] (volflag=df)") ap2.add_argument("--sparse", metavar="MiB", type=int, default=4, help="windows-only: minimum size of incoming uploads through up2k before they are made into sparse files") ap2.add_argument("--turbo", metavar="LVL", type=int, default=0, help="configure turbo-mode in up2k client; [\033[32m-1\033[0m] = forbidden/always-off, [\033[32m0\033[0m] = default-off and warn if enabled, [\033[32m1\033[0m] = default-off, [\033[32m2\033[0m] = on, [\033[32m3\033[0m] = on and disable datecheck") ap2.add_argument("--u2j", metavar="JOBS", type=int, default=2, help="web-client: number of file chunks to upload in parallel; 1 or 2 is good for low-latency (same-country) connections, 4-8 for android clients, 16 for cross-atlantic (max=64)") diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index 5e44315d..bffd2f98 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -1617,11 +1617,14 @@ class AuthSrv(object): use = True lim.nosub = True - zs = vol.flags.get("df") or ( - "{}g".format(self.args.df) if self.args.df else "" - ) - if zs: + zs = vol.flags.get("df") or self.args.df or "" + if zs not in ("", "0"): use = True + try: + _ = float(zs) + zs = "%sg" % (zs) + except: + pass lim.dfl = unhumanize(zs) zs = vol.flags.get("sz") diff --git a/copyparty/util.py b/copyparty/util.py index 050bf718..d64f6e49 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -358,6 +358,18 @@ APPLESAN_TXT = r"/(__MACOS|Icon\r\r)|/\.(_|DS_Store|AppleDouble|LSOverride|Docum APPLESAN_RE = re.compile(APPLESAN_TXT) +HUMANSIZE_UNITS = ("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB") + +UNHUMANIZE_UNITS = { + "b": 1, + "k": 1024, + "m": 1024 * 1024, + "g": 1024 * 1024 * 1024, + "t": 1024 * 1024 * 1024 * 1024, + "p": 1024 * 1024 * 1024 * 1024 * 1024, + "e": 1024 * 1024 * 1024 * 1024 * 1024 * 1024, +} + VF_CAREFUL = {"mv_re_t": 5, "rm_re_t": 5, "mv_re_r": 0.1, "rm_re_r": 0.1} @@ -1808,7 +1820,7 @@ def gencookie(k: str, v: str, r: str, tls: bool, dur: int = 0, txt: str = "") -> def humansize(sz: float, terse: bool = False) -> str: - for unit in ["B", "KiB", "MiB", "GiB", "TiB"]: + for unit in HUMANSIZE_UNITS: if sz < 1024: break @@ -1829,12 +1841,7 @@ def unhumanize(sz: str) -> int: pass mc = sz[-1:].lower() - mi = { - "k": 1024, - "m": 1024 * 1024, - "g": 1024 * 1024 * 1024, - "t": 1024 * 1024 * 1024 * 1024, - }.get(mc, 1) + mi = UNHUMANIZE_UNITS.get(mc, 1) return int(float(sz[:-1]) * mi) diff --git a/tests/util.py b/tests/util.py index cf510639..56c78cae 100644 --- a/tests/util.py +++ b/tests/util.py @@ -110,7 +110,7 @@ class Cfg(Namespace): def __init__(self, a=None, v=None, c=None, **ka0): ka = {} - ex = "daw dav_auth dav_inf dav_mac dav_rt e2d e2ds e2dsa e2t e2ts e2tsr e2v e2vu e2vp early_ban ed emp exp force_js getmod grid hardlink ih ihead magic never_symlink nid nih no_acode no_athumb no_dav no_dedup no_del no_dupe no_lifetime no_logues no_mv no_pipe no_readme no_robots no_sb_md no_sb_lg no_scandir no_tarcmp no_thumb no_vthumb no_zip nrand nw og og_no_head og_s_title q rand smb srch_dbg stats uqe vague_403 vc ver xdev xlink xvol" + ex = "daw dav_auth dav_inf dav_mac dav_rt e2d e2ds e2dsa e2t e2ts e2tsr e2v e2vu e2vp early_ban ed emp exp force_js getmod grid hardlink ih ihead magic never_symlink nid nih no_acode no_athumb no_dav no_dedup no_del no_dupe no_lifetime no_logues no_mv no_pipe no_poll no_readme no_robots no_sb_md no_sb_lg no_scandir no_tarcmp no_thumb no_vthumb no_zip nrand nw og og_no_head og_s_title q rand smb srch_dbg stats uqe vague_403 vc ver xdev xlink xvol" ka.update(**{k: False for k in ex.split()}) ex = "dotpart dotsrch no_dhash no_fastboot no_rescan no_sendfile no_snap no_voldump re_dhash plain_ip" @@ -125,10 +125,10 @@ class Cfg(Namespace): ex = "au_vol mtab_age reg_cap s_thead s_tbody th_convt" ka.update(**{k: 9 for k in ex.split()}) - ex = "db_act df k304 loris re_maxage rproxy rsp_jtr rsp_slp s_wr_slp snap_wri theme themes turbo" + ex = "db_act k304 loris re_maxage rproxy rsp_jtr rsp_slp s_wr_slp snap_wri theme themes turbo" ka.update(**{k: 0 for k in ex.split()}) - ex = "ah_alg bname doctitle exit favico idp_h_usr html_head lg_sbf log_fk md_sbf name og_desc og_site og_th og_title og_title_a og_title_v og_title_i tcolor textfiles unlist vname R RS SR" + ex = "ah_alg bname doctitle df exit favico idp_h_usr html_head lg_sbf log_fk md_sbf name og_desc og_site og_th og_title og_title_a og_title_v og_title_i tcolor textfiles unlist vname R RS SR" ka.update(**{k: "" for k in ex.split()}) ex = "grp on403 on404 xad xar xau xban xbd xbr xbu xiu xm"