ovdashboard/api/ovdashboard_api/routers/v1/image.py

89 lines
2.1 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
"""
from io import BytesIO
2022-09-08 14:02:50 +00:00
from logging import getLogger
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
2023-10-20 11:12:53 +00:00
from ...core.config import Config, ImageUIConfig, get_config
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
from ...core.webdav import WebDAV
2023-10-26 14:31:12 +00:00
from ._common import LM_IMAGE, RP_IMAGE
2022-08-31 10:21:24 +00:00
2022-09-08 14:02:50 +00:00
_logger = getLogger(__name__)
2023-10-23 21:32:25 +00:00
_PATH_NAME = "image_dir"
2022-09-08 14:02:50 +00:00
2022-08-31 10:21:24 +00:00
router = APIRouter(prefix="/image", tags=["image"])
2022-09-08 14:02:50 +00:00
@router.on_event("startup")
async def start_router() -> None:
_logger.debug(f"{router.prefix} router starting.")
2023-10-26 14:31:12 +00:00
remote_path = await RP_IMAGE()
2023-10-23 21:32:25 +00:00
if not webdav_ensure_path(remote_path):
2022-09-19 11:42:43 +00:00
webdav_ensure_files(
2023-10-23 21:32:25 +00:00
remote_path,
2022-09-19 11:42:43 +00:00
"img1.jpg",
"img2.jpg",
"img3.jpg",
)
2022-09-08 14:02:50 +00:00
2022-09-05 00:23:00 +00:00
@router.get(
"/list",
2023-10-26 14:31:12 +00:00
responses=LM_IMAGE.lister.responses,
2022-09-05 00:23:00 +00:00
)
2023-10-23 21:32:25 +00:00
async def list_all_images(
2023-10-26 14:31:12 +00:00
names: list[str] = Depends(LM_IMAGE.lister.func),
2022-08-31 10:21:24 +00:00
) -> list[str]:
2023-10-23 21:32:25 +00:00
return names
2022-08-31 10:21:24 +00:00
2022-09-05 00:23:00 +00:00
@router.get(
"/find/{prefix}",
2023-10-26 14:31:12 +00:00
responses=LM_IMAGE.filter.responses,
2022-09-05 00:23:00 +00:00
)
2023-10-23 21:32:25 +00:00
async def find_images_by_prefix(
2023-10-26 14:31:12 +00:00
names: list[str] = Depends(LM_IMAGE.filter.func),
2022-08-31 10:21:24 +00:00
) -> list[str]:
2023-10-23 21:32:25 +00:00
return names
2022-08-31 10:21:24 +00:00
@router.get(
"/get/{prefix}",
2023-10-26 14:31:12 +00:00
responses=LM_IMAGE.getter.responses,
2023-10-24 17:39:43 +00:00
response_class=StreamingResponse,
2022-08-31 10:21:24 +00:00
)
2023-10-23 21:32:25 +00:00
async def get_image_by_prefix(
2023-10-26 14:31:12 +00:00
remote_path: str = Depends(RP_IMAGE),
name: str = Depends(LM_IMAGE.getter.func),
2022-09-08 00:24:36 +00:00
) -> StreamingResponse:
2023-10-20 11:12:53 +00:00
cfg = await get_config()
2023-10-23 21:32:25 +00:00
img = Image.open(BytesIO(await WebDAV.read_bytes(f"{remote_path}/{name}")))
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",
2023-10-26 13:58:02 +00:00
headers={"Content-Disposition": f"filename={name}.jpg"},
2022-08-31 10:21:24 +00:00
)
2022-09-15 22:32:11 +00:00
2023-10-24 17:39:43 +00:00
@router.get("/config")
2022-09-15 22:32:11 +00:00
async def get_ui_config(
2023-10-20 11:12:53 +00:00
cfg: Config = Depends(get_config),
2022-09-15 22:32:11 +00:00
) -> ImageUIConfig:
return cfg.image