furizon_webint/propic.py

64 lines
2.1 KiB
Python
Raw Normal View History

2022-12-19 21:07:53 +00:00
from sanic.response import html, redirect, text
from sanic import Blueprint, exceptions
from random import choice
from ext import *
2023-05-08 20:08:44 +00:00
from config import headers, PROPIC_DEADLINE
2022-12-19 21:07:53 +00:00
from PIL import Image
from os.path import isfile
from os import unlink
from io import BytesIO
from hashlib import sha224
2023-05-08 20:08:44 +00:00
from time import time
2022-12-19 21:07:53 +00:00
bp = Blueprint("propic", url_prefix="/manage/propic")
@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!")
if order.propic_locked:
raise exceptions.BadRequest("You have been limited from further editing the propic.")
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':
await order.edit_answer('propic', None)
elif request.form.get('submit') == 'Delete fursuit image':
2022-12-19 21:07:53 +00:00
await order.edit_answer('propic_fursuiter', None)
else:
for fn, body in request.files.items():
if fn not in ['propic', 'propic_fursuiter']:
continue
2022-12-19 21:07:53 +00:00
if not body[0].body: continue
2022-12-21 19:58:33 +00:00
h = sha224(body[0].body).hexdigest()[:32]
try:
img = Image.open(BytesIO(body[0].body))
2022-12-21 19:58:33 +00:00
with open(f"res/propic/{fn}_{order.code}_original", "wb") as f:
f.write(body[0].body)
width, height = img.size
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')
img.thumbnail((512,512))
img.save(f"res/propic/{fn}_{order.code}_{h}.jpg")
except:
raise exceptions.BadRequest("The image you uploaded is not valid.")
else:
await order.edit_answer(fn, f"{fn}_{order.code}_{h}.jpg")
await order.send_answers()
2022-12-19 21:07:53 +00:00
return redirect("/manage/welcome#badge")