66 lines
No EOL
2.1 KiB
Python
66 lines
No EOL
2.1 KiB
Python
import mimetypes
|
|
import time
|
|
|
|
from . import router
|
|
from fastapi import UploadFile, Request, Depends
|
|
|
|
from ..util import minioClient
|
|
from ..auth import JWTBearer
|
|
import config
|
|
|
|
from pywuffs import ImageDecoderType
|
|
from pywuffs.aux import (
|
|
ImageDecoder,
|
|
ImageDecoderConfig,
|
|
)
|
|
pfpConfig = ImageDecoderConfig()
|
|
pfpConfig.max_incl_dimension = 400
|
|
pfpConfig.enabled_decoders = [
|
|
ImageDecoderType.GIF,
|
|
ImageDecoderType.PNG,
|
|
ImageDecoderType.JPEG,
|
|
]
|
|
|
|
iconConfig = ImageDecoderConfig()
|
|
iconConfig.max_incl_dimension = 24
|
|
iconConfig.enabled_decoders = [
|
|
ImageDecoderType.PNG,
|
|
]
|
|
|
|
@router.post("/pfp/upload", dependencies=[Depends(JWTBearer())])
|
|
async def pfpUpload(file: UploadFile, request: Request):
|
|
if file.size > config.MAX_PFP_SIZE:
|
|
return {"error": "file too big"}
|
|
whoami = request.state.jwt
|
|
username = whoami["account"].lower()
|
|
|
|
# It's not the path I exactly wanted, but this will have to do - WUFFS ensures that the file is valid then we just save it to the server
|
|
# I hope there's no issue in doing that...
|
|
decoder = ImageDecoder(pfpConfig)
|
|
data = await file.read()
|
|
decoded = decoder.decode(data)
|
|
if decoded.error_message:
|
|
return {"error": "invalid file"}
|
|
file.file.seek(0)
|
|
|
|
mime = mimetypes.guess_type(file.filename)
|
|
minioClient.put_object("pfp", username, file.file, file.size, content_type=mime[0])
|
|
return {"url": f"https://{config.MINIO_ADDR}/pfp/{username}?{time.time():.0f}"}
|
|
|
|
@router.post("/pfp/uploadIcon", dependencies=[Depends(JWTBearer())])
|
|
async def pfpUpload(file: UploadFile, request: Request):
|
|
if file.size > config.MAX_PFP_SIZE:
|
|
return {"error": "file too big"}
|
|
whoami = request.state.jwt
|
|
username = whoami["account"].lower()
|
|
|
|
decoder = ImageDecoder(iconConfig)
|
|
data = await file.read()
|
|
decoded = decoder.decode(data)
|
|
if decoded.error_message:
|
|
return {"error": "invalid file"}
|
|
file.file.seek(0)
|
|
|
|
mime = mimetypes.guess_type(file.filename)
|
|
minioClient.put_object("pfp", username+"/icon", file.file, file.size, content_type=mime[0])
|
|
return {"url": f"https://{config.MINIO_ADDR}/pfp/{username}/icon?{time.time():.0f}"} |