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.

storage.go 2.3KB

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