alist/internal/driver/manage.go

102 lines
2.2 KiB
Go
Raw Normal View History

2022-06-07 18:13:55 +08:00
package driver
import (
log "github.com/sirupsen/logrus"
2022-06-08 16:20:58 +08:00
"reflect"
2022-06-08 16:32:20 +08:00
"strings"
2022-06-07 18:13:55 +08:00
)
type New func() Driver
var driversMap = map[string]New{}
2022-06-08 16:20:58 +08:00
var driverItemsMap = map[string]Items{}
2022-06-07 18:13:55 +08:00
2022-06-07 22:02:41 +08:00
func RegisterDriver(config Config, driver New) {
log.Infof("register driver: [%s]", config.Name)
2022-06-08 16:20:58 +08:00
registerDriverItems(config, driver().GetAddition())
2022-06-07 22:02:41 +08:00
driversMap[config.Name] = driver
2022-06-07 18:13:55 +08:00
}
2022-06-08 16:20:58 +08:00
func registerDriverItems(config Config, addition Additional) {
tAddition := reflect.TypeOf(addition)
mainItems := getMainItems(config)
additionalItems := getAdditionalItems(tAddition)
driverItemsMap[config.Name] = Items{mainItems, additionalItems}
}
func getMainItems(config Config) []Item {
items := []Item{{
Name: "virtual_path",
2022-06-08 16:42:06 +08:00
Type: TypeString,
2022-06-08 16:20:58 +08:00
Required: true,
Help: "",
}, {
Name: "index",
2022-06-08 16:42:06 +08:00
Type: TypeNumber,
2022-06-08 16:20:58 +08:00
Help: "use to sort",
}, {
Name: "down_proxy_url",
2022-06-08 16:42:06 +08:00
Type: TypeText,
2022-06-08 16:20:58 +08:00
}, {
Name: "webdav_direct",
2022-06-08 16:42:06 +08:00
Type: TypeBool,
2022-06-08 16:20:58 +08:00
Help: "Transfer the WebDAV of this account through the native without redirect",
}}
if !config.OnlyProxy && !config.OnlyLocal {
items = append(items, []Item{{
Name: "web_proxy",
2022-06-08 16:42:06 +08:00
Type: TypeBool,
2022-06-08 16:20:58 +08:00
}, {
Name: "webdav_proxy",
2022-06-08 16:42:06 +08:00
Type: TypeBool,
2022-06-08 16:20:58 +08:00
},
}...)
}
if config.LocalSort {
items = append(items, []Item{{
Name: "order_by",
2022-06-08 16:42:06 +08:00
Type: TypeSelect,
Values: "name,size,modified",
2022-06-08 16:20:58 +08:00
}, {
Name: "order_direction",
2022-06-08 16:42:06 +08:00
Type: TypeSelect,
2022-06-08 16:20:58 +08:00
Values: "ASC,DESC",
}}...)
}
items = append(items, Item{
Name: "extract_folder",
2022-06-08 16:42:06 +08:00
Type: TypeSelect,
2022-06-08 16:20:58 +08:00
Values: "front,back",
})
return items
}
func getAdditionalItems(t reflect.Type) []Item {
var items []Item
for i := 0; i < t.NumField(); i++ {
2022-06-08 16:32:20 +08:00
field := t.Field(i)
tag := field.Tag
2022-06-08 16:20:58 +08:00
ignore, ok := tag.Lookup("ignore")
if !ok || ignore == "false" {
continue
}
item := Item{
Name: tag.Get("json"),
2022-06-08 17:01:36 +08:00
Type: strings.ToLower(field.Type.Name()),
2022-06-08 16:20:58 +08:00
Default: tag.Get("default"),
Values: tag.Get("values"),
Required: tag.Get("required") == "true",
Help: tag.Get("help"),
}
2022-06-08 16:32:20 +08:00
if tag.Get("type") != "" {
item.Type = tag.Get("type")
}
2022-06-08 16:20:58 +08:00
// set default type to string
if item.Type == "" {
item.Type = "string"
}
items = append(items, item)
}
return items
}