gitea源码

lfs.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "fmt"
  6. "time"
  7. "code.gitea.io/gitea/modules/generate"
  8. )
  9. // LFS represents the server-side configuration for Git LFS.
  10. // Ideally these options should be in a section like "[lfs_server]",
  11. // but they are in "[server]" section due to historical reasons.
  12. // Could be refactored in the future while keeping backwards compatibility.
  13. var LFS = struct {
  14. StartServer bool `ini:"LFS_START_SERVER"`
  15. AllowPureSSH bool `ini:"LFS_ALLOW_PURE_SSH"`
  16. JWTSecretBytes []byte `ini:"-"`
  17. HTTPAuthExpiry time.Duration `ini:"LFS_HTTP_AUTH_EXPIRY"`
  18. MaxFileSize int64 `ini:"LFS_MAX_FILE_SIZE"`
  19. LocksPagingNum int `ini:"LFS_LOCKS_PAGING_NUM"`
  20. MaxBatchSize int `ini:"LFS_MAX_BATCH_SIZE"`
  21. Storage *Storage
  22. }{}
  23. // LFSClient represents configuration for Gitea's LFS clients, for example: mirroring upstream Git LFS
  24. var LFSClient = struct {
  25. BatchSize int `ini:"BATCH_SIZE"`
  26. BatchOperationConcurrency int `ini:"BATCH_OPERATION_CONCURRENCY"`
  27. }{}
  28. func loadLFSFrom(rootCfg ConfigProvider) error {
  29. mustMapSetting(rootCfg, "lfs_client", &LFSClient)
  30. mustMapSetting(rootCfg, "server", &LFS)
  31. sec := rootCfg.Section("server")
  32. lfsSec, _ := rootCfg.GetSection("lfs")
  33. // Specifically default PATH to LFS_CONTENT_PATH
  34. // DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version
  35. // if these are removed, the warning will not be shown
  36. deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0")
  37. if val := sec.Key("LFS_CONTENT_PATH").String(); val != "" {
  38. if lfsSec == nil {
  39. lfsSec = rootCfg.Section("lfs")
  40. }
  41. lfsSec.Key("PATH").MustString(val)
  42. }
  43. var err error
  44. LFS.Storage, err = getStorage(rootCfg, "lfs", "", lfsSec)
  45. if err != nil {
  46. return err
  47. }
  48. // Rest of LFS service settings
  49. if LFS.LocksPagingNum == 0 {
  50. LFS.LocksPagingNum = 50
  51. }
  52. if LFSClient.BatchSize < 1 {
  53. LFSClient.BatchSize = 20
  54. }
  55. if LFSClient.BatchOperationConcurrency < 1 {
  56. // match the default git-lfs's `lfs.concurrenttransfers` https://github.com/git-lfs/git-lfs/blob/main/docs/man/git-lfs-config.adoc#upload-and-download-transfer-settings
  57. LFSClient.BatchOperationConcurrency = 8
  58. }
  59. LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(24 * time.Hour)
  60. if !LFS.StartServer || !InstallLock {
  61. return nil
  62. }
  63. jwtSecretBase64 := loadSecret(rootCfg.Section("server"), "LFS_JWT_SECRET_URI", "LFS_JWT_SECRET")
  64. LFS.JWTSecretBytes, err = generate.DecodeJwtSecretBase64(jwtSecretBase64)
  65. if err != nil {
  66. LFS.JWTSecretBytes, jwtSecretBase64, err = generate.NewJwtSecretWithBase64()
  67. if err != nil {
  68. return fmt.Errorf("error generating JWT Secret for custom config: %v", err)
  69. }
  70. // Save secret
  71. saveCfg, err := rootCfg.PrepareSaving()
  72. if err != nil {
  73. return fmt.Errorf("error saving JWT Secret for custom config: %v", err)
  74. }
  75. rootCfg.Section("server").Key("LFS_JWT_SECRET").SetValue(jwtSecretBase64)
  76. saveCfg.Section("server").Key("LFS_JWT_SECRET").SetValue(jwtSecretBase64)
  77. if err := saveCfg.Save(); err != nil {
  78. return fmt.Errorf("error saving JWT Secret for custom config: %v", err)
  79. }
  80. }
  81. return nil
  82. }