File tree 4 files changed +53
-3
lines changed
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) {
173
173
pushCommits .Len = len (pushCommits .Commits )
174
174
175
175
assert .Equal (t ,
176
- "https://secure.gravatar.com /avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon " ,
176
+ "/suburl/user /avatar/user2/-1 " ,
177
177
pushCommits .
AvatarLink (
"[email protected] " ))
178
178
179
179
assert .Equal (t ,
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import (
17
17
"image/png"
18
18
"os"
19
19
"path/filepath"
20
+ "strconv"
20
21
"strings"
21
22
"time"
22
23
"unicode/utf8"
@@ -374,9 +375,20 @@ func (u *User) generateRandomAvatar(e Engine) error {
374
375
return nil
375
376
}
376
377
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).
379
381
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 {
380
392
if u .ID == - 1 {
381
393
return base .DefaultAvatarLink ()
382
394
}
Original file line number Diff line number Diff line change @@ -404,6 +404,7 @@ func RegisterRoutes(m *macaron.Macaron) {
404
404
// r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
405
405
m .Any ("/activate" , user .Activate , reqSignIn )
406
406
m .Any ("/activate_email" , user .ActivateEmail )
407
+ m .Get ("/avatar/:username/:size" , user .Avatar )
407
408
m .Get ("/email2user" , user .Email2User )
408
409
m .Get ("/recover_account" , user .ResetPasswd )
409
410
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