users: Fix uploading user avatars.

Due to recent refactoring in 9fb03cb2c7, a user could not
upload avatar if the server uses local upload backend and there
was already an avatar file for that user.

This commit fixes it to just check if there exists a file only
when importing and not when the user is actually trying to
change the avatar.

Fixes #30676.
This commit is contained in:
Sahil Batra
2024-07-02 17:38:44 +05:30
committed by Tim Abbott
parent 58b40fb6c7
commit 5ef14c3a8e
2 changed files with 13 additions and 7 deletions

View File

@@ -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: def ensure_avatar_image(user_profile: UserProfile, medium: bool = False) -> None:
file_path = user_avatar_path(user_profile) 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) image_data, _ = upload_backend.get_avatar_contents(file_path)
if medium: if medium:
@@ -267,7 +278,7 @@ def ensure_avatar_image(user_profile: UserProfile, medium: bool = False) -> None
else: else:
resized_avatar = resize_avatar(image_data) resized_avatar = resize_avatar(image_data)
upload_backend.upload_single_avatar_image( upload_backend.upload_single_avatar_image(
upload_backend.get_avatar_path(file_path, medium), final_file_path,
user_profile=user_profile, user_profile=user_profile,
image_data=resized_avatar, image_data=resized_avatar,
content_type="image/png", content_type="image/png",

View File

@@ -126,12 +126,7 @@ class LocalUploadBackend(ZulipUploadBackend):
image_data: bytes, image_data: bytes,
content_type: Optional[str], content_type: Optional[str],
) -> None: ) -> None:
output_path = os.path.join( write_local_file("avatars", file_path, image_data)
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)
@override @override
def delete_avatar_image(self, path_id: str) -> None: def delete_avatar_image(self, path_id: str) -> None: