File tree Expand file tree Collapse file tree 4 files changed +53
-3
lines changed Expand file tree Collapse file tree 4 files changed +53
-3
lines changed Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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).
379381func (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 }
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments