2019-09-26 11:21:23 -05:00
|
|
|
// 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 (
|
2020-03-27 07:34:39 -05:00
|
|
|
"errors"
|
2020-12-03 13:46:11 -05:00
|
|
|
"net/url"
|
2021-04-12 19:57:12 -05:00
|
|
|
"path"
|
2019-09-26 11:21:23 -05:00
|
|
|
"strconv"
|
2019-12-29 09:24:50 -05:00
|
|
|
"strings"
|
2019-09-26 11:21:23 -05:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-09-06 16:05:44 -05:00
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
2019-09-26 11:21:23 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-04-12 19:57:12 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-09-26 11:21:23 -05:00
|
|
|
)
|
|
|
|
|
2021-09-06 16:05:44 -05:00
|
|
|
func cacheableRedirect(ctx *context.Context, location string) {
|
|
|
|
ctx.Resp.Header().Set("Cache-Control", httpcache.GetCacheControl())
|
|
|
|
ctx.Redirect(location)
|
|
|
|
}
|
|
|
|
|
2019-09-26 11:21:23 -05:00
|
|
|
// 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)
|
|
|
|
|
2019-12-29 09:24:50 -05:00
|
|
|
var user *models.User
|
|
|
|
if strings.ToLower(userName) != "ghost" {
|
|
|
|
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
|
2019-09-26 11:21:23 -05:00
|
|
|
}
|
2019-12-29 09:24:50 -05:00
|
|
|
} else {
|
|
|
|
user = models.NewGhostUser()
|
2019-09-26 11:21:23 -05:00
|
|
|
}
|
|
|
|
|
2021-09-06 16:05:44 -05:00
|
|
|
cacheableRedirect(ctx, user.RealSizedAvatarLink(size))
|
2019-09-26 11:21:23 -05:00
|
|
|
}
|
2020-03-27 07:34:39 -05:00
|
|
|
|
|
|
|
// AvatarByEmailHash redirects the browser to the appropriate Avatar link
|
|
|
|
func AvatarByEmailHash(ctx *context.Context) {
|
2020-12-03 13:46:11 -05:00
|
|
|
var err error
|
|
|
|
|
2020-03-27 07:34:39 -05:00
|
|
|
hash := ctx.Params(":hash")
|
|
|
|
if len(hash) == 0 {
|
|
|
|
ctx.ServerError("invalid avatar hash", errors.New("hash cannot be empty"))
|
|
|
|
return
|
|
|
|
}
|
2020-12-03 13:46:11 -05:00
|
|
|
|
|
|
|
var email string
|
|
|
|
email, err = models.GetEmailForHash(hash)
|
2020-03-27 07:34:39 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("invalid avatar hash", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(email) == 0 {
|
2021-09-06 16:05:44 -05:00
|
|
|
cacheableRedirect(ctx, models.DefaultAvatarLink())
|
2020-03-27 07:34:39 -05:00
|
|
|
return
|
|
|
|
}
|
2021-07-28 20:42:15 -05:00
|
|
|
size := ctx.FormInt("size")
|
2020-03-27 07:34:39 -05:00
|
|
|
if size == 0 {
|
2020-12-03 13:46:11 -05:00
|
|
|
size = models.DefaultAvatarSize
|
2020-03-27 07:34:39 -05:00
|
|
|
}
|
2020-12-03 13:46:11 -05:00
|
|
|
|
|
|
|
var avatarURL *url.URL
|
2021-04-12 19:57:12 -05:00
|
|
|
|
|
|
|
if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
|
|
|
|
avatarURL, err = models.LibravatarURL(email)
|
|
|
|
if err != nil {
|
|
|
|
avatarURL, err = url.Parse(models.DefaultAvatarLink())
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("invalid default avatar url", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !setting.DisableGravatar {
|
|
|
|
copyOfGravatarSourceURL := *setting.GravatarSourceURL
|
|
|
|
avatarURL = ©OfGravatarSourceURL
|
|
|
|
avatarURL.Path = path.Join(avatarURL.Path, hash)
|
|
|
|
} else {
|
2020-12-03 13:46:11 -05:00
|
|
|
avatarURL, err = url.Parse(models.DefaultAvatarLink())
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("invalid default avatar url", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:05:44 -05:00
|
|
|
cacheableRedirect(ctx, models.MakeFinalAvatarURL(avatarURL, size))
|
2020-03-27 07:34:39 -05:00
|
|
|
}
|