Skip to content

Commit 324cff6

Browse files
authored
Send size to /avatars if requested (#15459)
If an avatar is requested in a particular size ensure that /avatars also gets the size request Fix #15453 Signed-off-by: Andrew Thornton <[email protected]>
1 parent c29620c commit 324cff6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

models/avatar.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func LibravatarURL(email string) (*url.URL, error) {
8181
}
8282

8383
// HashedAvatarLink returns an avatar link for a provided email
84-
func HashedAvatarLink(email string) string {
84+
func HashedAvatarLink(email string, size int) string {
8585
lowerEmail := strings.ToLower(strings.TrimSpace(email))
8686
sum := fmt.Sprintf("%x", md5.Sum([]byte(lowerEmail)))
8787
_, _ = cache.GetString("Avatar:"+sum, func() (string, error) {
@@ -108,6 +108,9 @@ func HashedAvatarLink(email string) string {
108108
}
109109
return lowerEmail, nil
110110
})
111+
if size > 0 {
112+
return setting.AppSubURL + "/avatar/" + url.PathEscape(sum) + "?size=" + strconv.Itoa(size)
113+
}
111114
return setting.AppSubURL + "/avatar/" + url.PathEscape(sum)
112115
}
113116

@@ -129,7 +132,7 @@ func SizedAvatarLink(email string, size int) string {
129132
// This is the slow path that would need to call LibravatarURL() which
130133
// does DNS lookups. Avoid it by issuing a redirect so we don't block
131134
// the template render with network requests.
132-
return HashedAvatarLink(email)
135+
return HashedAvatarLink(email, size)
133136
} else if !setting.DisableGravatar {
134137
// copy GravatarSourceURL, because we will modify its Path.
135138
copyOfGravatarSourceURL := *setting.GravatarSourceURL

models/user_avatar.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,19 @@ func (u *User) RealSizedAvatarLink(size int) string {
8282
if u.Avatar == "" {
8383
return DefaultAvatarLink()
8484
}
85+
if size > 0 {
86+
return setting.AppSubURL + "/avatars/" + u.Avatar + "?size=" + strconv.Itoa(size)
87+
}
8588
return setting.AppSubURL + "/avatars/" + u.Avatar
8689
case setting.DisableGravatar, setting.OfflineMode:
8790
if u.Avatar == "" {
8891
if err := u.GenerateRandomAvatar(); err != nil {
8992
log.Error("GenerateRandomAvatar: %v", err)
9093
}
9194
}
92-
95+
if size > 0 {
96+
return setting.AppSubURL + "/avatars/" + u.Avatar + "?size=" + strconv.Itoa(size)
97+
}
9398
return setting.AppSubURL + "/avatars/" + u.Avatar
9499
}
95100
return SizedAvatarLink(u.AvatarEmail, size)

0 commit comments

Comments
 (0)