A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

111 lines
3.1 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 setting
  5. import (
  6. "encoding/base64"
  7. "os"
  8. "path/filepath"
  9. "time"
  10. "code.gitea.io/gitea/modules/generate"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/util"
  14. ini "gopkg.in/ini.v1"
  15. )
  16. // LFS represents the configuration for Git LFS
  17. var LFS = struct {
  18. StartServer bool `ini:"LFS_START_SERVER"`
  19. JWTSecretBase64 string `ini:"LFS_JWT_SECRET"`
  20. JWTSecretBytes []byte `ini:"-"`
  21. HTTPAuthExpiry time.Duration `ini:"LFS_HTTP_AUTH_EXPIRY"`
  22. MaxFileSize int64 `ini:"LFS_MAX_FILE_SIZE"`
  23. LocksPagingNum int `ini:"LFS_LOCKS_PAGING_NUM"`
  24. Storage
  25. }{}
  26. func newLFSService() {
  27. sec := Cfg.Section("server")
  28. if err := sec.MapTo(&LFS); err != nil {
  29. log.Fatal("Failed to map LFS settings: %v", err)
  30. }
  31. lfsSec := Cfg.Section("lfs")
  32. storageType := lfsSec.Key("STORAGE_TYPE").MustString("")
  33. // Specifically default PATH to LFS_CONTENT_PATH
  34. lfsSec.Key("PATH").MustString(
  35. sec.Key("LFS_CONTENT_PATH").String())
  36. LFS.Storage = getStorage("lfs", storageType, lfsSec)
  37. // Rest of LFS service settings
  38. if LFS.LocksPagingNum == 0 {
  39. LFS.LocksPagingNum = 50
  40. }
  41. LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
  42. if LFS.StartServer {
  43. LFS.JWTSecretBytes = make([]byte, 32)
  44. n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))
  45. if err != nil || n != 32 {
  46. LFS.JWTSecretBase64, err = generate.NewJwtSecret()
  47. if err != nil {
  48. log.Fatal("Error generating JWT Secret for custom config: %v", err)
  49. return
  50. }
  51. // Save secret
  52. cfg := ini.Empty()
  53. isFile, err := util.IsFile(CustomConf)
  54. if err != nil {
  55. log.Error("Unable to check if %s is a file. Error: %v", CustomConf, err)
  56. }
  57. if isFile {
  58. // Keeps custom settings if there is already something.
  59. if err := cfg.Append(CustomConf); err != nil {
  60. log.Error("Failed to load custom conf '%s': %v", CustomConf, err)
  61. }
  62. }
  63. cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
  64. if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
  65. log.Fatal("Failed to create '%s': %v", CustomConf, err)
  66. }
  67. if err := cfg.SaveTo(CustomConf); err != nil {
  68. log.Fatal("Error saving generated JWT Secret to custom config: %v", err)
  69. return
  70. }
  71. }
  72. }
  73. }
  74. // CheckLFSVersion will check lfs version, if not satisfied, then disable it.
  75. func CheckLFSVersion() {
  76. if LFS.StartServer {
  77. //Disable LFS client hooks if installed for the current OS user
  78. //Needs at least git v2.1.2
  79. err := git.LoadGitVersion()
  80. if err != nil {
  81. log.Fatal("Error retrieving git version: %v", err)
  82. }
  83. if git.CheckGitVersionAtLeast("2.1.2") != nil {
  84. LFS.StartServer = false
  85. log.Error("LFS server support needs at least Git v2.1.2")
  86. } else {
  87. git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
  88. "-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
  89. }
  90. }
  91. }