Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

storage.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "path/filepath"
  7. "reflect"
  8. ini "gopkg.in/ini.v1"
  9. )
  10. // Storage represents configuration of storages
  11. type Storage struct {
  12. Type string
  13. Path string
  14. Section *ini.Section
  15. ServeDirect bool
  16. }
  17. // MapTo implements the Mappable interface
  18. func (s *Storage) MapTo(v interface{}) error {
  19. pathValue := reflect.ValueOf(v).Elem().FieldByName("Path")
  20. if pathValue.IsValid() && pathValue.Kind() == reflect.String {
  21. pathValue.SetString(s.Path)
  22. }
  23. if s.Section != nil {
  24. return s.Section.MapTo(v)
  25. }
  26. return nil
  27. }
  28. func getStorage(name, typ string, targetSec *ini.Section) Storage {
  29. const sectionName = "storage"
  30. sec := Cfg.Section(sectionName)
  31. // Global Defaults
  32. sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
  33. sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
  34. sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
  35. sec.Key("MINIO_BUCKET").MustString("gitea")
  36. sec.Key("MINIO_LOCATION").MustString("us-east-1")
  37. sec.Key("MINIO_USE_SSL").MustBool(false)
  38. var storage Storage
  39. storage.Section = targetSec
  40. storage.Type = typ
  41. overrides := make([]*ini.Section, 0, 3)
  42. nameSec, err := Cfg.GetSection(sectionName + "." + name)
  43. if err == nil {
  44. overrides = append(overrides, nameSec)
  45. }
  46. typeSec, err := Cfg.GetSection(sectionName + "." + typ)
  47. if err == nil {
  48. overrides = append(overrides, typeSec)
  49. nextType := typeSec.Key("STORAGE_TYPE").String()
  50. if len(nextType) > 0 {
  51. storage.Type = nextType // Support custom STORAGE_TYPE
  52. }
  53. }
  54. overrides = append(overrides, sec)
  55. for _, override := range overrides {
  56. for _, key := range override.Keys() {
  57. if !targetSec.HasKey(key.Name()) {
  58. _, _ = targetSec.NewKey(key.Name(), key.Value())
  59. }
  60. }
  61. if len(storage.Type) == 0 {
  62. storage.Type = override.Key("STORAGE_TYPE").String()
  63. }
  64. }
  65. storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)
  66. // Specific defaults
  67. storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name))
  68. if !filepath.IsAbs(storage.Path) {
  69. storage.Path = filepath.Join(AppWorkPath, storage.Path)
  70. storage.Section.Key("PATH").SetValue(storage.Path)
  71. }
  72. storage.Section.Key("MINIO_BASE_PATH").MustString(name + "/")
  73. return storage
  74. }