Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/media/media_recipe.py: 88%
30 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1from enum import Enum
3from fastapi import APIRouter, HTTPException, status
4from pydantic import UUID4
5from starlette.responses import FileResponse
7from mealie.schema.recipe import Recipe
8from mealie.schema.recipe.recipe_timeline_events import RecipeTimelineEventOut
10router = APIRouter(prefix="/recipes")
13class ImageType(str, Enum):
14 original = "original.webp"
15 small = "min-original.webp"
16 tiny = "tiny-original.webp"
19@router.get("/{recipe_id}/images/{file_name}")
20async def get_recipe_img(recipe_id: str, file_name: ImageType = ImageType.original):
21 """
22 Takes in a recipe id, returns the static image. This route is proxied in the docker image
23 and should not hit the API in production
24 """
25 recipe_image = Recipe.directory_from_id(recipe_id).joinpath("images", file_name.value)
27 if recipe_image.exists():
28 return FileResponse(recipe_image, media_type="image/webp")
29 else:
30 raise HTTPException(status.HTTP_404_NOT_FOUND)
33@router.get("/{recipe_id}/images/timeline/{timeline_event_id}/{file_name}")
34async def get_recipe_timeline_event_img(
35 recipe_id: str, timeline_event_id: str, file_name: ImageType = ImageType.original
36):
37 """
38 Takes in a recipe id and event timeline id, returns the static image. This route is proxied in the docker image
39 and should not hit the API in production
40 """
41 timeline_event_image = RecipeTimelineEventOut.image_dir_from_id(recipe_id, timeline_event_id).joinpath(
42 file_name.value
43 )
45 if timeline_event_image.exists(): 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true
46 return FileResponse(timeline_event_image, media_type="image/webp")
47 else:
48 raise HTTPException(status.HTTP_404_NOT_FOUND)
51@router.get("/{recipe_id}/assets/{file_name}")
52async def get_recipe_asset(recipe_id: UUID4, file_name: str):
53 """Returns a recipe asset"""
54 file = Recipe.directory_from_id(recipe_id).joinpath("assets", file_name)
56 try:
57 return FileResponse(file)
58 except Exception as e:
59 raise HTTPException(status.HTTP_404_NOT_FOUND) from e