check for duplicate images when admigger threads for banners/sidebar/badges are used

remotes/1693176582716663532/tmp_refs/heads/watchparty
Aevann1 2022-09-27 03:24:20 +02:00
parent cc85cab01d
commit faf4403e26
3 changed files with 37 additions and 4 deletions

View File

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

View File

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

View File

@ -11,6 +11,7 @@ gevent
gevent-websocket
greenlet
gunicorn
imagehash
lxml
mistletoe
matplotlib