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
|
2022-09-02 12:20:29 +00:00
|
|
|
from io import BytesIO
|
2022-09-08 14:02:50 +00:00
|
|
|
from logging import getLogger
|
2022-09-02 12:20:29 +00:00
|
|
|
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
|
|
|
|
|
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
|
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.")
|
|
|
|
|
2022-09-19 11:42:43 +00:00
|
|
|
if not webdav_ensure_path(await image_lister.remote_path):
|
|
|
|
webdav_ensure_files(
|
|
|
|
await image_lister.remote_path,
|
|
|
|
"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],
|
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:
|
2023-10-20 11:12:53 +00:00
|
|
|
cfg = await get_config()
|
|
|
|
img = Image.open(
|
|
|
|
BytesIO(await WebDAV.read_bytes(f"{await image_lister.remote_path}/{name}"))
|
|
|
|
)
|
2022-09-02 12:20:29 +00:00
|
|
|
|
|
|
|
img_buffer = BytesIO()
|
2022-09-06 00:16:24 +00:00
|
|
|
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
|