Skip to content

Commit d958b9d

Browse files
strktechknowlogick
authored andcommitted
Alwaywas return local url for users avatar (#8245)
* Always return local url for users avatar Avoids having to wait for DNS lookups when libravatar is activated fixing #6046 * Avoid double slash in avatar link * Move avatar route to the correct place
1 parent b2b9278 commit d958b9d

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

models/action_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
173173
pushCommits.Len = len(pushCommits.Commits)
174174

175175
assert.Equal(t,
176-
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
176+
"/suburl/user/avatar/user2/-1",
177177
pushCommits.AvatarLink("[email protected]"))
178178

179179
assert.Equal(t,

models/user.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"image/png"
1818
"os"
1919
"path/filepath"
20+
"strconv"
2021
"strings"
2122
"time"
2223
"unicode/utf8"
@@ -374,9 +375,20 @@ func (u *User) generateRandomAvatar(e Engine) error {
374375
return nil
375376
}
376377

377-
// SizedRelAvatarLink returns a relative link to the user's avatar. When
378-
// applicable, the link is for an avatar of the indicated size (in pixels).
378+
// SizedRelAvatarLink returns a link to the user's avatar via
379+
// the local explore page. Function returns immediately.
380+
// When applicable, the link is for an avatar of the indicated size (in pixels).
379381
func (u *User) SizedRelAvatarLink(size int) string {
382+
return strings.TrimRight(setting.AppSubURL, "/") + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
383+
}
384+
385+
// RealSizedAvatarLink returns a link to the user's avatar. When
386+
// applicable, the link is for an avatar of the indicated size (in pixels).
387+
//
388+
// This function make take time to return when federated avatars
389+
// are in use, due to a DNS lookup need
390+
//
391+
func (u *User) RealSizedAvatarLink(size int) string {
380392
if u.ID == -1 {
381393
return base.DefaultAvatarLink()
382394
}

routers/routes/routes.go

+1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ func RegisterRoutes(m *macaron.Macaron) {
404404
// r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
405405
m.Any("/activate", user.Activate, reqSignIn)
406406
m.Any("/activate_email", user.ActivateEmail)
407+
m.Get("/avatar/:username/:size", user.Avatar)
407408
m.Get("/email2user", user.Email2User)
408409
m.Get("/recover_account", user.ResetPasswd)
409410
m.Post("/recover_account", user.ResetPasswdPost)

routers/user/avatar.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package user
6+
7+
import (
8+
"strconv"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/log"
13+
)
14+
15+
// Avatar redirect browser to user avatar of requested size
16+
func Avatar(ctx *context.Context) {
17+
userName := ctx.Params(":username")
18+
size, err := strconv.Atoi(ctx.Params(":size"))
19+
if err != nil {
20+
ctx.ServerError("Invalid avatar size", err)
21+
return
22+
}
23+
24+
log.Debug("Asked avatar for user %v and size %v", userName, size)
25+
26+
user, err := models.GetUserByName(userName)
27+
if err != nil {
28+
if models.IsErrUserNotExist(err) {
29+
ctx.ServerError("Requested avatar for invalid user", err)
30+
} else {
31+
ctx.ServerError("Retrieving user by name", err)
32+
}
33+
return
34+
}
35+
36+
ctx.Redirect(user.RealSizedAvatarLink(size))
37+
}

0 commit comments

Comments
 (0)