diff --git a/api/ovdashboard_api/routers/_common.py b/api/ovdashboard_api/routers/_common.py index 4dcf3dd..3589147 100644 --- a/api/ovdashboard_api/routers/_common.py +++ b/api/ovdashboard_api/routers/_common.py @@ -2,9 +2,11 @@ import re from dataclasses import dataclass from typing import Iterator +from fastapi import HTTPException, status from webdav3.exceptions import RemoteResourceNotFound from .. import CLIENT +from ..dav_file import DavFile @dataclass(frozen=True) @@ -36,3 +38,37 @@ class FilePrefixFinder: for file_name in self.lister() if file_name.lower().startswith(prefix.lower()) ) + + +@dataclass(frozen=True) +class FilePrefixLoader: + finder: FilePrefixFinder + + @property + def responses(self) -> dict: + return { + status.HTTP_200_OK: { + "description": "Operation successful", + }, + status.HTTP_404_NOT_FOUND: { + "description": "file not found in " + + repr(self.finder.lister.remote_path), + "content": None, + }, + status.HTTP_409_CONFLICT: { + "description": "ambiguous file name for " + + repr(self.finder.lister.remote_path), + "content": None, + }, + } + + def __call__(self, prefix: str) -> DavFile: + file_names = list(self.finder(prefix)) + + if not (file_names): + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + + elif len(file_names) > 1: + raise HTTPException(status_code=status.HTTP_409_CONFLICT) + + return DavFile(f"{self.finder.lister.remote_path}/{file_names[0]}") diff --git a/api/ovdashboard_api/routers/image.py b/api/ovdashboard_api/routers/image.py index c5ccf07..f6c7283 100644 --- a/api/ovdashboard_api/routers/image.py +++ b/api/ovdashboard_api/routers/image.py @@ -2,12 +2,12 @@ import re from io import BytesIO from typing import Iterator -from fastapi import APIRouter, Depends, HTTPException, status +from fastapi import APIRouter, Depends from fastapi.responses import StreamingResponse from PIL import Image from ..dav_file import DavFile -from ._common import FileNameLister, FilePrefixFinder +from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader router = APIRouter(prefix="/image", tags=["image"]) @@ -20,6 +20,7 @@ _lister = FileNameLister( ) _finder = FilePrefixFinder(_lister) +_loader = FilePrefixLoader(_finder) @router.get("/list", response_model=list[str]) @@ -39,34 +40,13 @@ async def find_images( @router.get( "/get/{prefix}", response_class=StreamingResponse, - responses={ - status.HTTP_200_OK: { - "description": "Operation successful", - }, - status.HTTP_404_NOT_FOUND: { - "description": "image file not found", - "content": None, - }, - status.HTTP_409_CONFLICT: { - "description": "ambiguous image file name", - "content": None, - }, - }, + responses=_loader.responses, ) async def get_image( prefix: str, - file_names: Iterator[str] = Depends(_finder), -) -> StreamingResponse: - file_names = list(file_names) - - if not (file_names := list(file_names)): - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) - - elif len(file_names) > 1: - raise HTTPException(status_code=status.HTTP_409_CONFLICT) - - img_file = DavFile(f"img/{file_names[0]}") - img = Image.open(BytesIO(await img_file.bytes)).convert("RGB") + dav_file: DavFile = Depends(_loader), +) -> str: + img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB") img_buffer = BytesIO() img.save(img_buffer, format='JPEG', quality=85) diff --git a/api/ovdashboard_api/routers/text.py b/api/ovdashboard_api/routers/text.py index 0bde893..ad5f45d 100644 --- a/api/ovdashboard_api/routers/text.py +++ b/api/ovdashboard_api/routers/text.py @@ -1,12 +1,12 @@ import re from typing import Iterator -from fastapi import APIRouter, Depends, HTTPException, status +from fastapi import APIRouter, Depends from markdown import markdown from ..config import SETTINGS from ..dav_file import DavFile -from ._common import FileNameLister, FilePrefixFinder +from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader router = APIRouter(prefix="/text", tags=["text"]) @@ -19,6 +19,7 @@ _lister = FileNameLister( ) _finder = FilePrefixFinder(_lister) +_loader = FilePrefixLoader(_finder) async def get_ticker_lines() -> Iterator[str]: @@ -67,32 +68,12 @@ async def find_texts( @router.get( "/get/{prefix}", response_model=str, - responses={ - status.HTTP_200_OK: { - "description": "Operation successful", - }, - status.HTTP_404_NOT_FOUND: { - "description": "text file not found", - "content": None, - }, - status.HTTP_409_CONFLICT: { - "description": "ambiguous text file name", - "content": None, - }, - }, + responses=_loader.responses, ) async def get_text( - file_names: Iterator[str] = Depends(_finder), + dav_file: DavFile = Depends(_loader), ) -> str: - file_names = list(file_names) - - if not (file_names := list(file_names)): - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) - - elif len(file_names) > 1: - raise HTTPException(status_code=status.HTTP_409_CONFLICT) - - text = await DavFile(f"text/{file_names[0]}").string + text = await dav_file.string return markdown( text