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.

43 lines
1.0 KiB

  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. "strconv"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // Avatar redirect browser to user avatar of requested size
  13. func Avatar(ctx *context.Context) {
  14. userName := ctx.Params(":username")
  15. size, err := strconv.Atoi(ctx.Params(":size"))
  16. if err != nil {
  17. ctx.ServerError("Invalid avatar size", err)
  18. return
  19. }
  20. log.Debug("Asked avatar for user %v and size %v", userName, size)
  21. var user *models.User
  22. if strings.ToLower(userName) != "ghost" {
  23. user, err = models.GetUserByName(userName)
  24. if err != nil {
  25. if models.IsErrUserNotExist(err) {
  26. ctx.ServerError("Requested avatar for invalid user", err)
  27. } else {
  28. ctx.ServerError("Retrieving user by name", err)
  29. }
  30. return
  31. }
  32. } else {
  33. user = models.NewGhostUser()
  34. }
  35. ctx.Redirect(user.RealSizedAvatarLink(size))
  36. }