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

108 lines
2.4 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
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-23 21:32:25 +00:00
from ._common import filter_prefix, filter_prefix_unique, get_remote_path, list_files
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"])
2023-10-23 21:32:25 +00:00
_ls = list_files(
path_name=_PATH_NAME,
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
)
2023-10-23 21:32:25 +00:00
_rp = get_remote_path(path_name=_PATH_NAME)
_fp = filter_prefix(_ls.func)
_fpu = filter_prefix_unique(_fp.func)
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.")
2023-10-23 21:32:25 +00:00
remote_path = await _rp.func()
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",
response_model=list[str],
2023-10-23 21:32:25 +00:00
responses=_ls.responses,
2022-09-05 00:23:00 +00:00
)
2023-10-23 21:32:25 +00:00
async def list_all_images(
names: list[str] = Depends(_ls.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}",
response_model=list[str],
2023-10-23 21:32:25 +00:00
responses=_fp.responses,
2022-09-05 00:23:00 +00:00
)
2023-10-23 21:32:25 +00:00
async def find_images_by_prefix(
names: list[str] = Depends(_fp.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}",
2022-08-31 14:03:29 +00:00
response_class=StreamingResponse,
2023-10-23 21:32:25 +00:00
responses=_fpu.responses,
2022-08-31 10:21:24 +00:00
)
2023-10-23 21:32:25 +00:00
async def get_image_by_prefix(
2022-08-31 10:21:24 +00:00
prefix: str,
2023-10-23 21:32:25 +00:00
remote_path: str = Depends(_rp.func),
name: str = Depends(_fpu.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-17 12:55:38 +00:00
headers={"Content-Disposition": f"filename={prefix}.jpg"},
2022-08-31 10:21:24 +00:00
)
2022-09-15 22:32:11 +00:00
@router.get(
"/config",
response_model=ImageUIConfig,
)
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