ovdashboard/api/ovdashboard_api/routers/image.py

107 lines
2.3 KiB
Python
Raw Normal View History

2022-09-05 12:54:02 +00:00
"""
Router "image" provides:
- listing image files
- finding image files by name prefix
- getting image files in a uniform format by name prefix
"""
2022-08-31 10:21:24 +00:00
import re
from io import BytesIO
2022-09-08 14:02:50 +00:00
from logging import getLogger
from typing import Iterator
2022-08-31 10:21:24 +00:00
2022-09-02 14:51:11 +00:00
from fastapi import APIRouter, Depends
2022-08-31 10:21:24 +00:00
from fastapi.responses import StreamingResponse
from PIL import Image
2022-09-15 22:32:11 +00:00
from ..config import Config, ImageUIConfig
2022-09-08 14:02:50 +00:00
from ..dav_common import webdav_ensure_path
2022-08-31 10:21:24 +00:00
from ..dav_file import DavFile
2022-09-04 14:14:22 +00:00
from ._common import FileNameLister, PrefixFinder, PrefixUnique
2022-08-31 10:21:24 +00:00
2022-09-08 14:02:50 +00:00
_logger = getLogger(__name__)
2022-08-31 10:21:24 +00:00
router = APIRouter(prefix="/image", tags=["image"])
2022-09-06 23:50:42 +00:00
image_lister = FileNameLister(
2022-09-08 14:02:50 +00:00
path_name="image_dir",
2022-09-02 13:22:35 +00:00
re=re.compile(
r"\.(gif|jpe?g|tiff?|png|bmp)$",
flags=re.IGNORECASE,
),
2022-08-31 10:21:24 +00:00
)
2022-09-06 23:50:42 +00:00
image_finder = PrefixFinder(image_lister)
image_unique = PrefixUnique(image_finder)
2022-08-31 10:21:24 +00:00
2022-09-08 14:02:50 +00:00
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
webdav_ensure_path(await image_lister.remote_path)
2022-09-05 00:23:00 +00:00
@router.get(
"/list",
response_model=list[str],
2022-09-06 23:50:42 +00:00
responses=image_lister.responses,
2022-09-05 00:23:00 +00:00
)
2022-08-31 10:21:24 +00:00
async def list_images(
2022-09-06 23:50:42 +00:00
names: Iterator[str] = Depends(image_lister),
2022-08-31 10:21:24 +00:00
) -> list[str]:
2022-09-04 14:14:22 +00:00
return list(names)
2022-08-31 10:21:24 +00:00
2022-09-05 00:23:00 +00:00
@router.get(
"/find/{prefix}",
response_model=list[str],
2022-09-06 23:50:42 +00:00
responses=image_finder.responses,
2022-09-05 00:23:00 +00:00
)
2022-08-31 10:21:24 +00:00
async def find_images(
2022-09-06 23:50:42 +00:00
names: Iterator[str] = Depends(image_finder),
2022-08-31 10:21:24 +00:00
) -> list[str]:
2022-09-04 14:14:22 +00:00
return list(names)
2022-08-31 10:21:24 +00:00
@router.get(
"/get/{prefix}",
2022-08-31 14:03:29 +00:00
response_class=StreamingResponse,
2022-09-06 23:50:42 +00:00
responses=image_unique.responses,
2022-08-31 10:21:24 +00:00
)
async def get_image(
prefix: str,
2022-09-06 23:50:42 +00:00
name: str = Depends(image_unique),
2022-09-08 00:24:36 +00:00
) -> StreamingResponse:
cfg = await Config.get()
2022-09-08 14:02:50 +00:00
dav_file = DavFile(f"{await image_lister.remote_path}/{name}")
img = Image.open(
2022-09-08 00:24:36 +00:00
BytesIO(await dav_file.as_bytes)
).convert(
cfg.image.mode
)
img_buffer = BytesIO()
img.save(img_buffer, **cfg.image.save_params)
2022-08-31 10:21:24 +00:00
img_buffer.seek(0)
return StreamingResponse(
2022-08-31 10:41:36 +00:00
content=img_buffer,
2022-08-31 10:21:24 +00:00
media_type="image/jpeg",
headers={
"Content-Disposition": f"filename={prefix}.jpg"
},
)
2022-09-15 22:32:11 +00:00
@router.get(
"/config",
response_model=ImageUIConfig,
)
async def get_ui_config(
cfg: Config = Depends(Config.get),
) -> ImageUIConfig:
return cfg.image