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+ "/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"
@@ -373,9 +374,20 @@ func (u *User) generateRandomAvatar(e Engine) error {
373374 return nil
374375}
375376
376- // SizedRelAvatarLink returns a relative link to the user's avatar. When
377- // applicable, the link is for an avatar of the indicated size (in pixels).
377+ // SizedRelAvatarLink returns a link to the user's avatar via
378+ // the local explore page. Function returns immediately.
379+ // When applicable, the link is for an avatar of the indicated size (in pixels).
378380func (u * User ) SizedRelAvatarLink (size int ) string {
381+ return setting .AppSubURL + "/user/avatar/" + u .Name + "/" + strconv .Itoa (size )
382+ }
383+
384+ // RealSizedAvatarLink returns a link to the user's avatar. When
385+ // applicable, the link is for an avatar of the indicated size (in pixels).
386+ //
387+ // This function make take time to return when federated avatars
388+ // are in use, due to a DNS lookup need
389+ //
390+ func (u * User ) RealSizedAvatarLink (size int ) string {
379391 if u .ID == - 1 {
380392 return base .DefaultAvatarLink ()
381393 }
Original file line number Diff line number Diff line change @@ -278,6 +278,7 @@ func RegisterRoutes(m *macaron.Macaron) {
278278
279279 // ***** START: User *****
280280 m .Group ("/user" , func () {
281+ m .Get ("/avatar/:username/:size" , user .Avatar )
281282 m .Get ("/login" , user .SignIn )
282283 m .Post ("/login" , bindIgnErr (auth.SignInForm {}), user .SignInPost )
283284 m .Group ("" , func () {
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