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