load background image from WebDAV

This commit is contained in:
Jörn-Michael Miehe 2023-09-04 21:23:27 +00:00
parent f7754ec901
commit dd519ec3cc
7 changed files with 50 additions and 18 deletions

View file

@ -11,6 +11,7 @@ class User(BaseModel):
class Puzzle(BaseModel):
background: str
solution: str

View file

@ -1,10 +1,12 @@
import itertools
import random
import re
from io import BytesIO
from typing import Any, Self, Sequence
from fastapi import Depends
from PIL import ImageFont
from fastapi.responses import StreamingResponse
from PIL import Image, ImageFont
from ..config import Config, get_config
from ..dav_common import dav_file_exists, dav_get_file, dav_list_files
@ -122,3 +124,22 @@ async def get_image(
except RuntimeError:
# Erstelle automatisch generiertes Bild
return await get_auto_image(index=index, letter=letter, images=images)
async def api_return_image(
img: Image.Image,
) -> StreamingResponse:
"""
Bild mit API zurückgeben
"""
# Bilddaten in Puffer laden
img_buffer = BytesIO()
img.save(img_buffer, format="JPEG", quality=85)
img_buffer.seek(0)
# zurückgeben
return StreamingResponse(
content=img_buffer,
media_type="image/jpeg",
)

View file

@ -1,12 +1,11 @@
from datetime import date
from io import BytesIO
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse
from ..config import Config, get_config
from ._image import AdventImage
from ._misc import get_image, shuffle
from ._misc import api_return_image, get_image, shuffle
from .user import user_is_admin
router = APIRouter(prefix="/days", tags=["days"])
@ -67,12 +66,4 @@ async def get_image_for_day(
if not (can_view or is_admin):
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
# Bilddaten in Puffer laden
img_buffer = BytesIO()
image.img.save(img_buffer, format="JPEG", quality=85)
img_buffer.seek(0)
return StreamingResponse(
content=img_buffer,
media_type="image/jpeg",
)
return await api_return_image(image.img)

View file

@ -1,3 +1,25 @@
from fastapi import APIRouter
from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from PIL import Image
from ..config import Config, get_config
from ..dav_common import dav_get_file
from ._misc import api_return_image
router = APIRouter(prefix="/general", tags=["general"])
@router.get(
"/background",
response_class=StreamingResponse,
)
async def get_image_for_day(
cfg: Config = Depends(get_config),
) -> StreamingResponse:
"""
Hintergrundbild laden
"""
return await api_return_image(
Image.open(await dav_get_file(f"files/{cfg.puzzle.background}"))
)

BIN
ui/src/assets/adventskalender.jpg (Stored with Git LFS)

Binary file not shown.

View file

@ -11,7 +11,7 @@
</ul>
</div>
<figure class="image">
<img src="@/assets/adventskalender.jpg" />
<img :src="$advent22.api_url('general/background')" />
<ThouCanvas>
<PreviewDoor
v-for="(_, index) in doors"

View file

@ -9,7 +9,7 @@
</ul>
</div>
<figure class="image">
<img src="@/assets/adventskalender.jpg" />
<img :src="$advent22.api_url('general/background')" />
<RectangleCanvas
:rectangles="rectangles"
@draw="on_draw"