mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 06:43:34 +01:00
Merge pull request 'fix: repo avatar generating (#6338)' (#6339) from Snoweuph/forgejo:fix/repo-avatar into forgejo
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
Some checks are pending
/ release (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6339 Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
commit
1597e46fe8
2 changed files with 37 additions and 3 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
|
@ -107,7 +106,18 @@ func RemoveRandomAvatars(ctx context.Context) error {
|
||||||
|
|
||||||
// generateAvatar generates the avatar from a template repository
|
// generateAvatar generates the avatar from a template repository
|
||||||
func generateAvatar(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
func generateAvatar(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
|
||||||
generateRepo.Avatar = strings.Replace(templateRepo.Avatar, strconv.FormatInt(templateRepo.ID, 10), strconv.FormatInt(generateRepo.ID, 10), 1)
|
file, err := storage.RepoAvatars.Open(templateRepo.CustomAvatarRelativePath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
data, err := io.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
generateRepo.Avatar = avatar.HashAvatar(generateRepo.ID, data)
|
||||||
if _, err := storage.Copy(storage.RepoAvatars, generateRepo.CustomAvatarRelativePath(), storage.RepoAvatars, templateRepo.CustomAvatarRelativePath()); err != nil {
|
if _, err := storage.Copy(storage.RepoAvatars, generateRepo.CustomAvatarRelativePath(), storage.RepoAvatars, templateRepo.CustomAvatarRelativePath()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ func TestUploadAvatar(t *testing.T) {
|
||||||
|
|
||||||
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, avatar.HashAvatar(10, buff.Bytes()), repo.Avatar)
|
assert.Equal(t, avatar.HashAvatar(repo.ID, buff.Bytes()), repo.Avatar)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUploadBigAvatar(t *testing.T) {
|
func TestUploadBigAvatar(t *testing.T) {
|
||||||
|
@ -62,3 +62,27 @@ func TestDeleteAvatar(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, "", repo.Avatar)
|
assert.Equal(t, "", repo.Avatar)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTemplateGenerateAvatar(t *testing.T) {
|
||||||
|
// Generate image
|
||||||
|
myImage := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
||||||
|
var buff bytes.Buffer
|
||||||
|
png.Encode(&buff, myImage)
|
||||||
|
|
||||||
|
require.NoError(t, unittest.PrepareTestDatabase())
|
||||||
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})
|
||||||
|
|
||||||
|
// Upload Avatar
|
||||||
|
err := UploadAvatar(db.DefaultContext, repo, buff.Bytes())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, avatar.HashAvatar(repo.ID, buff.Bytes()), repo.Avatar)
|
||||||
|
|
||||||
|
// Generate the Avatar for Another Repo
|
||||||
|
genRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 11})
|
||||||
|
err = generateAvatar(db.DefaultContext, repo, genRepo)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, avatar.HashAvatar(genRepo.ID, buff.Bytes()), genRepo.Avatar)
|
||||||
|
|
||||||
|
// Make sure The 2 Hashes are not the same
|
||||||
|
assert.NotEqual(t, repo.Avatar, genRepo.Avatar)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue