mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-22 12:54:53 -05:00
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
This commit is contained in:
parent
b2b927808b
commit
d958b9db4f
4 changed files with 53 additions and 3 deletions
|
@ -173,7 +173,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
|
||||||
pushCommits.Len = len(pushCommits.Commits)
|
pushCommits.Len = len(pushCommits.Commits)
|
||||||
|
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
|
"/suburl/user/avatar/user2/-1",
|
||||||
pushCommits.AvatarLink("user2@example.com"))
|
pushCommits.AvatarLink("user2@example.com"))
|
||||||
|
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"image/png"
|
"image/png"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
@ -374,9 +375,20 @@ func (u *User) generateRandomAvatar(e Engine) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SizedRelAvatarLink returns a relative link to the user's avatar. When
|
// SizedRelAvatarLink returns a link to the user's avatar via
|
||||||
// applicable, the link is for an avatar of the indicated size (in pixels).
|
// the local explore page. Function returns immediately.
|
||||||
|
// When applicable, the link is for an avatar of the indicated size (in pixels).
|
||||||
func (u *User) SizedRelAvatarLink(size int) string {
|
func (u *User) SizedRelAvatarLink(size int) string {
|
||||||
|
return strings.TrimRight(setting.AppSubURL, "/") + "/user/avatar/" + u.Name + "/" + strconv.Itoa(size)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RealSizedAvatarLink returns a link to the user's avatar. When
|
||||||
|
// applicable, the link is for an avatar of the indicated size (in pixels).
|
||||||
|
//
|
||||||
|
// This function make take time to return when federated avatars
|
||||||
|
// are in use, due to a DNS lookup need
|
||||||
|
//
|
||||||
|
func (u *User) RealSizedAvatarLink(size int) string {
|
||||||
if u.ID == -1 {
|
if u.ID == -1 {
|
||||||
return base.DefaultAvatarLink()
|
return base.DefaultAvatarLink()
|
||||||
}
|
}
|
||||||
|
|
|
@ -404,6 +404,7 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||||
// r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
|
// r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
|
||||||
m.Any("/activate", user.Activate, reqSignIn)
|
m.Any("/activate", user.Activate, reqSignIn)
|
||||||
m.Any("/activate_email", user.ActivateEmail)
|
m.Any("/activate_email", user.ActivateEmail)
|
||||||
|
m.Get("/avatar/:username/:size", user.Avatar)
|
||||||
m.Get("/email2user", user.Email2User)
|
m.Get("/email2user", user.Email2User)
|
||||||
m.Get("/recover_account", user.ResetPasswd)
|
m.Get("/recover_account", user.ResetPasswd)
|
||||||
m.Post("/recover_account", user.ResetPasswdPost)
|
m.Post("/recover_account", user.ResetPasswdPost)
|
||||||
|
|
37
routers/user/avatar.go
Normal file
37
routers/user/avatar.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a MIT-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
|
"code.gitea.io/gitea/modules/context"
|
||||||
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Avatar redirect browser to user avatar of requested size
|
||||||
|
func Avatar(ctx *context.Context) {
|
||||||
|
userName := ctx.Params(":username")
|
||||||
|
size, err := strconv.Atoi(ctx.Params(":size"))
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError("Invalid avatar size", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug("Asked avatar for user %v and size %v", userName, size)
|
||||||
|
|
||||||
|
user, err := models.GetUserByName(userName)
|
||||||
|
if err != nil {
|
||||||
|
if models.IsErrUserNotExist(err) {
|
||||||
|
ctx.ServerError("Requested avatar for invalid user", err)
|
||||||
|
} else {
|
||||||
|
ctx.ServerError("Retrieving user by name", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.Redirect(user.RealSizedAvatarLink(size))
|
||||||
|
}
|
Loading…
Reference in a new issue