From fa4dee419db616965a8d9389759b40d340d8cb36 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 27 Sep 2022 02:47:52 +0200 Subject: [PATCH 1/3] remove unnecessary imports --- files/helpers/const.py | 4 ++-- files/routes/asset_submissions.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/files/helpers/const.py b/files/helpers/const.py index 962e6342f..a1c5ffca5 100644 --- a/files/helpers/const.py +++ b/files/helpers/const.py @@ -1,4 +1,4 @@ -from os import environ, listdir +from os import environ import re from copy import deepcopy from json import loads @@ -1098,4 +1098,4 @@ forced_hats = { "marsify": ("Marsified", "I can't pick my own Marseys, help!"), "is_suspended": ("Behind Bars", "This user is banned and needs to do better!"), "agendaposter": ("Egg_irl", "This user is getting in touch with xir identity!") -} +} \ No newline at end of file diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index dd1f9c2c9..9a3e3226b 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -1,5 +1,5 @@ from shutil import move, copyfile -from os import listdir, rename, path +from os import rename, path from files.__main__ import app, limiter from files.helpers.const import * From cc85cab01d35e7e4c4f8202e1059f4539bfec912 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 27 Sep 2022 02:48:03 +0200 Subject: [PATCH 2/3] make badges 300px instead --- files/routes/comments.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/routes/comments.py b/files/routes/comments.py index c9ee808ff..d47cbdd59 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -222,7 +222,7 @@ def comment(v): g.db.flush() filename = f'files/assets/images/badges/{badge.id}.webp' copyfile(oldname, filename) - process_image(filename, resize=400) + process_image(filename, resize=300) requests.post(f'https://api.cloudflare.com/client/v4/zones/{CF_ZONE}/purge_cache', headers=CF_HEADERS, data=f'{{"files": ["https://{SITE}/assets/images/badges/{badge.id}.webp"]}}', timeout=5) except Exception as e: From faf4403e263baca3f5d446f4f458d5f5924dd659 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 27 Sep 2022 03:24:20 +0200 Subject: [PATCH 3/3] check for duplicate images when admigger threads for banners/sidebar/badges are used --- files/helpers/media.py | 36 ++++++++++++++++++++++++++++++++---- files/routes/errors.py | 4 ++++ requirements.txt | 1 + 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/files/helpers/media.py b/files/helpers/media.py index 690e038f9..d9c891b9b 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -8,7 +8,7 @@ import requests import time from .const import * import gevent - +import imagehash def process_files(): body = '' @@ -103,8 +103,36 @@ def process_image(filename=None, resize=0, trim=False): i = ImageOps.exif_transpose(i) i.save(filename, format="WEBP", method=6, quality=88) - if resize in (400,1200) and os.stat(filename).st_size > 1 * 1024 * 1024: - os.remove(filename) - abort(413) + + if resize in (300,400,1200): + if os.stat(filename).st_size > 1 * 1024 * 1024: + os.remove(filename) + abort(413) + + 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' + + 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)) + 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(417) return filename \ No newline at end of file diff --git a/files/routes/errors.py b/files/routes/errors.py index 54f9f5923..03fb8441b 100644 --- a/files/routes/errors.py +++ b/files/routes/errors.py @@ -64,6 +64,10 @@ def error_415(e): if request.headers.get("Authorization") or request.headers.get("xhr"): return {"error": "Please upload only Image, Video, or Audio files!"}, 415 else: return render_template('errors/415.html', err=True), 415 +@app.errorhandler(417) +def error_417(e): + return {"error": "Image already exists!"}, 417 + @app.errorhandler(418) def error_418(e): if request.headers.get("Authorization") or request.headers.get("xhr"): diff --git a/requirements.txt b/requirements.txt index 141069f0f..fb8a40bf5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,6 +11,7 @@ gevent gevent-websocket greenlet gunicorn +imagehash lxml mistletoe matplotlib