From e55678e28f0b1310a34e4f47ab96c9fed5e83814 Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 25 May 2021 17:36:31 +0200 Subject: [PATCH] fix thumb/ico bugs --- copyparty/__main__.py | 6 +++--- copyparty/httpcli.py | 11 ++++++----- copyparty/ico.py | 20 +++++++++----------- copyparty/svchub.py | 10 ++++++++-- copyparty/th_srv.py | 6 +++--- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index f55ced22..62fbe512 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -250,9 +250,9 @@ def run_argparse(argv, formatter): ap.add_argument("--salt", type=str, default="hunter2", help="up2k file-hash salt") ap2 = ap.add_argument_group('thumbnail options') - ap.add_argument("--no-thumb", action="store_true", help="disable all thumbnails") - ap.add_argument("--no-vthumb", action="store_true", help="disable video thumbnails") - ap.add_argument("--thumbsz", metavar="WxH", default="352x352", help="thumbnail res") + ap2.add_argument("--no-thumb", action="store_true", help="disable all thumbnails") + ap2.add_argument("--no-vthumb", action="store_true", help="disable video thumbnails") + ap2.add_argument("--thumbsz", metavar="WxH", default="352x352", help="thumbnail res") ap2 = ap.add_argument_group('database options') ap2.add_argument("-e2d", action="store_true", help="enable up2k database") diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index e1da2ee0..9813678d 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -1208,10 +1208,11 @@ class HttpCli(object): return True def tx_ico(self, ext): - n = ext.split(".")[::-1] + bad = re.compile(r"[](){}[]|^[0-9_-]*$") + n = ext.split(".")[1:][::-1] ext = "" for v in n: - if len(v) > 7: + if len(v) > 7 or bad.match(v): break ext = "{}.{}".format(v, ext) @@ -1374,9 +1375,6 @@ class HttpCli(object): raise Pebkac(404) if self.readable and not stat.S_ISDIR(st.st_mode): - if abspath.endswith(".md") and "raw" not in self.uparam: - return self.tx_md(abspath) - if rem.startswith(".hist/up2k."): raise Pebkac(403) @@ -1390,6 +1388,9 @@ class HttpCli(object): return self.tx_ico(rem) + if abspath.endswith(".md") and "raw" not in self.uparam: + return self.tx_md(abspath) + return self.tx_file(abspath) srv_info = [] diff --git a/copyparty/ico.py b/copyparty/ico.py index 4f0fae68..eb03c7c2 100644 --- a/copyparty/ico.py +++ b/copyparty/ico.py @@ -1,6 +1,8 @@ import hashlib import colorsys +from .__init__ import PY2 + class Ico(object): def __init__(self): @@ -9,18 +11,14 @@ class Ico(object): def get(self, ext): """placeholder to make thumbnails not break""" - if False: - h = hashlib.md5(ext.encode("utf-8")).digest()[:6] - lo = [int(x / 3) for x in h] - hi = [int(x / 3 + 170) for x in h] - c = lo[:3] + hi[3:6] - else: - h = hashlib.md5(ext.encode("utf-8")).digest()[:2] - c1 = colorsys.hsv_to_rgb(h[0] / 256.0, 1, 0.3) - c2 = colorsys.hsv_to_rgb(h[0] / 256.0, 1, 1) - c = list(c1) + list(c2) - c = [int(x * 255) for x in c] + h = hashlib.md5(ext.encode("utf-8")).digest()[:2] + if PY2: + h = [ord(x) for x in h] + c1 = colorsys.hsv_to_rgb(h[0] / 256.0, 1, 0.3) + c2 = colorsys.hsv_to_rgb(h[0] / 256.0, 1, 1) + c = list(c1) + list(c2) + c = [int(x * 255) for x in c] c = "".join(["{:02x}".format(x) for x in c]) svg = """\ diff --git a/copyparty/svchub.py b/copyparty/svchub.py index 727dfef0..a7d72059 100644 --- a/copyparty/svchub.py +++ b/copyparty/svchub.py @@ -2,6 +2,7 @@ from __future__ import print_function, unicode_literals import re +import os import sys import time import threading @@ -39,8 +40,13 @@ class SvcHub(object): self.tcpsrv = TcpSrv(self) self.up2k = Up2k(self) - enth = HAVE_PIL and not args.no_thumb - self.thumbsrv = ThumbSrv(self) if enth else None + self.thumbsrv = None + if not args.no_thumb: + if HAVE_PIL: + self.thumbsrv = ThumbSrv(self) + else: + msg = "need Pillow to create thumbnails; for example:\n {} -m pip install --user Pillow" + self.log("thumb", msg.format(os.path.basename(sys.executable)), c=3) # decide which worker impl to use if self.check_mp_enable(): diff --git a/copyparty/th_srv.py b/copyparty/th_srv.py index 1106be19..6b3851bd 100644 --- a/copyparty/th_srv.py +++ b/copyparty/th_srv.py @@ -127,7 +127,7 @@ class ThumbSrv(object): with self.mutex: try: self.busy[tpath].append(cond) - self.log("conv {}".format(tpath)) + self.log("wait {}".format(tpath)) except: thdir = os.path.dirname(tpath) try: @@ -142,7 +142,7 @@ class ThumbSrv(object): self.busy[tpath] = [cond] self.q.put([abspath, tpath]) - self.log("CONV {}".format(tpath)) + self.log("conv {}".format(tpath)) while not self.stopping: with self.mutex: @@ -198,7 +198,7 @@ class ThumbSrv(object): def conv_pil(self, abspath, tpath): with Image.open(abspath) as im: - if im.mode in ("RGBA", "P"): + if im.mode not in ("RGB", "L"): im = im.convert("RGB") im.thumbnail(self.res)