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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2020 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 models
  5. import (
  6. "crypto/md5"
  7. "fmt"
  8. "net/url"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "code.gitea.io/gitea/modules/base"
  13. "code.gitea.io/gitea/modules/cache"
  14. "code.gitea.io/gitea/modules/log"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. // EmailHash represents a pre-generated hash map
  18. type EmailHash struct {
  19. Hash string `xorm:"pk varchar(32)"`
  20. Email string `xorm:"UNIQUE NOT NULL"`
  21. }
  22. // DefaultAvatarLink the default avatar link
  23. func DefaultAvatarLink() string {
  24. u, err := url.Parse(setting.AppSubURL)
  25. if err != nil {
  26. log.Error("GetUserByEmail: %v", err)
  27. return ""
  28. }
  29. u.Path = path.Join(u.Path, "/img/avatar_default.png")
  30. return u.String()
  31. }
  32. // DefaultAvatarSize is a sentinel value for the default avatar size, as
  33. // determined by the avatar-hosting service.
  34. const DefaultAvatarSize = -1
  35. // DefaultAvatarPixelSize is the default size in pixels of a rendered avatar
  36. const DefaultAvatarPixelSize = 28
  37. // AvatarRenderedSizeFactor is the factor by which the default size is increased for finer rendering
  38. const AvatarRenderedSizeFactor = 2
  39. // HashEmail hashes email address to MD5 string.
  40. // https://en.gravatar.com/site/implement/hash/
  41. func HashEmail(email string) string {
  42. return base.EncodeMD5(strings.ToLower(strings.TrimSpace(email)))
  43. }
  44. // GetEmailForHash converts a provided md5sum to the email
  45. func GetEmailForHash(md5Sum string) (string, error) {
  46. return cache.GetString("Avatar:"+md5Sum, func() (string, error) {
  47. emailHash := EmailHash{
  48. Hash: strings.ToLower(strings.TrimSpace(md5Sum)),
  49. }
  50. _, err := x.Get(&emailHash)
  51. return emailHash.Email, err
  52. })
  53. }
  54. // LibravatarURL returns the URL for the given email. This function should only
  55. // be called if a federated avatar service is enabled.
  56. func LibravatarURL(email string) (*url.URL, error) {
  57. urlStr, err := setting.LibravatarService.FromEmail(email)
  58. if err != nil {
  59. log.Error("LibravatarService.FromEmail(email=%s): error %v", email, err)
  60. return nil, err
  61. }
  62. u, err := url.Parse(urlStr)
  63. if err != nil {
  64. log.Error("Failed to parse libravatar url(%s): error %v", urlStr, err)
  65. return nil, err
  66. }
  67. return u, nil
  68. }
  69. // HashedAvatarLink returns an avatar link for a provided email
  70. func HashedAvatarLink(email string) string {
  71. lowerEmail := strings.ToLower(strings.TrimSpace(email))
  72. sum := fmt.Sprintf("%x", md5.Sum([]byte(lowerEmail)))
  73. _, _ = cache.GetString("Avatar:"+sum, func() (string, error) {
  74. emailHash := &EmailHash{
  75. Email: lowerEmail,
  76. Hash: sum,
  77. }
  78. // OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors
  79. sess := x.NewSession()
  80. defer sess.Close()
  81. if err := sess.Begin(); err != nil {
  82. // we don't care about any DB problem just return the lowerEmail
  83. return lowerEmail, nil
  84. }
  85. has, err := sess.Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash))
  86. if has || err != nil {
  87. // Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time
  88. return lowerEmail, nil
  89. }
  90. _, _ = sess.Insert(emailHash)
  91. if err := sess.Commit(); err != nil {
  92. // Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time
  93. return lowerEmail, nil
  94. }
  95. return lowerEmail, nil
  96. })
  97. return setting.AppSubURL + "/avatar/" + url.PathEscape(sum)
  98. }
  99. // MakeFinalAvatarURL constructs the final avatar URL string
  100. func MakeFinalAvatarURL(u *url.URL, size int) string {
  101. vals := u.Query()
  102. vals.Set("d", "identicon")
  103. if size != DefaultAvatarSize {
  104. vals.Set("s", strconv.Itoa(size))
  105. }
  106. u.RawQuery = vals.Encode()
  107. return u.String()
  108. }
  109. // SizedAvatarLink returns a sized link to the avatar for the given email address.
  110. func SizedAvatarLink(email string, size int) string {
  111. var avatarURL *url.URL
  112. if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
  113. // This is the slow path that would need to call LibravatarURL() which
  114. // does DNS lookups. Avoid it by issuing a redirect so we don't block
  115. // the template render with network requests.
  116. return HashedAvatarLink(email)
  117. } else if !setting.DisableGravatar {
  118. // copy GravatarSourceURL, because we will modify its Path.
  119. copyOfGravatarSourceURL := *setting.GravatarSourceURL
  120. avatarURL = &copyOfGravatarSourceURL
  121. avatarURL.Path = path.Join(avatarURL.Path, HashEmail(email))
  122. } else {
  123. return DefaultAvatarLink()
  124. }
  125. return MakeFinalAvatarURL(avatarURL, size)
  126. }