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.

lfs.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. 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. LFS.Storage.Type = lfsSec.Key("STORAGE_TYPE").MustString("")
  33. if LFS.Storage.Type == "" {
  34. LFS.Storage.Type = "default"
  35. }
  36. if LFS.Storage.Type != LocalStorageType && LFS.Storage.Type != MinioStorageType {
  37. storage, ok := storages[LFS.Storage.Type]
  38. if !ok {
  39. log.Fatal("Failed to get lfs storage type: %s", LFS.Storage.Type)
  40. }
  41. LFS.Storage = storage
  42. }
  43. // Override
  44. LFS.ServeDirect = lfsSec.Key("SERVE_DIRECT").MustBool(LFS.ServeDirect)
  45. switch LFS.Storage.Type {
  46. case LocalStorageType:
  47. // keep compatible
  48. LFS.Path = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
  49. LFS.Path = lfsSec.Key("PATH").MustString(LFS.Path)
  50. if !filepath.IsAbs(LFS.Path) {
  51. LFS.Path = filepath.Join(AppWorkPath, LFS.Path)
  52. }
  53. case MinioStorageType:
  54. LFS.Minio.Endpoint = lfsSec.Key("MINIO_ENDPOINT").MustString(LFS.Minio.Endpoint)
  55. LFS.Minio.AccessKeyID = lfsSec.Key("MINIO_ACCESS_KEY_ID").MustString(LFS.Minio.AccessKeyID)
  56. LFS.Minio.SecretAccessKey = lfsSec.Key("MINIO_SECRET_ACCESS_KEY").MustString(LFS.Minio.SecretAccessKey)
  57. LFS.Minio.Bucket = lfsSec.Key("MINIO_BUCKET").MustString(LFS.Minio.Bucket)
  58. LFS.Minio.Location = lfsSec.Key("MINIO_LOCATION").MustString(LFS.Minio.Location)
  59. LFS.Minio.UseSSL = lfsSec.Key("MINIO_USE_SSL").MustBool(LFS.Minio.UseSSL)
  60. LFS.Minio.BasePath = lfsSec.Key("MINIO_BASE_PATH").MustString("lfs/")
  61. }
  62. if LFS.LocksPagingNum == 0 {
  63. LFS.LocksPagingNum = 50
  64. }
  65. LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(20 * time.Minute)
  66. if LFS.StartServer {
  67. LFS.JWTSecretBytes = make([]byte, 32)
  68. n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64))
  69. if err != nil || n != 32 {
  70. LFS.JWTSecretBase64, err = generate.NewJwtSecret()
  71. if err != nil {
  72. log.Fatal("Error generating JWT Secret for custom config: %v", err)
  73. return
  74. }
  75. // Save secret
  76. cfg := ini.Empty()
  77. if com.IsFile(CustomConf) {
  78. // Keeps custom settings if there is already something.
  79. if err := cfg.Append(CustomConf); err != nil {
  80. log.Error("Failed to load custom conf '%s': %v", CustomConf, err)
  81. }
  82. }
  83. cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
  84. if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
  85. log.Fatal("Failed to create '%s': %v", CustomConf, err)
  86. }
  87. if err := cfg.SaveTo(CustomConf); err != nil {
  88. log.Fatal("Error saving generated JWT Secret to custom config: %v", err)
  89. return
  90. }
  91. }
  92. }
  93. }
  94. // CheckLFSVersion will check lfs version, if not satisfied, then disable it.
  95. func CheckLFSVersion() {
  96. if LFS.StartServer {
  97. //Disable LFS client hooks if installed for the current OS user
  98. //Needs at least git v2.1.2
  99. err := git.LoadGitVersion()
  100. if err != nil {
  101. log.Fatal("Error retrieving git version: %v", err)
  102. }
  103. if git.CheckGitVersionConstraint(">= 2.1.2") != nil {
  104. LFS.StartServer = false
  105. log.Error("LFS server support needs at least Git v2.1.2")
  106. } else {
  107. git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "filter.lfs.required=",
  108. "-c", "filter.lfs.smudge=", "-c", "filter.lfs.clean=")
  109. }
  110. }
  111. }