Add FilePrefixLoader
This commit is contained in:
parent
749f56268c
commit
b28d6fa4e7
3 changed files with 49 additions and 52 deletions
|
@ -2,9 +2,11 @@ import re
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
|
from fastapi import HTTPException, status
|
||||||
from webdav3.exceptions import RemoteResourceNotFound
|
from webdav3.exceptions import RemoteResourceNotFound
|
||||||
|
|
||||||
from .. import CLIENT
|
from .. import CLIENT
|
||||||
|
from ..dav_file import DavFile
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
|
@ -36,3 +38,37 @@ class FilePrefixFinder:
|
||||||
for file_name in self.lister()
|
for file_name in self.lister()
|
||||||
if file_name.lower().startswith(prefix.lower())
|
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]}")
|
||||||
|
|
|
@ -2,12 +2,12 @@ import re
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from ..dav_file import DavFile
|
from ..dav_file import DavFile
|
||||||
from ._common import FileNameLister, FilePrefixFinder
|
from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader
|
||||||
|
|
||||||
router = APIRouter(prefix="/image", tags=["image"])
|
router = APIRouter(prefix="/image", tags=["image"])
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ _lister = FileNameLister(
|
||||||
)
|
)
|
||||||
|
|
||||||
_finder = FilePrefixFinder(_lister)
|
_finder = FilePrefixFinder(_lister)
|
||||||
|
_loader = FilePrefixLoader(_finder)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/list", response_model=list[str])
|
@router.get("/list", response_model=list[str])
|
||||||
|
@ -39,34 +40,13 @@ async def find_images(
|
||||||
@router.get(
|
@router.get(
|
||||||
"/get/{prefix}",
|
"/get/{prefix}",
|
||||||
response_class=StreamingResponse,
|
response_class=StreamingResponse,
|
||||||
responses={
|
responses=_loader.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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
async def get_image(
|
async def get_image(
|
||||||
prefix: str,
|
prefix: str,
|
||||||
file_names: Iterator[str] = Depends(_finder),
|
dav_file: DavFile = Depends(_loader),
|
||||||
) -> StreamingResponse:
|
) -> str:
|
||||||
file_names = list(file_names)
|
img = Image.open(BytesIO(await dav_file.bytes)).convert("RGB")
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
img_buffer = BytesIO()
|
img_buffer = BytesIO()
|
||||||
img.save(img_buffer, format='JPEG', quality=85)
|
img.save(img_buffer, format='JPEG', quality=85)
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import re
|
import re
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
|
|
||||||
from ..config import SETTINGS
|
from ..config import SETTINGS
|
||||||
from ..dav_file import DavFile
|
from ..dav_file import DavFile
|
||||||
from ._common import FileNameLister, FilePrefixFinder
|
from ._common import FileNameLister, FilePrefixFinder, FilePrefixLoader
|
||||||
|
|
||||||
router = APIRouter(prefix="/text", tags=["text"])
|
router = APIRouter(prefix="/text", tags=["text"])
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ _lister = FileNameLister(
|
||||||
)
|
)
|
||||||
|
|
||||||
_finder = FilePrefixFinder(_lister)
|
_finder = FilePrefixFinder(_lister)
|
||||||
|
_loader = FilePrefixLoader(_finder)
|
||||||
|
|
||||||
|
|
||||||
async def get_ticker_lines() -> Iterator[str]:
|
async def get_ticker_lines() -> Iterator[str]:
|
||||||
|
@ -67,32 +68,12 @@ async def find_texts(
|
||||||
@router.get(
|
@router.get(
|
||||||
"/get/{prefix}",
|
"/get/{prefix}",
|
||||||
response_model=str,
|
response_model=str,
|
||||||
responses={
|
responses=_loader.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,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
async def get_text(
|
async def get_text(
|
||||||
file_names: Iterator[str] = Depends(_finder),
|
dav_file: DavFile = Depends(_loader),
|
||||||
) -> str:
|
) -> str:
|
||||||
file_names = list(file_names)
|
text = await dav_file.string
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
return markdown(
|
return markdown(
|
||||||
text
|
text
|
||||||
|
|
Loading…
Reference in a new issue