diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-10-06 07:25:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-06 01:25:46 +0200 |
commit | f0ba87fda88c7bb601eee17f3e3a72bea8a71521 (patch) | |
tree | cb5caf3a22fceaec8d2d421bec0cb094ecdb9c53 /models/user_avatar.go | |
parent | 48c2578bd8ce4ddd90bf926bd40388f57c90fe14 (diff) | |
download | gitea-f0ba87fda88c7bb601eee17f3e3a72bea8a71521.tar.gz gitea-f0ba87fda88c7bb601eee17f3e3a72bea8a71521.zip |
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
Diffstat (limited to 'models/user_avatar.go')
-rw-r--r-- | models/user_avatar.go | 65 |
1 files changed, 22 insertions, 43 deletions
diff --git a/models/user_avatar.go b/models/user_avatar.go index a7ca1c9151..b8296568c2 100644 --- a/models/user_avatar.go +++ b/models/user_avatar.go @@ -9,9 +9,8 @@ import ( "fmt" "image/png" "io" - "strconv" - "strings" + "code.gitea.io/gitea/models/avatars" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/avatar" "code.gitea.io/gitea/modules/log" @@ -40,7 +39,7 @@ func (u *User) generateRandomAvatar(e db.Engine) error { return fmt.Errorf("RandomImage: %v", err) } - u.Avatar = HashEmail(seed) + u.Avatar = avatars.HashEmail(seed) // 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 { @@ -60,61 +59,41 @@ func (u *User) generateRandomAvatar(e db.Engine) error { return nil } -// SizedRelAvatarLink returns a link to the user's avatar via -// the local explore page. Function returns immediately. -// When applicable, the link is for an avatar of the indicated size (in pixels). -func (u *User) SizedRelAvatarLink(size int) string { - return setting.AppSubURL + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size) -} - -// RealSizedAvatarLink returns a link to the user's avatar. When -// applicable, the link is for an avatar of the indicated size (in pixels). -// -// This function make take time to return when federated avatars -// are in use, due to a DNS lookup need -// -func (u *User) RealSizedAvatarLink(size int) string { +// AvatarLinkWithSize returns a link to the user's avatar with size. size <= 0 means default size +func (u *User) AvatarLinkWithSize(size int) string { if u.ID == -1 { - return DefaultAvatarLink() + // ghost user + return avatars.DefaultAvatarLink() } + useLocalAvatar := false + autoGenerateAvatar := false + switch { case u.UseCustomAvatar: - if u.Avatar == "" { - return DefaultAvatarLink() - } - if size > 0 { - return setting.AppSubURL + "/avatars/" + u.Avatar + "?size=" + strconv.Itoa(size) - } - return setting.AppSubURL + "/avatars/" + u.Avatar + useLocalAvatar = true case setting.DisableGravatar, setting.OfflineMode: - if u.Avatar == "" { + useLocalAvatar = true + autoGenerateAvatar = true + } + + if useLocalAvatar { + if u.Avatar == "" && autoGenerateAvatar { if err := u.GenerateRandomAvatar(); err != nil { log.Error("GenerateRandomAvatar: %v", err) } } - if size > 0 { - return setting.AppSubURL + "/avatars/" + u.Avatar + "?size=" + strconv.Itoa(size) + if u.Avatar == "" { + return avatars.DefaultAvatarLink() } - return setting.AppSubURL + "/avatars/" + u.Avatar + return avatars.GenerateUserAvatarImageLink(u.Avatar, size) } - return SizedAvatarLink(u.AvatarEmail, size) + return avatars.GenerateEmailAvatarFastLink(u.AvatarEmail, size) } -// RelAvatarLink returns a relative link to the user's avatar. The link -// may either be a sub-URL to this site, or a full URL to an external avatar -// service. -func (u *User) RelAvatarLink() string { - return u.SizedRelAvatarLink(DefaultAvatarSize) -} - -// AvatarLink returns user avatar absolute link. +// AvatarLink returns a avatar link with default size func (u *User) AvatarLink() string { - link := u.RelAvatarLink() - if link[0] == '/' && link[1] != '/' { - return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL)[1:] - } - return link + return u.AvatarLinkWithSize(0) } // UploadAvatar saves custom avatar for user. |