2019-09-26 19:21:23 +03: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 15:34:39 +03:00
"errors"
2020-12-03 21:46:11 +03:00
"net/url"
2021-04-13 03:57:12 +03:00
"path"
2019-09-26 19:21:23 +03:00
"strconv"
2019-12-29 17:24:50 +03:00
"strings"
2019-09-26 19:21:23 +03:00
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
2021-04-13 03:57:12 +03:00
"code.gitea.io/gitea/modules/setting"
2019-09-26 19:21:23 +03: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 17:24:50 +03: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 19:21:23 +03:00
}
2019-12-29 17:24:50 +03:00
} else {
user = models . NewGhostUser ( )
2019-09-26 19:21:23 +03:00
}
ctx . Redirect ( user . RealSizedAvatarLink ( size ) )
}
2020-03-27 15:34:39 +03:00
// AvatarByEmailHash redirects the browser to the appropriate Avatar link
func AvatarByEmailHash ( ctx * context . Context ) {
2020-12-03 21:46:11 +03:00
var err error
2020-03-27 15:34:39 +03:00
hash := ctx . Params ( ":hash" )
if len ( hash ) == 0 {
ctx . ServerError ( "invalid avatar hash" , errors . New ( "hash cannot be empty" ) )
return
}
2020-12-03 21:46:11 +03:00
var email string
email , err = models . GetEmailForHash ( hash )
2020-03-27 15:34:39 +03:00
if err != nil {
ctx . ServerError ( "invalid avatar hash" , err )
return
}
if len ( email ) == 0 {
2020-12-03 21:46:11 +03:00
ctx . Redirect ( models . DefaultAvatarLink ( ) )
2020-03-27 15:34:39 +03:00
return
}
size := ctx . QueryInt ( "size" )
if size == 0 {
2020-12-03 21:46:11 +03:00
size = models . DefaultAvatarSize
2020-03-27 15:34:39 +03:00
}
2020-12-03 21:46:11 +03:00
var avatarURL * url . URL
2021-04-13 03:57:12 +03: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 = & copyOfGravatarSourceURL
avatarURL . Path = path . Join ( avatarURL . Path , hash )
} else {
2020-12-03 21:46:11 +03:00
avatarURL , err = url . Parse ( models . DefaultAvatarLink ( ) )
if err != nil {
ctx . ServerError ( "invalid default avatar url" , err )
return
}
}
ctx . Redirect ( models . MakeFinalAvatarURL ( avatarURL , size ) )
2020-03-27 15:34:39 +03:00
}