From 2dcf6bcb0f543bd4d9126af0ef8b1c25457ae37a Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Sat, 22 Oct 2022 17:24:07 +0200 Subject: [PATCH] make sure all images with resize parameter are under 500KB --- files/helpers/media.py | 42 ++++++++++++++----------------- files/routes/asset_submissions.py | 4 +-- files/routes/subs.py | 6 ++--- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/files/helpers/media.py b/files/helpers/media.py index 28ba9d15c..5cccc4323 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -154,36 +154,32 @@ def process_image(filename=None, resize=0, trim=False, uploader=None, patron=Fal i.save(filename, format="WEBP", method=6, quality=88) - if resize in (300,400,1200): + if resize: if os.stat(filename).st_size > MAX_IMAGE_SIZE_BANNER_RESIZED_KB * 1024: os.remove(filename) abort(413, f"Max size for banners, sidebars, and badges is {MAX_IMAGE_SIZE_BANNER_RESIZED_KB} KB") - if resize == 1200: - path = f'files/assets/images/{SITE_NAME}/banners' - elif resize == 400: - path = f'files/assets/images/{SITE_NAME}/sidebar' - else: - path = f'files/assets/images/badges' + if filename.startswith('files/assets/images/'): + path = filename.rsplit('/', 1)[0] - hashes = {} + hashes = {} - for img in os.listdir(path): - if resize == 400 and img in ('256.webp','585.webp'): continue - img_path = f'{path}/{img}' - if img_path == filename: continue - img = Image.open(img_path) - i_hash = str(imagehash.phash(img)) + for img in os.listdir(path): + if resize == 400 and img in ('256.webp','585.webp'): continue + img_path = f'{path}/{img}' + if img_path == filename: continue + img = Image.open(img_path) + i_hash = str(imagehash.phash(img)) + if i_hash in hashes.keys(): + print(hashes[i_hash], flush=True) + print(img_path, flush=True) + else: hashes[i_hash] = img_path + + i = Image.open(filename) + i_hash = str(imagehash.phash(i)) if i_hash in hashes.keys(): - print(hashes[i_hash], flush=True) - print(img_path, flush=True) - else: hashes[i_hash] = img_path - - i = Image.open(filename) - i_hash = str(imagehash.phash(i)) - if i_hash in hashes.keys(): - os.remove(filename) - abort(409, "Image already exists!") + os.remove(filename) + abort(409, "Image already exists!") db = db or g.db diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index e63b12f44..fcef24e59 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -244,7 +244,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'): filename = f'/asset_submissions/hats/{name}.webp' copyfile(highquality, filename) - process_image(filename) + process_image(filename, resize=100) hat = HatDef(name=name, author_id=author.id, description=description, price=price, submitter_id=v.id) g.db.add(hat) @@ -451,7 +451,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'): filename = f"files/assets/images/hats/{name}.webp" copyfile(new_path, filename) - process_image(filename) + process_image(filename, resize=100) purge_files_in_cache([f"https://{SITE}/i/hats/{name}.webp", f"https://{SITE}/assets/images/hats/{name}.webp", f"https://{SITE}/asset_submissions/hats/original/{name}.{format}"]) ma = ModAction( kind="update_hat", diff --git a/files/routes/subs.py b/files/routes/subs.py index 6141e2088..41f04b29c 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -480,7 +480,7 @@ def sub_banner(v, sub): name = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(name) - bannerurl = process_image(name, patron=v.patron) + bannerurl = process_image(name, patron=v.patron, resize=1200) if bannerurl: if sub.bannerurl and '/images/' in sub.bannerurl: @@ -512,7 +512,7 @@ def sub_sidebar(v, sub): file = request.files["sidebar"] name = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(name) - sidebarurl = process_image(name, patron=v.patron) + sidebarurl = process_image(name, patron=v.patron, resize=400) if sidebarurl: if sub.sidebarurl and '/images/' in sub.sidebarurl: @@ -544,7 +544,7 @@ def sub_marsey(v, sub): file = request.files["marsey"] name = f'/images/{time.time()}'.replace('.','') + '.webp' file.save(name) - marseyurl = process_image(name, patron=v.patron) + marseyurl = process_image(name, patron=v.patron, resize=200) if marseyurl: if sub.marseyurl and '/images/' in sub.marseyurl: