Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/op/driver.go
1560 views
1
package op
2
3
import (
4
"reflect"
5
"strings"
6
7
"github.com/alist-org/alist/v3/internal/conf"
8
9
"github.com/alist-org/alist/v3/internal/driver"
10
"github.com/pkg/errors"
11
)
12
13
type DriverConstructor func() driver.Driver
14
15
var driverMap = map[string]DriverConstructor{}
16
var driverInfoMap = map[string]driver.Info{}
17
18
func RegisterDriver(driver DriverConstructor) {
19
// log.Infof("register driver: [%s]", config.Name)
20
tempDriver := driver()
21
tempConfig := tempDriver.Config()
22
registerDriverItems(tempConfig, tempDriver.GetAddition())
23
driverMap[tempConfig.Name] = driver
24
}
25
26
func GetDriver(name string) (DriverConstructor, error) {
27
n, ok := driverMap[name]
28
if !ok {
29
return nil, errors.Errorf("no driver named: %s", name)
30
}
31
return n, nil
32
}
33
34
func GetDriverNames() []string {
35
var driverNames []string
36
for k := range driverInfoMap {
37
driverNames = append(driverNames, k)
38
}
39
return driverNames
40
}
41
42
func GetDriverInfoMap() map[string]driver.Info {
43
return driverInfoMap
44
}
45
46
func registerDriverItems(config driver.Config, addition driver.Additional) {
47
// log.Debugf("addition of %s: %+v", config.Name, addition)
48
tAddition := reflect.TypeOf(addition)
49
for tAddition.Kind() == reflect.Pointer {
50
tAddition = tAddition.Elem()
51
}
52
mainItems := getMainItems(config)
53
additionalItems := getAdditionalItems(tAddition, config.DefaultRoot)
54
driverInfoMap[config.Name] = driver.Info{
55
Common: mainItems,
56
Additional: additionalItems,
57
Config: config,
58
}
59
}
60
61
func getMainItems(config driver.Config) []driver.Item {
62
items := []driver.Item{{
63
Name: "mount_path",
64
Type: conf.TypeString,
65
Required: true,
66
Help: "The path you want to mount to, it is unique and cannot be repeated",
67
}, {
68
Name: "order",
69
Type: conf.TypeNumber,
70
Help: "use to sort",
71
}, {
72
Name: "remark",
73
Type: conf.TypeText,
74
}}
75
if !config.NoCache {
76
items = append(items, driver.Item{
77
Name: "cache_expiration",
78
Type: conf.TypeNumber,
79
Default: "30",
80
Required: true,
81
Help: "The cache expiration time for this storage",
82
})
83
}
84
if !config.OnlyProxy && !config.OnlyLocal {
85
items = append(items, []driver.Item{{
86
Name: "web_proxy",
87
Type: conf.TypeBool,
88
}, {
89
Name: "webdav_policy",
90
Type: conf.TypeSelect,
91
Options: "302_redirect,use_proxy_url,native_proxy",
92
Default: "302_redirect",
93
Required: true,
94
},
95
}...)
96
if config.ProxyRangeOption {
97
item := driver.Item{
98
Name: "proxy_range",
99
Type: conf.TypeBool,
100
Help: "Need to enable proxy",
101
}
102
if config.Name == "139Yun" {
103
item.Default = "true"
104
}
105
items = append(items, item)
106
}
107
} else {
108
items = append(items, driver.Item{
109
Name: "webdav_policy",
110
Type: conf.TypeSelect,
111
Default: "native_proxy",
112
Options: "use_proxy_url,native_proxy",
113
Required: true,
114
})
115
}
116
items = append(items, driver.Item{
117
Name: "down_proxy_url",
118
Type: conf.TypeText,
119
})
120
if config.LocalSort {
121
items = append(items, []driver.Item{{
122
Name: "order_by",
123
Type: conf.TypeSelect,
124
Options: "name,size,modified",
125
}, {
126
Name: "order_direction",
127
Type: conf.TypeSelect,
128
Options: "asc,desc",
129
}}...)
130
}
131
items = append(items, driver.Item{
132
Name: "extract_folder",
133
Type: conf.TypeSelect,
134
Options: "front,back",
135
})
136
items = append(items, driver.Item{
137
Name: "disable_index",
138
Type: conf.TypeBool,
139
Default: "false",
140
Required: true,
141
})
142
items = append(items, driver.Item{
143
Name: "enable_sign",
144
Type: conf.TypeBool,
145
Default: "false",
146
Required: true,
147
})
148
return items
149
}
150
func getAdditionalItems(t reflect.Type, defaultRoot string) []driver.Item {
151
var items []driver.Item
152
for i := 0; i < t.NumField(); i++ {
153
field := t.Field(i)
154
if field.Type.Kind() == reflect.Struct {
155
items = append(items, getAdditionalItems(field.Type, defaultRoot)...)
156
continue
157
}
158
tag := field.Tag
159
ignore, ok1 := tag.Lookup("ignore")
160
name, ok2 := tag.Lookup("json")
161
if (ok1 && ignore == "true") || !ok2 {
162
continue
163
}
164
item := driver.Item{
165
Name: name,
166
Type: strings.ToLower(field.Type.Name()),
167
Default: tag.Get("default"),
168
Options: tag.Get("options"),
169
Required: tag.Get("required") == "true",
170
Help: tag.Get("help"),
171
}
172
if tag.Get("type") != "" {
173
item.Type = tag.Get("type")
174
}
175
if item.Name == "root_folder_id" || item.Name == "root_folder_path" {
176
if item.Default == "" {
177
item.Default = defaultRoot
178
}
179
item.Required = item.Default != ""
180
}
181
// set default type to string
182
if item.Type == "" {
183
item.Type = "string"
184
}
185
items = append(items, item)
186
}
187
return items
188
}
189
190