Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/media/media_recipe.py: 74%

30 statements  

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

1from enum import Enum 1a

2 

3from fastapi import APIRouter, HTTPException, status 1a

4from pydantic import UUID4 1a

5from starlette.responses import FileResponse 1a

6 

7from mealie.schema.recipe import Recipe 1a

8from mealie.schema.recipe.recipe_timeline_events import RecipeTimelineEventOut 1a

9 

10router = APIRouter(prefix="/recipes") 1a

11 

12 

13class ImageType(str, Enum): 1a

14 original = "original.webp" 1a

15 small = "min-original.webp" 1a

16 tiny = "tiny-original.webp" 1a

17 

18 

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) 1b

26 

27 if recipe_image.exists(): 27 ↛ 28line 27 didn't jump to line 28 because the condition on line 27 was never true1b

28 return FileResponse(recipe_image, media_type="image/webp") 

29 else: 

30 raise HTTPException(status.HTTP_404_NOT_FOUND) 1b

31 

32 

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( 1b

42 file_name.value 

43 ) 

44 

45 if timeline_event_image.exists(): 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true1b

46 return FileResponse(timeline_event_image, media_type="image/webp") 

47 else: 

48 raise HTTPException(status.HTTP_404_NOT_FOUND) 1b

49 

50 

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) 

55 

56 try: 

57 return FileResponse(file) 

58 except Exception as e: 

59 raise HTTPException(status.HTTP_404_NOT_FOUND) from e