diff options
author | Sandro Santilli <strk@kbt.io> | 2019-09-26 18:21:23 +0200 |
---|---|---|
committer | techknowlogick <techknowlogick@gitea.io> | 2019-09-26 12:21:23 -0400 |
commit | d958b9db4fa0f1910b3ca82338e3d68a70efedd9 (patch) | |
tree | 9f2de2fd82eed04ac8ed5cc26771824ba4648506 /routers | |
parent | b2b927808b201eef44ef26be7d8529c315fcd170 (diff) | |
download | gitea-d958b9db4fa0f1910b3ca82338e3d68a70efedd9.tar.gz gitea-d958b9db4fa0f1910b3ca82338e3d68a70efedd9.zip |
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
Diffstat (limited to 'routers')
-rw-r--r-- | routers/routes/routes.go | 1 | ||||
-rw-r--r-- | routers/user/avatar.go | 37 |
2 files changed, 38 insertions, 0 deletions
diff --git a/routers/routes/routes.go b/routers/routes/routes.go index e18ac0923e..5262d4ad5c 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -404,6 +404,7 @@ func RegisterRoutes(m *macaron.Macaron) { // r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds) m.Any("/activate", user.Activate, reqSignIn) m.Any("/activate_email", user.ActivateEmail) + m.Get("/avatar/:username/:size", user.Avatar) m.Get("/email2user", user.Email2User) m.Get("/recover_account", user.ResetPasswd) m.Post("/recover_account", user.ResetPasswdPost) diff --git a/routers/user/avatar.go b/routers/user/avatar.go new file mode 100644 index 0000000000..20597c72ff --- /dev/null +++ b/routers/user/avatar.go @@ -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)) +} |