2023-08-06 10:47:07 +00:00
|
|
|
from sanic.response import html, redirect
|
2022-12-19 21:07:53 +00:00
|
|
|
from sanic import Blueprint, exceptions
|
|
|
|
from ext import *
|
2023-08-06 10:47:07 +00:00
|
|
|
from config import PROPIC_DEADLINE
|
2022-12-19 21:07:53 +00:00
|
|
|
from PIL import Image
|
|
|
|
from io import BytesIO
|
|
|
|
from hashlib import sha224
|
2023-05-08 20:08:44 +00:00
|
|
|
from time import time
|
2024-01-08 21:02:27 +00:00
|
|
|
import os
|
2022-12-19 21:07:53 +00:00
|
|
|
|
|
|
|
bp = Blueprint("propic", url_prefix="/manage/propic")
|
|
|
|
|
2023-12-30 10:27:42 +00:00
|
|
|
async def resetDefaultPropic(request, order: Order, isFursuiter, sendAnswer=True):
|
|
|
|
s = "_fursuiter" if isFursuiter else ""
|
2024-01-08 21:02:27 +00:00
|
|
|
if (DEV_MODE):
|
|
|
|
print("Resetting {fn} picture for {orderCod}".format(fn="Badge" if not isFursuiter else "fursuit", orderCod = order.code))
|
2023-12-30 10:27:42 +00:00
|
|
|
with open("res/propic/default.png", "rb") as f:
|
|
|
|
data = f.read()
|
2024-01-08 21:02:27 +00:00
|
|
|
f.close()
|
2024-01-09 22:42:54 +00:00
|
|
|
|
|
|
|
convertedFilename = order.ans(f'propic{s}')
|
|
|
|
if convertedFilename is not None:
|
|
|
|
convertedFilename = f"res/propic/{convertedFilename}"
|
|
|
|
if os.path.exists(convertedFilename):
|
|
|
|
os.remove(convertedFilename) # converted file
|
|
|
|
originalFilename = f"res/propic/propic{s}_{order.code}_original"
|
|
|
|
if os.path.exists(originalFilename):
|
|
|
|
os.remove(originalFilename) # original file
|
|
|
|
|
2023-12-30 10:27:42 +00:00
|
|
|
await order.edit_answer_fileUpload(f'propic{s}_file', f'propic{s}_file_{order.code}_default.png', 'image/png', data)
|
|
|
|
if(sendAnswer):
|
|
|
|
await order.send_answers()
|
|
|
|
|
2022-12-19 21:07:53 +00:00
|
|
|
@bp.post("/upload")
|
|
|
|
async def upload_propic(request, order: Order):
|
|
|
|
if not order: raise exceptions.Forbidden("You have been logged out. Please access the link in your E-Mail to login again!")
|
|
|
|
|
2023-01-19 16:03:38 +00:00
|
|
|
if order.propic_locked:
|
|
|
|
raise exceptions.BadRequest("You have been limited from further editing the propic.")
|
|
|
|
|
2023-05-23 20:15:01 +00:00
|
|
|
if request.form.get('submit') != 'Upload' and time() > PROPIC_DEADLINE:
|
|
|
|
raise exceptions.BadRequest("The deadline has passed. You cannot modify the badges at this moment.")
|
|
|
|
|
2022-12-19 21:07:53 +00:00
|
|
|
if request.form.get('submit') == 'Delete main image':
|
2023-12-30 10:27:42 +00:00
|
|
|
await resetDefaultPropic(request, order, False, sendAnswer=False)
|
2024-01-09 22:42:54 +00:00
|
|
|
await order.edit_answer('propic', None) #This MUST come after the reset default propic!
|
2023-05-23 20:15:01 +00:00
|
|
|
elif request.form.get('submit') == 'Delete fursuit image':
|
2023-12-30 10:27:42 +00:00
|
|
|
await resetDefaultPropic(request, order, True, sendAnswer=False)
|
2024-01-09 22:42:54 +00:00
|
|
|
await order.edit_answer('propic_fursuiter', None) #This MUST come after the reset default propic!
|
2023-05-23 20:15:01 +00:00
|
|
|
else:
|
|
|
|
for fn, body in request.files.items():
|
|
|
|
if fn not in ['propic', 'propic_fursuiter']:
|
|
|
|
continue
|
2022-12-19 21:07:53 +00:00
|
|
|
|
2023-05-23 20:15:01 +00:00
|
|
|
if not body[0].body: continue
|
2022-12-21 19:58:33 +00:00
|
|
|
|
2024-01-08 21:02:27 +00:00
|
|
|
# Check max file size
|
|
|
|
if len(body[0].body) > PROPIC_MAX_FILE_SIZE:
|
|
|
|
raise exceptions.BadRequest("File size too large for " + ("Profile picture" if fn == 'propic' else 'Fursuit picture'))
|
2023-05-23 20:15:01 +00:00
|
|
|
|
2024-01-08 21:02:27 +00:00
|
|
|
h = sha224(body[0].body).hexdigest()[:32]
|
|
|
|
errorDetails = ''
|
2023-05-23 20:15:01 +00:00
|
|
|
try:
|
|
|
|
img = Image.open(BytesIO(body[0].body))
|
2024-01-08 21:02:27 +00:00
|
|
|
width, height = img.size
|
|
|
|
# Checking for min / max size
|
|
|
|
if width < PROPIC_MIN_SIZE[0] or height < PROPIC_MIN_SIZE[1]:
|
|
|
|
errorDetails = "Image too small [{width}x{width}] for {pfpn}".format(width=width, pfpn=("Profile picture" if fn == 'propic' else 'Fursuit picture'))
|
|
|
|
raise exceptions.BadRequest(errorDetails)
|
|
|
|
|
|
|
|
if width > PROPIC_MAX_SIZE[0] or height > PROPIC_MAX_SIZE[1]:
|
|
|
|
errorDetails = "Image too big [{width}x{width}] for {pfpn}".format(width=width, pfpn=("Profile picture" if fn == 'propic' else 'Fursuit picture'))
|
|
|
|
raise exceptions.BadRequest(errorDetails)
|
|
|
|
|
2022-12-21 19:58:33 +00:00
|
|
|
|
2024-01-09 22:42:54 +00:00
|
|
|
with open(f"res/propic/{fn}_{order.code}_original", "wb") as f:
|
2023-05-23 20:15:01 +00:00
|
|
|
f.write(body[0].body)
|
|
|
|
|
|
|
|
aspect_ratio = width/height
|
|
|
|
if aspect_ratio > 1:
|
|
|
|
crop_amount = (width - height) / 2
|
|
|
|
img = img.crop((crop_amount, 0, width - crop_amount, height))
|
|
|
|
elif aspect_ratio < 1:
|
|
|
|
crop_amount = (height - width) / 2
|
|
|
|
img = img.crop((0, crop_amount, width, height - crop_amount))
|
|
|
|
|
|
|
|
img = img.convert('RGB')
|
2024-01-08 21:02:27 +00:00
|
|
|
width, height = img.size
|
|
|
|
|
2023-05-23 20:15:01 +00:00
|
|
|
img.thumbnail((512,512))
|
2023-12-30 10:27:42 +00:00
|
|
|
imgBytes = BytesIO()
|
|
|
|
img.save(imgBytes, format='jpeg')
|
|
|
|
imgBytes = imgBytes.getvalue()
|
|
|
|
with open(f"res/propic/{fn}_{order.code}_{h}.jpg", "wb") as f:
|
|
|
|
f.write(imgBytes)
|
|
|
|
|
|
|
|
await order.edit_answer_fileUpload(f'{fn}_file', f'{fn}_file_{order.code}_{h}.jpg', 'image/jpeg', imgBytes)
|
|
|
|
except Exception:
|
|
|
|
import traceback
|
2024-01-08 21:02:27 +00:00
|
|
|
if DEV_MODE: print(traceback.format_exc())
|
|
|
|
raise exceptions.BadRequest(errorDetails if errorDetails else "The image you uploaded is not valid.")
|
2023-05-23 20:15:01 +00:00
|
|
|
else:
|
|
|
|
await order.edit_answer(fn, f"{fn}_{order.code}_{h}.jpg")
|
|
|
|
|
2023-08-06 13:08:27 +00:00
|
|
|
await order.send_answers()
|
2022-12-19 21:07:53 +00:00
|
|
|
return redirect("/manage/welcome#badge")
|