2022-09-18 21:59:02 +00:00
|
|
|
"""
|
|
|
|
Router "file" provides:
|
|
|
|
|
|
|
|
- listing files
|
|
|
|
- finding files by name prefix
|
|
|
|
- getting files by name prefix
|
|
|
|
"""
|
|
|
|
|
2023-10-26 16:21:07 +00:00
|
|
|
import logging
|
2022-09-18 21:59:02 +00:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
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-26 14:31:12 +00:00
|
|
|
from ._common import LM_FILE, RP_FILE
|
2022-09-18 21:59:02 +00:00
|
|
|
|
2023-10-26 16:21:07 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2022-09-18 21:59:02 +00:00
|
|
|
_magic = Magic(mime=True)
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/file", tags=["file"])
|
|
|
|
|
|
|
|
|
|
|
|
@router.on_event("startup")
|
|
|
|
async def start_router() -> None:
|
|
|
|
_logger.debug(f"{router.prefix} router starting.")
|
|
|
|
|
2023-10-26 14:31:12 +00:00
|
|
|
remote_path = await RP_FILE()
|
2023-10-23 21:32:25 +00:00
|
|
|
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",
|
2023-10-26 14:31:12 +00:00
|
|
|
responses=LM_FILE.lister.responses,
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def list_all_files(
|
2023-10-26 14:31:12 +00:00
|
|
|
names: list[str] = Depends(LM_FILE.lister.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}",
|
2023-10-26 14:31:12 +00:00
|
|
|
responses=LM_FILE.filter.responses,
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def find_files_by_prefix(
|
2023-10-26 14:31:12 +00:00
|
|
|
names: list[str] = Depends(LM_FILE.filter.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}",
|
2023-10-26 14:31:12 +00:00
|
|
|
responses=LM_FILE.getter.responses,
|
2023-10-24 17:39:43 +00:00
|
|
|
response_class=StreamingResponse,
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|
2023-10-23 21:32:25 +00:00
|
|
|
async def get_file_by_prefix(
|
2023-10-26 14:31:12 +00:00
|
|
|
remote_path: str = Depends(RP_FILE),
|
|
|
|
name: str = Depends(LM_FILE.getter.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-26 13:58:02 +00:00
|
|
|
headers={"Content-Disposition": f"filename={name}"},
|
2022-09-18 21:59:02 +00:00
|
|
|
)
|