Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/media/media_recipe.py: 50%
30 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 15:32 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 15:32 +0000
1from enum import Enum 1a
3from fastapi import APIRouter, HTTPException, status 1a
4from pydantic import UUID4 1a
5from starlette.responses import FileResponse 1a
7from mealie.schema.recipe import Recipe 1a
8from mealie.schema.recipe.recipe_timeline_events import RecipeTimelineEventOut 1a
10router = APIRouter(prefix="/recipes") 1a
13class ImageType(str, Enum): 1a
14 original = "original.webp" 1a
15 small = "min-original.webp" 1a
16 tiny = "tiny-original.webp" 1a
19@router.get("/{recipe_id}/images/{file_name}") 1a
20async def get_recipe_img(recipe_id: str, file_name: ImageType = ImageType.original): 1a
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}") 1a
34async def get_recipe_timeline_event_img( 1a
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():
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}") 1a
52async def get_recipe_asset(recipe_id: UUID4, file_name: str): 1a
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