Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/utility_routes.py: 42%

15 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 15:32 +0000

1from pathlib import Path 1a

2 

3from fastapi import APIRouter, Depends, HTTPException, status 1a

4from starlette.responses import FileResponse 1a

5 

6from mealie.core.config import get_app_dirs 1a

7from mealie.core.dependencies import validate_file_token 1a

8 

9router = APIRouter(prefix="/api/utils", tags=["Utils"], include_in_schema=True) 1a

10 

11 

12@router.get("/download") 1a

13async def download_file(file_path: Path = Depends(validate_file_token)): 1a

14 """Uses a file token obtained by an active user to retrieve a file from the operating 

15 system.""" 

16 

17 file_path = Path(file_path).resolve() 

18 

19 dirs = get_app_dirs() 

20 

21 if not file_path.is_relative_to(dirs.DATA_DIR): 

22 raise HTTPException(status.HTTP_400_BAD_REQUEST) 

23 

24 if not file_path.is_file(): 

25 raise HTTPException(status.HTTP_400_BAD_REQUEST) 

26 

27 return FileResponse(file_path, media_type="application/octet-stream", filename=file_path.name)