2021-04-10 21:42:04 -08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2021-06-09 13:04:54 -08:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
2021-04-10 21:42:04 -08:00
|
|
|
from starlette.responses import FileResponse
|
|
|
|
|
|
2023-12-19 21:34:34 -06:00
|
|
|
from mealie.core.config import get_app_dirs
|
2021-08-28 15:36:46 -08:00
|
|
|
from mealie.core.dependencies import validate_file_token
|
2021-08-28 14:27:56 -08:00
|
|
|
|
2021-04-10 21:42:04 -08:00
|
|
|
router = APIRouter(prefix="/api/utils", tags=["Utils"], include_in_schema=True)
|
|
|
|
|
|
|
|
|
|
|
2021-06-09 13:04:54 -08:00
|
|
|
@router.get("/download")
|
2022-10-23 13:04:04 -08:00
|
|
|
async def download_file(file_path: Path = Depends(validate_file_token)):
|
2021-04-29 21:46:27 -08:00
|
|
|
"""Uses a file token obtained by an active user to retrieve a file from the operating
|
|
|
|
|
system."""
|
2023-12-19 21:34:34 -06:00
|
|
|
|
|
|
|
|
file_path = Path(file_path).resolve()
|
|
|
|
|
|
|
|
|
|
dirs = get_app_dirs()
|
|
|
|
|
|
|
|
|
|
if not file_path.is_relative_to(dirs.DATA_DIR):
|
|
|
|
|
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
2021-04-29 18:22:45 +02:00
|
|
|
if not file_path.is_file():
|
2021-04-29 21:46:27 -08:00
|
|
|
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
2021-04-29 18:22:45 +02:00
|
|
|
|
|
|
|
|
return FileResponse(file_path, media_type="application/octet-stream", filename=file_path.name)
|