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.

122 lines
3.3 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. "github.com/unknwon/com"
  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. ContentPath string `ini:"LFS_CONTENT_PATH"`
  20. JWTSecretBase64 string `ini:"LFS_JWT_SECRET"`
  21. JWTSecretBytes []byte `ini:"-"`
  22. HTTPAuthExpiry time.Duration `ini:"LFS_HTTP_AUTH_EXPIRY"`
  23. MaxFileSize int64 `ini:"LFS_MAX_FILE_SIZE"`
  24. LocksPagingNum int `ini:"LFS_LOCKS_PAGING_NUM"`
  25. StoreType string
  26. ServeDirect bool
  27. Minio struct {
  28. Endpoint string
  29. AccessKeyID string
  30. SecretAccessKey string
  31. UseSSL bool
  32. Bucket string
  33. Location string
  34. BasePath string
  35. }
  36. }{
  37. StoreType: "local",
  38. }
  39. func newLFSService() {
  40. sec := Cfg.Section("server")
  41. if err := sec.MapTo(&LFS); err != nil {
  42. log.Fatal("Failed to map LFS settings: %v", err)
  43. }
  44. LFS.ContentPath = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
  45. if !filepath.IsAbs(LFS.ContentPath) {
  46. LFS.ContentPath = filepath.Join(AppWorkPath, LFS.ContentPath)
  47. }
  48. if LFS.LocksPagingNum == 0 {
  49. LFS.LocksPagingNum = 50
  50. }
  51. LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
  52. if LFS.StartServer {
  53. LFS.JWTSecretBytes = make([]byte, 32)
  54. n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))
  55. if err != nil || n != 32 {
  56. LFS.JWTSecretBase64, err = generate.NewJwtSecret()
  57. if err != nil {
  58. log.Fatal("Error generating JWT Secret for custom config: %v", err)
  59. return
  60. }
  61. // Save secret
  62. cfg := ini.Empty()
  63. if com.IsFile(CustomConf) {
  64. // Keeps custom settings if there is already something.
  65. if err := cfg.Append(CustomConf); err != nil {
  66. log.Error("Failed to load custom conf '%s': %v", CustomConf, err)
  67. }
  68. }
  69. cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
  70. if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
  71. log.Fatal("Failed to create '%s': %v", CustomConf, err)
  72. }
  73. if err := cfg.SaveTo(CustomConf); err != nil {
  74. log.Fatal("Error saving generated JWT Secret to custom config: %v", err)
  75. return
  76. }
  77. }
  78. }
  79. }
  80. func ensureLFSDirectory() {
  81. if LFS.StartServer {
  82. if err := os.MkdirAll(LFS.ContentPath, 0700); err != nil {
  83. log.Fatal("Failed to create '%s': %v", LFS.ContentPath, err)
  84. }
  85. }
  86. }
  87. // CheckLFSVersion will check lfs version, if not satisfied, then disable it.
  88. func CheckLFSVersion() {
  89. if LFS.StartServer {
  90. //Disable LFS client hooks if installed for the current OS user
  91. //Needs at least git v2.1.2
  92. err := git.LoadGitVersion()
  93. if err != nil {
  94. log.Fatal("Error retrieving git version: %v", err)
  95. }
  96. if git.CheckGitVersionConstraint(">= 2.1.2") != nil {
  97. LFS.StartServer = false
  98. log.Error("LFS server support needs at least Git v2.1.2")
  99. } else {
  100. git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
  101. "-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
  102. }
  103. }
  104. }