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.

picture.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 setting
  5. import (
  6. "net/url"
  7. "code.gitea.io/gitea/modules/log"
  8. "strk.kbt.io/projects/go/libravatar"
  9. )
  10. // settings
  11. var (
  12. // Picture settings
  13. Avatar = struct {
  14. Storage
  15. MaxWidth int
  16. MaxHeight int
  17. MaxFileSize int64
  18. }{
  19. MaxWidth: 4096,
  20. MaxHeight: 3072,
  21. MaxFileSize: 1048576,
  22. }
  23. GravatarSource string
  24. GravatarSourceURL *url.URL
  25. DisableGravatar bool
  26. EnableFederatedAvatar bool
  27. LibravatarService *libravatar.Libravatar
  28. RepoAvatar = struct {
  29. Storage
  30. Fallback string
  31. FallbackImage string
  32. }{}
  33. )
  34. func newPictureService() {
  35. sec := Cfg.Section("picture")
  36. avatarSec := Cfg.Section("avatar")
  37. storageType := sec.Key("AVATAR_STORAGE_TYPE").MustString("")
  38. // Specifically default PATH to AVATAR_UPLOAD_PATH
  39. avatarSec.Key("PATH").MustString(
  40. sec.Key("AVATAR_UPLOAD_PATH").String())
  41. Avatar.Storage = getStorage("avatars", storageType, avatarSec)
  42. Avatar.MaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
  43. Avatar.MaxHeight = sec.Key("AVATAR_MAX_HEIGHT").MustInt(3072)
  44. Avatar.MaxFileSize = sec.Key("AVATAR_MAX_FILE_SIZE").MustInt64(1048576)
  45. switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
  46. case "duoshuo":
  47. GravatarSource = "http://gravatar.duoshuo.com/avatar/"
  48. case "gravatar":
  49. GravatarSource = "https://secure.gravatar.com/avatar/"
  50. case "libravatar":
  51. GravatarSource = "https://seccdn.libravatar.org/avatar/"
  52. default:
  53. GravatarSource = source
  54. }
  55. DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool()
  56. EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(!InstallLock)
  57. if OfflineMode {
  58. DisableGravatar = true
  59. EnableFederatedAvatar = false
  60. }
  61. if DisableGravatar {
  62. EnableFederatedAvatar = false
  63. }
  64. if EnableFederatedAvatar || !DisableGravatar {
  65. var err error
  66. GravatarSourceURL, err = url.Parse(GravatarSource)
  67. if err != nil {
  68. log.Fatal("Failed to parse Gravatar URL(%s): %v",
  69. GravatarSource, err)
  70. }
  71. }
  72. if EnableFederatedAvatar {
  73. LibravatarService = libravatar.New()
  74. if GravatarSourceURL.Scheme == "https" {
  75. LibravatarService.SetUseHTTPS(true)
  76. LibravatarService.SetSecureFallbackHost(GravatarSourceURL.Host)
  77. } else {
  78. LibravatarService.SetUseHTTPS(false)
  79. LibravatarService.SetFallbackHost(GravatarSourceURL.Host)
  80. }
  81. }
  82. newRepoAvatarService()
  83. }
  84. func newRepoAvatarService() {
  85. sec := Cfg.Section("picture")
  86. repoAvatarSec := Cfg.Section("repo-avatar")
  87. storageType := sec.Key("REPOSITORY_AVATAR_STORAGE_TYPE").MustString("")
  88. // Specifically default PATH to AVATAR_UPLOAD_PATH
  89. repoAvatarSec.Key("PATH").MustString(
  90. sec.Key("REPOSITORY_AVATAR_UPLOAD_PATH").String())
  91. RepoAvatar.Storage = getStorage("repo-avatars", storageType, repoAvatarSec)
  92. RepoAvatar.Fallback = sec.Key("REPOSITORY_AVATAR_FALLBACK").MustString("none")
  93. RepoAvatar.FallbackImage = sec.Key("REPOSITORY_AVATAR_FALLBACK_IMAGE").MustString("/img/repo_default.png")
  94. }