mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.3 KiB
99 lines
2.3 KiB
5 years ago
|
// 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 (
|
||
5 years ago
|
"errors"
|
||
4 years ago
|
"net/url"
|
||
4 years ago
|
"path"
|
||
5 years ago
|
"strconv"
|
||
5 years ago
|
"strings"
|
||
5 years ago
|
|
||
|
"code.gitea.io/gitea/models"
|
||
|
"code.gitea.io/gitea/modules/context"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
5 years ago
|
)
|
||
|
|
||
|
// 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)
|
||
|
|
||
5 years ago
|
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
|
||
5 years ago
|
}
|
||
5 years ago
|
} else {
|
||
|
user = models.NewGhostUser()
|
||
5 years ago
|
}
|
||
|
|
||
|
ctx.Redirect(user.RealSizedAvatarLink(size))
|
||
|
}
|
||
5 years ago
|
|
||
|
// AvatarByEmailHash redirects the browser to the appropriate Avatar link
|
||
|
func AvatarByEmailHash(ctx *context.Context) {
|
||
4 years ago
|
var err error
|
||
|
|
||
5 years ago
|
hash := ctx.Params(":hash")
|
||
|
if len(hash) == 0 {
|
||
|
ctx.ServerError("invalid avatar hash", errors.New("hash cannot be empty"))
|
||
|
return
|
||
|
}
|
||
4 years ago
|
|
||
|
var email string
|
||
|
email, err = models.GetEmailForHash(hash)
|
||
5 years ago
|
if err != nil {
|
||
|
ctx.ServerError("invalid avatar hash", err)
|
||
|
return
|
||
|
}
|
||
|
if len(email) == 0 {
|
||
4 years ago
|
ctx.Redirect(models.DefaultAvatarLink())
|
||
5 years ago
|
return
|
||
|
}
|
||
|
size := ctx.QueryInt("size")
|
||
|
if size == 0 {
|
||
4 years ago
|
size = models.DefaultAvatarSize
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
|
var avatarURL *url.URL
|
||
4 years ago
|
|
||
|
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 {
|
||
4 years ago
|
avatarURL, err = url.Parse(models.DefaultAvatarLink())
|
||
|
if err != nil {
|
||
|
ctx.ServerError("invalid default avatar url", err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ctx.Redirect(models.MakeFinalAvatarURL(avatarURL, size))
|
||
5 years ago
|
}
|