diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.py index 35e3d7a117..119ce917d7 100644 --- a/zerver/lib/upload/__init__.py +++ b/zerver/lib/upload/__init__.py @@ -260,6 +260,17 @@ def copy_avatar(source_profile: UserProfile, target_profile: UserProfile) -> Non def ensure_avatar_image(user_profile: UserProfile, medium: bool = False) -> None: file_path = user_avatar_path(user_profile) + final_file_path = upload_backend.get_avatar_path(file_path, medium) + + if settings.LOCAL_AVATARS_DIR is not None: + output_path = os.path.join( + settings.LOCAL_AVATARS_DIR, + final_file_path, + ) + + if os.path.isfile(output_path): + return + image_data, _ = upload_backend.get_avatar_contents(file_path) if medium: @@ -267,7 +278,7 @@ def ensure_avatar_image(user_profile: UserProfile, medium: bool = False) -> None else: resized_avatar = resize_avatar(image_data) upload_backend.upload_single_avatar_image( - upload_backend.get_avatar_path(file_path, medium), + final_file_path, user_profile=user_profile, image_data=resized_avatar, content_type="image/png", diff --git a/zerver/lib/upload/local.py b/zerver/lib/upload/local.py index 10c3ed2ed1..44b4781fb1 100644 --- a/zerver/lib/upload/local.py +++ b/zerver/lib/upload/local.py @@ -126,12 +126,7 @@ class LocalUploadBackend(ZulipUploadBackend): image_data: bytes, content_type: Optional[str], ) -> None: - output_path = os.path.join( - assert_is_not_none(settings.LOCAL_AVATARS_DIR), - file_path, - ) - if not os.path.isfile(output_path): - write_local_file("avatars", file_path, image_data) + write_local_file("avatars", file_path, image_data) @override def delete_avatar_image(self, path_id: str) -> None: