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): class Puzzle(BaseModel):
background: str
solution: str solution: str

View file

@ -1,10 +1,12 @@
import itertools import itertools
import random import random
import re import re
from io import BytesIO
from typing import Any, Self, Sequence from typing import Any, Self, Sequence
from fastapi import Depends 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 ..config import Config, get_config
from ..dav_common import dav_file_exists, dav_get_file, dav_list_files from ..dav_common import dav_file_exists, dav_get_file, dav_list_files
@ -122,3 +124,22 @@ async def get_image(
except RuntimeError: except RuntimeError:
# Erstelle automatisch generiertes Bild # Erstelle automatisch generiertes Bild
return await get_auto_image(index=index, letter=letter, images=images) 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 datetime import date
from io import BytesIO
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from ..config import Config, get_config from ..config import Config, get_config
from ._image import AdventImage 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 from .user import user_is_admin
router = APIRouter(prefix="/days", tags=["days"]) router = APIRouter(prefix="/days", tags=["days"])
@ -67,12 +66,4 @@ async def get_image_for_day(
if not (can_view or is_admin): if not (can_view or is_admin):
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!") raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Wie unhöflich!!!")
# Bilddaten in Puffer laden return await api_return_image(image.img)
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",
)

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

View file

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