aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository
diff options
context:
space:
mode:
authorGusted <postmaster@gusted.xyz>2023-01-02 22:46:39 +0100
committerGitHub <noreply@github.com>2023-01-02 22:46:39 +0100
commit96797fed311151ff889f87c94c7b6aaa16c5d535 (patch)
tree30892e553fa197e8205b9d1ddad82aef3e707caa /services/repository
parentfcd6ceef2b76aabd6a8ffab688492cc7f69269bf (diff)
downloadgitea-96797fed311151ff889f87c94c7b6aaa16c5d535.tar.gz
gitea-96797fed311151ff889f87c94c7b6aaa16c5d535.zip
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 <xiaolunwen@gmail.com> Co-authored-by: Yarden Shoham <hrsi88@gmail.com>
Diffstat (limited to 'services/repository')
-rw-r--r--services/repository/avatar.go3
-rw-r--r--services/repository/avatar_test.go5
2 files changed, 3 insertions, 5 deletions
diff --git a/services/repository/avatar.go b/services/repository/avatar.go
index a829a1000a..5fe8bd2c72 100644
--- a/services/repository/avatar.go
+++ b/services/repository/avatar.go
@@ -5,7 +5,6 @@ package repository
import (
"context"
- "crypto/md5"
"fmt"
"image/png"
"io"
@@ -27,7 +26,7 @@ func UploadAvatar(repo *repo_model.Repository, data []byte) error {
return err
}
- newAvatar := fmt.Sprintf("%d-%x", repo.ID, md5.Sum(data))
+ newAvatar := avatar.HashAvatar(repo.ID, data)
if repo.Avatar == newAvatar { // upload the same picture
return nil
}
diff --git a/services/repository/avatar_test.go b/services/repository/avatar_test.go
index 3875302696..5ec899ec3f 100644
--- a/services/repository/avatar_test.go
+++ b/services/repository/avatar_test.go
@@ -5,14 +5,13 @@ package repository
import (
"bytes"
- "crypto/md5"
- "fmt"
"image"
"image/png"
"testing"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
+ "code.gitea.io/gitea/modules/avatar"
"github.com/stretchr/testify/assert"
)
@@ -28,7 +27,7 @@ func TestUploadAvatar(t *testing.T) {
err := UploadAvatar(repo, buff.Bytes())
assert.NoError(t, err)
- assert.Equal(t, fmt.Sprintf("%d-%x", 10, md5.Sum(buff.Bytes())), repo.Avatar)
+ assert.Equal(t, avatar.HashAvatar(10, buff.Bytes()), repo.Avatar)
}
func TestUploadBigAvatar(t *testing.T) {