upload: Create function to delete avatar image.

This commit is contained in:
Vishnu Ks
2018-09-07 15:44:40 +00:00
committed by Tim Abbott
parent 1d94fc7dbb
commit 5bdadc8061
3 changed files with 75 additions and 1 deletions

View File

@@ -167,6 +167,9 @@ class ZulipUploadBackend:
target_user_profile: UserProfile) -> None:
raise NotImplementedError()
def delete_avatar_image(self, user: UserProfile) -> None:
raise NotImplementedError()
def delete_message_image(self, path_id: str) -> bool:
raise NotImplementedError()
@@ -359,6 +362,14 @@ class S3UploadBackend(ZulipUploadBackend):
self.write_avatar_images(s3_file_name, target_user_profile,
image_data, content_type)
def delete_avatar_image(self, user: UserProfile) -> None:
path_id = user_avatar_path(user)
bucket_name = settings.S3_AVATAR_BUCKET
self.delete_file_from_s3(path_id + ".original", bucket_name)
self.delete_file_from_s3(path_id + "-medium.png", bucket_name)
self.delete_file_from_s3(path_id, bucket_name)
def get_avatar_key(self, file_name: str) -> Key:
conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
bucket_name = settings.S3_AVATAR_BUCKET
@@ -531,6 +542,13 @@ class LocalUploadBackend(ZulipUploadBackend):
image_data = user_file.read()
self.write_avatar_images(file_path, image_data)
def delete_avatar_image(self, user: UserProfile) -> None:
path_id = user_avatar_path(user)
delete_local_file("avatars", path_id + ".original")
delete_local_file("avatars", path_id + ".png")
delete_local_file("avatars", path_id + "-medium.png")
def get_avatar_url(self, hash_key: str, medium: bool=False) -> str:
# ?x=x allows templates to append additional parameters with &s
medium_suffix = "-medium" if medium else ""
@@ -607,6 +625,9 @@ def upload_avatar_image(user_file: File, acting_user_profile: UserProfile,
target_user_profile: UserProfile) -> None:
upload_backend.upload_avatar_image(user_file, acting_user_profile, target_user_profile)
def delete_avatar_image(user_profile: UserProfile):
upload_backend.delete_avatar_image(user_profile)
def copy_avatar(source_profile: UserProfile, target_profile: UserProfile) -> None:
upload_backend.copy_avatar(source_profile, target_profile)