2022-09-18 21:59:02 +00:00
|
|
|
"""
|
|
|
|
Router "file" provides:
|
|
|
|
|
|
|
|
- listing files
|
|
|
|
- finding files by name prefix
|
|
|
|
- getting files by name prefix
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
from io import BytesIO
|
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from magic import Magic
|
|
|
|
|
2023-10-20 11:12:44 +00:00
|
|
|
from ...core.dav_common import webdav_ensure_files, webdav_ensure_path
|
2023-10-20 08:43:15 +00:00
|
|
|
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-09-18 21:59:02 +00:00
|
|
|
|
|
|
|
_logger = getLogger(__name__)
|
|
|
|
_magic = Magic(mime=True)
|
2023-10-23 21:32:25 +00:00
|
|
|
_PATH_NAME = "file_dir"
|
2022-09-18 21:59:02 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/file", tags=["file"])
|
|
|
|
|
2023-10-23 21:32:25 +00:00
|
|
|
_ls = list_files(
|
|
|
|
path_name=_PATH_NAME,
|
2022-09-18 21:59:02 +00:00
|
|
|
re=re.compile(
|
|
|
|
r"[^/]$",
|
|
|
|
flags=re.IGNORECASE,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
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-09-18 21:59:02 +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):
|
2023-10-20 11:12:44 +00:00
|
|
|
webdav_ensure_files(
|
2023-10-23 21:32:25 +00:00
|
|
|
remote_path,
|
2023-10-20 11:12:44 +00:00
|
|
|
"logo.svg",
|
|
|
|
"thw.svg",
|
|
|
|
)
|
2022-09-18 21:59:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
"/list",
|
|
|
|
response_model=list[str],
|
2023-10-23 21:32:25 +00:00
|
|
|
responses=_ls.responses,
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def list_all_files(
|
|
|
|
names: list[str] = Depends(_ls.func),
|
2022-09-18 21:59:02 +00:00
|
|
|
) -> list[str]:
|
2023-10-23 21:32:25 +00:00
|
|
|
return names
|
2022-09-18 21:59:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
"/find/{prefix}",
|
|
|
|
response_model=list[str],
|
2023-10-23 21:32:25 +00:00
|
|
|
responses=_fp.responses,
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def find_files_by_prefix(
|
|
|
|
names: list[str] = Depends(_fp.func),
|
2022-09-18 21:59:02 +00:00
|
|
|
) -> list[str]:
|
2023-10-23 21:32:25 +00:00
|
|
|
return names
|
2022-09-18 21:59:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
"/get/{prefix}",
|
|
|
|
response_class=StreamingResponse,
|
2023-10-23 21:32:25 +00:00
|
|
|
responses=_fpu.responses,
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def get_file_by_prefix(
|
2022-09-18 21:59:02 +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-18 21:59:02 +00:00
|
|
|
) -> StreamingResponse:
|
2023-10-23 21:32:25 +00:00
|
|
|
buffer = BytesIO(await WebDAV.read_bytes(f"{remote_path}/{name}"))
|
2022-09-18 21:59:02 +00:00
|
|
|
|
|
|
|
mime = _magic.from_buffer(buffer.read(2048))
|
|
|
|
buffer.seek(0)
|
|
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
content=buffer,
|
|
|
|
media_type=mime,
|
2023-10-17 12:55:38 +00:00
|
|
|
headers={"Content-Disposition": f"filename={prefix}"},
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|