Aevann 2023-08-12 23:00:11 +03:00
parent 4db24f2ecf
commit e35803e702
2 changed files with 14 additions and 9 deletions

View File

@ -456,6 +456,9 @@ function handle_files(input, newfiles) {
if (!newfiles) return; if (!newfiles) return;
for (const file of newfiles) { for (const file of newfiles) {
if (file.type.startsWith('image/'))
continue
let max_size let max_size
let max_size_patron let max_size_patron
let type let type

View File

@ -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 # 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 # if an image is too large or webp conversion fails, it'll crash
# to avoid this, we'll simply return None instead # to avoid this, we'll simply return None instead
original_resize = resize
has_request = has_request_context() has_request = has_request_context()
size = os.stat(filename).st_size size = os.stat(filename).st_size
is_patron = bool(v and v.patron) if v and v.patron:
max_size = MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024
if size > MAX_IMAGE_AUDIO_SIZE_MB_PATRON * 1024 * 1024 or not is_patron and size > MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024: else:
os.remove(filename) max_size = MAX_IMAGE_AUDIO_SIZE_MB * 1024 * 1024
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
try: try:
with Image.open(filename) as i: with Image.open(filename) as i:
if size > max_size:
ratio = max_size / size
resize = i.width * ratio
oldformat = i.format oldformat = i.format
params = ["magick"] params = ["magick"]
if resize == 99: params.append(f"{filename}[0]") 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 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: if size_after_conversion > MAX_IMAGE_SIZE_BANNER_RESIZED_MB * 1024 * 1024:
os.remove(filename) os.remove(filename)
if has_request: if has_request:
@ -233,7 +235,7 @@ def process_image(filename, v, resize=0, trim=False, uploader_id=None, db=None):
hashes = {} hashes = {}
for img in os.listdir(path): 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}' img_path = f'{path}/{img}'
if img_path == filename: continue if img_path == filename: continue