68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
import base64
|
|
from pathlib import Path
|
|
from fastapi import Request, APIRouter as FastAPI, Depends
|
|
from fastapi.exceptions import HTTPException
|
|
from responses.media import MediaResponse
|
|
from responses import UnprocessableEntity, Response as APIResponse, NotFound
|
|
from config import MANAGER, LIMITER
|
|
from orm.media import Media, PublicMediaModel, MediaType
|
|
from orm.users import UserModel
|
|
|
|
router = FastAPI()
|
|
|
|
|
|
@router.get(
|
|
"/media/{media_id}",
|
|
tags=["Media"],
|
|
status_code=200,
|
|
responses={
|
|
200: {"model": MediaResponse},
|
|
422: {"model": UnprocessableEntity},
|
|
404: {"model": NotFound},
|
|
},
|
|
)
|
|
@LIMITER.limit("2/second")
|
|
async def get_media(request: Request, media_id: str, _user: UserModel = Depends(MANAGER)):
|
|
"""
|
|
Gets a media object by its ID
|
|
"""
|
|
|
|
if (m := await Media.select(Media.media_id).where(Media.media_id == media_id).first()) is None:
|
|
raise HTTPException(status_code=404, detail="Media not found")
|
|
m = Media(**m)
|
|
if m.media_type == MediaType.FILE:
|
|
with Path(m.content).open("rb") as f:
|
|
m.content = base64.b64encode(f.read())
|
|
return MediaResponse(
|
|
data=PublicMediaModel(
|
|
media_id=m.media_id,
|
|
content=m.content,
|
|
content_type=m.content_type,
|
|
creation_date=m.creation_date,
|
|
)
|
|
)
|
|
|
|
|
|
@router.get(
|
|
"/media/{media_id}/report",
|
|
tags=["Media"],
|
|
status_code=200,
|
|
responses={
|
|
200: {"model": APIResponse},
|
|
422: {"model": UnprocessableEntity},
|
|
404: {"model": NotFound},
|
|
},
|
|
)
|
|
@LIMITER.limit("2/second")
|
|
async def report_media(request: Request, media_id: str, _user: UserModel = Depends(MANAGER)):
|
|
"""
|
|
Reports a piece of media by its ID. This creates
|
|
a report that can be seen by admins, which can
|
|
then decide what to do
|
|
"""
|
|
|
|
if (m := await Media.select(Media.media_id).where(Media.media_id == media_id).first()) is None:
|
|
raise HTTPException(status_code=404, detail="Media not found")
|
|
# TODO: Create report
|
|
return APIResponse(msg="Success")
|