advent22/api/advent22_api/routers/images.py

51 lines
1.3 KiB
Python
Raw Normal View History

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse
from PIL import Image
from ..core.calendar_config import CalendarConfig, get_calendar_config
from ..core.config import get_config
from ..core.depends import get_day_image
from ..core.helpers import api_return_image, load_image
from ._security import user_can_view_day, user_is_admin
router = APIRouter(prefix="/images", tags=["images"])
@router.on_event("startup")
async def startup() -> None:
cfg = await get_config()
print(cfg.puzzle.solution)
@router.get(
"/background",
response_class=StreamingResponse,
)
async def get_background(
cal_cfg: CalendarConfig = Depends(get_calendar_config),
) -> StreamingResponse:
"""
Hintergrundbild laden
"""
return await api_return_image(await load_image(f"files/{cal_cfg.background}"))
@router.get(
"/{day}",
response_class=StreamingResponse,
)
async def get_image_for_day(
image: Image.Image = Depends(get_day_image),
can_view: bool = Depends(user_can_view_day),
is_admin: bool = Depends(user_is_admin),
) -> StreamingResponse:
"""
Bild für einen Tag erstellen
"""
if not (can_view or is_admin):
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
return await api_return_image(image)