driver.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package op
  2. import (
  3. "reflect"
  4. "strings"
  5. "github.com/IceWhaleTech/CasaOS/internal/conf"
  6. "github.com/IceWhaleTech/CasaOS/internal/driver"
  7. "github.com/pkg/errors"
  8. )
  9. type New func() driver.Driver
  10. var driverNewMap = map[string]New{}
  11. var driverInfoMap = map[string][]driver.Item{} //driver.Info{}
  12. func RegisterDriver(driver New) {
  13. // log.Infof("register driver: [%s]", config.Name)
  14. tempDriver := driver()
  15. tempConfig := tempDriver.Config()
  16. registerDriverItems(tempConfig, tempDriver.GetAddition())
  17. driverNewMap[tempConfig.Name] = driver
  18. }
  19. func GetDriverNew(name string) (New, error) {
  20. n, ok := driverNewMap[name]
  21. if !ok {
  22. return nil, errors.Errorf("no driver named: %s", name)
  23. }
  24. return n, nil
  25. }
  26. func GetDriverNames() []string {
  27. var driverNames []string
  28. for k := range driverInfoMap {
  29. driverNames = append(driverNames, k)
  30. }
  31. return driverNames
  32. }
  33. // func GetDriverInfoMap() map[string]driver.Info {
  34. // return driverInfoMap
  35. // }
  36. func GetDriverInfoMap() map[string][]driver.Item {
  37. return driverInfoMap
  38. }
  39. func registerDriverItems(config driver.Config, addition driver.Additional) {
  40. // log.Debugf("addition of %s: %+v", config.Name, addition)
  41. tAddition := reflect.TypeOf(addition)
  42. for tAddition.Kind() == reflect.Pointer {
  43. tAddition = tAddition.Elem()
  44. }
  45. //mainItems := getMainItems(config)
  46. additionalItems := getAdditionalItems(tAddition, config.DefaultRoot)
  47. driverInfoMap[config.Name] = additionalItems
  48. // driver.Info{
  49. // Common: mainItems,
  50. // Additional: additionalItems,
  51. // Config: config,
  52. // }
  53. }
  54. func getMainItems(config driver.Config) []driver.Item {
  55. items := []driver.Item{{
  56. Name: "mount_path",
  57. Type: conf.TypeString,
  58. Required: true,
  59. Help: "",
  60. }, {
  61. Name: "order",
  62. Type: conf.TypeNumber,
  63. Help: "use to sort",
  64. }, {
  65. Name: "remark",
  66. Type: conf.TypeText,
  67. }}
  68. if !config.NoCache {
  69. items = append(items, driver.Item{
  70. Name: "cache_expiration",
  71. Type: conf.TypeNumber,
  72. Default: "30",
  73. Required: true,
  74. Help: "The cache expiration time for this storage",
  75. })
  76. }
  77. if !config.OnlyProxy && !config.OnlyLocal {
  78. items = append(items, []driver.Item{{
  79. Name: "web_proxy",
  80. Type: conf.TypeBool,
  81. }, {
  82. Name: "webdav_policy",
  83. Type: conf.TypeSelect,
  84. Options: "302_redirect,use_proxy_url,native_proxy",
  85. Default: "302_redirect",
  86. Required: true,
  87. },
  88. }...)
  89. } else {
  90. items = append(items, driver.Item{
  91. Name: "webdav_policy",
  92. Type: conf.TypeSelect,
  93. Default: "native_proxy",
  94. Options: "use_proxy_url,native_proxy",
  95. Required: true,
  96. })
  97. }
  98. items = append(items, driver.Item{
  99. Name: "down_proxy_url",
  100. Type: conf.TypeText,
  101. })
  102. if config.LocalSort {
  103. items = append(items, []driver.Item{{
  104. Name: "order_by",
  105. Type: conf.TypeSelect,
  106. Options: "name,size,modified",
  107. }, {
  108. Name: "order_direction",
  109. Type: conf.TypeSelect,
  110. Options: "asc,desc",
  111. }}...)
  112. }
  113. items = append(items, driver.Item{
  114. Name: "extract_folder",
  115. Type: conf.TypeSelect,
  116. Options: "front,back",
  117. })
  118. return items
  119. }
  120. func getAdditionalItems(t reflect.Type, defaultRoot string) []driver.Item {
  121. var items []driver.Item
  122. for i := 0; i < t.NumField(); i++ {
  123. field := t.Field(i)
  124. if field.Type.Kind() == reflect.Struct {
  125. items = append(items, getAdditionalItems(field.Type, defaultRoot)...)
  126. continue
  127. }
  128. tag := field.Tag
  129. ignore, ok1 := tag.Lookup("ignore")
  130. name, ok2 := tag.Lookup("json")
  131. if (ok1 && ignore == "true") || !ok2 {
  132. continue
  133. }
  134. if tag.Get("omit") == "true" {
  135. continue
  136. }
  137. item := driver.Item{
  138. Name: name,
  139. Type: strings.ToLower(field.Type.Name()),
  140. Default: tag.Get("default"),
  141. Options: tag.Get("options"),
  142. Required: tag.Get("required") == "true",
  143. Help: tag.Get("help"),
  144. }
  145. if tag.Get("type") != "" {
  146. item.Type = tag.Get("type")
  147. }
  148. if item.Name == "root_folder_id" || item.Name == "root_folder_path" {
  149. if item.Default == "" {
  150. item.Default = defaultRoot
  151. }
  152. item.Required = item.Default != ""
  153. }
  154. // set default type to string
  155. if item.Type == "" {
  156. item.Type = "string"
  157. }
  158. items = append(items, item)
  159. }
  160. return items
  161. }