From 96797fed311151ff889f87c94c7b6aaa16c5d535 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 2 Jan 2023 22:46:39 +0100 Subject: Unify hashing for avatar (#22289) - Unify the hashing code for repository and user avatars into a function. - Use a sane hash function instead of MD5. - Only require hashing once instead of twice(w.r.t. hashing for user avatar). - Improve the comment for the hashing code of why it works. Co-authored-by: Lunny Xiao Co-authored-by: Yarden Shoham --- services/user/user.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'services/user/user.go') diff --git a/services/user/user.go b/services/user/user.go index 65db732bf9..c95eb67a85 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -5,7 +5,6 @@ package user import ( "context" - "crypto/md5" "fmt" "image/png" "io" @@ -241,11 +240,7 @@ func UploadAvatar(u *user_model.User, data []byte) error { defer committer.Close() u.UseCustomAvatar = true - // Different users can upload same image as avatar - // If we prefix it with u.ID, it will be separated - // Otherwise, if any of the users delete his avatar - // Other users will lose their avatars too. - u.Avatar = fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data))))) + u.Avatar = avatar.HashAvatar(u.ID, data) if err = user_model.UpdateUserCols(ctx, u, "use_custom_avatar", "avatar"); err != nil { return fmt.Errorf("updateUser: %w", err) } -- cgit v1.2.3