import re from io import BytesIO from typing import Iterator from fastapi import APIRouter, Depends from fastapi.responses import StreamingResponse from PIL import Image from ..dav_file import DavFile from ._common import FileNameLister, PrefixFinder, PrefixUnique router = APIRouter(prefix="/image", tags=["image"]) _lister = FileNameLister( remote_path="img", re=re.compile( r"\.(gif|jpe?g|tiff?|png|bmp)$", flags=re.IGNORECASE, ), ) _finder = PrefixFinder(_lister) _unique = PrefixUnique(_finder) @router.get("/list", response_model=list[str]) async def list_images( names: Iterator[str] = Depends(_lister), ) -> list[str]: return list(names) @router.get("/find/{prefix}", response_model=list[str]) async def find_images( names: Iterator[str] = Depends(_finder), ) -> list[str]: return list(names) @router.get( "/get/{prefix}", response_class=StreamingResponse, responses=_unique.responses, ) async def get_image( prefix: str, name: str = Depends(_unique), ) -> str: dav_file = DavFile(f"{_lister.remote_path}/{name}") img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB") img_buffer = BytesIO() img.save(img_buffer, format='JPEG', quality=85) img_buffer.seek(0) return StreamingResponse( content=img_buffer, media_type="image/jpeg", headers={ "Content-Disposition": f"filename={prefix}.jpg" }, )