From e35803e702a5488fb09e769b0fc591148814e591 Mon Sep 17 00:00:00 2001 From: Aevann Date: Sat, 12 Aug 2023 23:00:11 +0300 Subject: [PATCH] do this https://rdrama.net/h/countryclub/post/79285/-/4788305#context --- files/assets/js/core.js | 3 +++ files/helpers/media.py | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 303db35d1..6a444fcb0 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -456,6 +456,9 @@ function handle_files(input, newfiles) { if (!newfiles) return; for (const file of newfiles) { + if (file.type.startsWith('image/')) + continue + let max_size let max_size_patron let type diff --git a/files/helpers/media.py b/files/helpers/media.py index d069f073f..c12716cde 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -179,18 +179,20 @@ def process_image(filename, v, resize=0, trim=False, uploader_id=None, db=None): # thumbnails are processed in a thread and not in the request context # if an image is too large or webp conversion fails, it'll crash # to avoid this, we'll simply return None instead + original_resize = resize has_request = has_request_context() size = os.stat(filename).st_size - is_patron = bool(v and v.patron) - - if size > MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 or not is_patron and size > MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024: - os.remove(filename) - if has_request: - abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for {patron}s)") - return None + if v and v.patron: + max_size = MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 + else: + max_size = MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024 try: with Image.open(filename) as i: + if size > max_size: + ratio = max_size / size + resize = i.width * ratio + oldformat = i.format params = ["magick"] if resize == 99: params.append(f"{filename}[0]") @@ -218,7 +220,7 @@ def process_image(filename, v, resize=0, trim=False, uploader_id=None, db=None): size_after_conversion = os.stat(filename).st_size - if resize: + if original_resize: if size_after_conversion > MAX_IMAGE_SIZE_BANNER_RESIZED_MB * 1024 * 1024: os.remove(filename) if has_request: @@ -233,7 +235,7 @@ def process_image(filename, v, resize=0, trim=False, uploader_id=None, db=None): hashes = {} for img in os.listdir(path): - if resize == 400 and img in {'256.webp','585.webp'}: continue + if original_resize == 400 and img in {'256.webp','585.webp'}: continue img_path = f'{path}/{img}' if img_path == filename: continue