gitea源码

util_avatar.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "context"
  6. "html"
  7. "html/template"
  8. "strconv"
  9. activities_model "code.gitea.io/gitea/models/activities"
  10. "code.gitea.io/gitea/models/avatars"
  11. "code.gitea.io/gitea/models/organization"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. user_model "code.gitea.io/gitea/models/user"
  14. gitea_html "code.gitea.io/gitea/modules/htmlutil"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. type AvatarUtils struct {
  18. ctx context.Context
  19. }
  20. func NewAvatarUtils(ctx context.Context) *AvatarUtils {
  21. return &AvatarUtils{ctx: ctx}
  22. }
  23. // AvatarHTML creates the HTML for an avatar
  24. func AvatarHTML(src string, size int, class, name string) template.HTML {
  25. sizeStr := strconv.Itoa(size)
  26. if name == "" {
  27. name = "avatar"
  28. }
  29. // use empty alt, otherwise if the image fails to load, the width will follow the "alt" text's width
  30. return template.HTML(`<img loading="lazy" alt class="` + class + `" src="` + src + `" title="` + html.EscapeString(name) + `" width="` + sizeStr + `" height="` + sizeStr + `"/>`)
  31. }
  32. // Avatar renders user avatars. args: user, size (int), class (string)
  33. func (au *AvatarUtils) Avatar(item any, others ...any) template.HTML {
  34. size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
  35. switch t := item.(type) {
  36. case *user_model.User:
  37. src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
  38. if src != "" {
  39. return AvatarHTML(src, size, class, t.DisplayName())
  40. }
  41. case *repo_model.Collaborator:
  42. src := t.AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
  43. if src != "" {
  44. return AvatarHTML(src, size, class, t.DisplayName())
  45. }
  46. case *organization.Organization:
  47. src := t.AsUser().AvatarLinkWithSize(au.ctx, size*setting.Avatar.RenderedSizeFactor)
  48. if src != "" {
  49. return AvatarHTML(src, size, class, t.AsUser().DisplayName())
  50. }
  51. }
  52. return AvatarHTML(avatars.DefaultAvatarLink(), size, class, "")
  53. }
  54. // AvatarByAction renders user avatars from action. args: action, size (int), class (string)
  55. func (au *AvatarUtils) AvatarByAction(action *activities_model.Action, others ...any) template.HTML {
  56. action.LoadActUser(au.ctx)
  57. return au.Avatar(action.ActUser, others...)
  58. }
  59. // AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
  60. func (au *AvatarUtils) AvatarByEmail(email, name string, others ...any) template.HTML {
  61. size, class := gitea_html.ParseSizeAndClass(avatars.DefaultAvatarPixelSize, avatars.DefaultAvatarClass, others...)
  62. src := avatars.GenerateEmailAvatarFastLink(au.ctx, email, size*setting.Avatar.RenderedSizeFactor)
  63. if src != "" {
  64. return AvatarHTML(src, size, class, name)
  65. }
  66. return ""
  67. }