Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/users/images.py: 93%

26 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 03:04 +0000

1import shutil 

2from uuid import uuid4 

3 

4from fastapi import File, HTTPException, UploadFile, status 

5from pydantic import UUID4 

6 

7from mealie.core.dependencies import get_temporary_path 

8from mealie.pkgs import cache, img 

9from mealie.routes._base import BaseUserController, controller 

10from mealie.routes._base.routers import UserAPIRouter 

11from mealie.routes.users._helpers import assert_user_change_allowed 

12from mealie.schema.user import PrivateUser 

13 

14router = UserAPIRouter(prefix="", tags=["Users: Images"]) 

15 

16 

17@controller(router) 

18class UserImageController(BaseUserController): 

19 @router.post("/{id}/image") 

20 def update_user_image( 

21 self, 

22 id: UUID4, 

23 profile: UploadFile = File(...), 

24 ): 

25 """Updates a User Image""" 

26 with get_temporary_path() as temp_path: 

27 assert_user_change_allowed(id, self.user, self.user) 

28 

29 # use a generated uuid and ignore the filename so we don't 

30 # need to worry about sanitizing user inputs. 

31 temp_img = temp_path.joinpath(str(uuid4())) 

32 

33 with temp_img.open("wb") as buffer: 

34 shutil.copyfileobj(profile.file, buffer) 

35 

36 image = img.PillowMinifier.to_webp(temp_img) 

37 dest = PrivateUser.get_directory(id) / "profile.webp" 

38 

39 shutil.copyfile(image, dest) 

40 

41 self.repos.users.patch(id, {"cache_key": cache.new_key()}) 

42 

43 if not dest.is_file(): 43 ↛ 44line 43 didn't jump to line 44 because the condition on line 43 was never true

44 raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR)