forked from rDrama/rDrama
check for duplicate images when admigger threads for banners/sidebar/badges are used
parent
cc85cab01d
commit
faf4403e26
|
@ -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:
|
||||
|
||||
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
|
|
@ -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"):
|
||||
|
|
|
@ -11,6 +11,7 @@ gevent
|
|||
gevent-websocket
|
||||
greenlet
|
||||
gunicorn
|
||||
imagehash
|
||||
lxml
|
||||
mistletoe
|
||||
matplotlib
|
||||
|
|
Loading…
Reference in New Issue