fix thumb/ico bugs

This commit is contained in:
ed 2021-05-25 17:36:31 +02:00
parent 3f4a8b9d6f
commit e55678e28f
5 changed files with 29 additions and 24 deletions

View file

@ -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")

View file

@ -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 = []

View file

@ -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]
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 = """\

View file

@ -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():

View file

@ -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)