add profile picture support and rearrange things to be slightly more sane
This commit is contained in:
parent
9148e2068f
commit
15e1018476
6 changed files with 100 additions and 10 deletions
9
cef_3M/endpoints/__init__.py
Normal file
9
cef_3M/endpoints/__init__.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
from fastapi import APIRouter
|
||||
import os
|
||||
import importlib
|
||||
router = APIRouter()
|
||||
|
||||
for module in os.listdir(os.path.dirname(__file__)):
|
||||
if module == '__init__.py' or module[-3:] != '.py':
|
||||
continue
|
||||
importlib.import_module("."+module[:-3], package="cef_3M.endpoints")
|
||||
66
cef_3M/endpoints/pfp.py
Normal file
66
cef_3M/endpoints/pfp.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
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}"}
|
||||
Loading…
Add table
Add a link
Reference in a new issue