alist/internal/operations/driver.go

125 lines
2.9 KiB
Go
Raw Normal View History

2022-06-09 09:11:46 +00:00
package operations
2022-06-07 10:13:55 +00:00
import (
"reflect"
"strings"
2022-06-09 09:11:46 +00:00
"github.com/alist-org/alist/v3/internal/driver"
"github.com/pkg/errors"
2022-06-07 10:13:55 +00:00
log "github.com/sirupsen/logrus"
)
2022-06-09 09:11:46 +00:00
type New func() driver.Driver
2022-06-07 10:13:55 +00:00
2022-06-09 09:11:46 +00:00
var driverNewMap = map[string]New{}
var driverItemsMap = map[string]driver.Items{}
2022-06-07 10:13:55 +00:00
2022-06-09 09:11:46 +00:00
func RegisterDriver(config driver.Config, driver New) {
2022-06-07 14:02:41 +00:00
log.Infof("register driver: [%s]", config.Name)
2022-06-08 08:20:58 +00:00
registerDriverItems(config, driver().GetAddition())
2022-06-09 09:11:46 +00:00
driverNewMap[config.Name] = driver
2022-06-07 10:13:55 +00:00
}
2022-06-08 08:20:58 +00:00
2022-06-09 09:11:46 +00:00
func GetDriverNew(name string) (New, error) {
n, ok := driverNewMap[name]
if !ok {
return nil, errors.Errorf("no driver named: %s", name)
}
return n, nil
}
2022-06-14 11:44:25 +00:00
func GetDriverItemsMap() map[string]driver.Items {
return driverItemsMap
}
2022-06-09 09:11:46 +00:00
func registerDriverItems(config driver.Config, addition driver.Additional) {
2022-06-26 12:18:12 +00:00
log.Debugf("addition of %s: %+v", config.Name, addition)
2022-06-08 08:20:58 +00:00
tAddition := reflect.TypeOf(addition)
mainItems := getMainItems(config)
additionalItems := getAdditionalItems(tAddition)
driverItemsMap[config.Name] = driver.Items{
Main: mainItems,
Additional: additionalItems,
}
2022-06-08 08:20:58 +00:00
}
2022-06-09 09:11:46 +00:00
func getMainItems(config driver.Config) []driver.Item {
items := []driver.Item{{
2022-06-08 08:20:58 +00:00
Name: "virtual_path",
2022-06-09 09:11:46 +00:00
Type: driver.TypeString,
2022-06-08 08:20:58 +00:00
Required: true,
Help: "",
}, {
Name: "index",
2022-06-09 09:11:46 +00:00
Type: driver.TypeNumber,
2022-06-08 08:20:58 +00:00
Help: "use to sort",
}, {
Name: "down_proxy_url",
2022-06-09 09:11:46 +00:00
Type: driver.TypeText,
2022-06-08 08:20:58 +00:00
}, {
Name: "webdav_direct",
2022-06-09 09:11:46 +00:00
Type: driver.TypeBool,
2022-06-08 08:20:58 +00:00
Help: "Transfer the WebDAV of this account through the native without redirect",
}}
if !config.OnlyProxy && !config.OnlyLocal {
2022-06-09 09:11:46 +00:00
items = append(items, []driver.Item{{
2022-06-08 08:20:58 +00:00
Name: "web_proxy",
2022-06-09 09:11:46 +00:00
Type: driver.TypeBool,
2022-06-08 08:20:58 +00:00
}, {
Name: "webdav_proxy",
2022-06-09 09:11:46 +00:00
Type: driver.TypeBool,
2022-06-08 08:20:58 +00:00
},
}...)
}
if config.LocalSort {
2022-06-09 09:11:46 +00:00
items = append(items, []driver.Item{{
2022-06-08 08:20:58 +00:00
Name: "order_by",
2022-06-09 09:11:46 +00:00
Type: driver.TypeSelect,
2022-06-08 08:42:06 +00:00
Values: "name,size,modified",
2022-06-08 08:20:58 +00:00
}, {
Name: "order_direction",
2022-06-09 09:11:46 +00:00
Type: driver.TypeSelect,
2022-06-08 08:20:58 +00:00
Values: "ASC,DESC",
}}...)
}
2022-06-09 09:11:46 +00:00
items = append(items, driver.Item{
2022-06-08 08:20:58 +00:00
Name: "extract_folder",
2022-06-09 09:11:46 +00:00
Type: driver.TypeSelect,
2022-06-08 08:20:58 +00:00
Values: "front,back",
})
return items
}
2022-06-09 09:11:46 +00:00
func getAdditionalItems(t reflect.Type) []driver.Item {
var items []driver.Item
2022-06-08 08:20:58 +00:00
for i := 0; i < t.NumField(); i++ {
2022-06-08 08:32:20 +00:00
field := t.Field(i)
2022-06-26 12:18:12 +00:00
if field.Type.Kind() == reflect.Struct {
items = append(items, getAdditionalItems(field.Type)...)
continue
}
2022-06-08 08:32:20 +00:00
tag := field.Tag
2022-06-08 08:20:58 +00:00
ignore, ok := tag.Lookup("ignore")
2022-06-26 12:18:12 +00:00
if ok && ignore == "true" {
2022-06-08 08:20:58 +00:00
continue
}
2022-06-09 09:11:46 +00:00
item := driver.Item{
2022-06-08 08:20:58 +00:00
Name: tag.Get("json"),
2022-06-08 09:01:36 +00:00
Type: strings.ToLower(field.Type.Name()),
2022-06-08 08:20:58 +00:00
Default: tag.Get("default"),
Values: tag.Get("values"),
Required: tag.Get("required") == "true",
Help: tag.Get("help"),
}
2022-06-08 08:32:20 +00:00
if tag.Get("type") != "" {
item.Type = tag.Get("type")
}
2022-06-08 08:20:58 +00:00
// set default type to string
if item.Type == "" {
item.Type = "string"
}
items = append(items, item)
}
return items
}