mirror of
https://gitea.com/gitea/gitea-mirror.git
synced 2026-03-20 11:50:27 +00:00
Refactor avatar package, support default avatar fallback (#36788)
* Fix #34715
This commit is contained in:
@@ -30,22 +30,16 @@ func GenerateRandomAvatar(ctx context.Context, u *User) error {
|
||||
seed = u.Name
|
||||
}
|
||||
|
||||
img, err := avatar.RandomImage([]byte(seed))
|
||||
if err != nil {
|
||||
return fmt.Errorf("RandomImage: %w", err)
|
||||
}
|
||||
img := avatar.RandomImageDefaultSize([]byte(seed))
|
||||
|
||||
u.Avatar = avatars.HashEmail(seed)
|
||||
|
||||
_, err = storage.Avatars.Stat(u.CustomAvatarRelativePath())
|
||||
_, err := storage.Avatars.Stat(u.CustomAvatarRelativePath())
|
||||
if err != nil {
|
||||
// If unable to Stat the avatar file (usually it means non-existing), then try to save a new one
|
||||
// Don't share the images so that we can delete them easily
|
||||
if err := storage.SaveFrom(storage.Avatars, u.CustomAvatarRelativePath(), func(w io.Writer) error {
|
||||
if err := png.Encode(w, img); err != nil {
|
||||
log.Error("Encode: %v", err)
|
||||
}
|
||||
return nil
|
||||
return png.Encode(w, img)
|
||||
}); err != nil {
|
||||
return fmt.Errorf("failed to save avatar %s: %w", u.CustomAvatarRelativePath(), err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user