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.

avatar.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "errors"
  7. "strconv"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. )
  14. // Avatar redirect browser to user avatar of requested size
  15. func Avatar(ctx *context.Context) {
  16. userName := ctx.Params(":username")
  17. size, err := strconv.Atoi(ctx.Params(":size"))
  18. if err != nil {
  19. ctx.ServerError("Invalid avatar size", err)
  20. return
  21. }
  22. log.Debug("Asked avatar for user %v and size %v", userName, size)
  23. var user *models.User
  24. if strings.ToLower(userName) != "ghost" {
  25. user, err = models.GetUserByName(userName)
  26. if err != nil {
  27. if models.IsErrUserNotExist(err) {
  28. ctx.ServerError("Requested avatar for invalid user", err)
  29. } else {
  30. ctx.ServerError("Retrieving user by name", err)
  31. }
  32. return
  33. }
  34. } else {
  35. user = models.NewGhostUser()
  36. }
  37. ctx.Redirect(user.RealSizedAvatarLink(size))
  38. }
  39. // AvatarByEmailHash redirects the browser to the appropriate Avatar link
  40. func AvatarByEmailHash(ctx *context.Context) {
  41. hash := ctx.Params(":hash")
  42. if len(hash) == 0 {
  43. ctx.ServerError("invalid avatar hash", errors.New("hash cannot be empty"))
  44. return
  45. }
  46. email, err := models.GetEmailForHash(hash)
  47. if err != nil {
  48. ctx.ServerError("invalid avatar hash", err)
  49. return
  50. }
  51. if len(email) == 0 {
  52. ctx.Redirect(base.DefaultAvatarLink())
  53. return
  54. }
  55. size := ctx.QueryInt("size")
  56. if size == 0 {
  57. size = base.DefaultAvatarSize
  58. }
  59. ctx.Redirect(base.SizedAvatarLinkWithDomain(email, size))
  60. }