alist/internal/op/driver.go

162 lines
3.8 KiB
Go
Raw Normal View History

package op
2022-06-07 10:13:55 +00:00
import (
"reflect"
"strings"
2022-08-07 05:23:15 +00:00
"github.com/alist-org/alist/v3/internal/conf"
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
)
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{}
2022-08-30 06:39:10 +00:00
var driverInfoMap = map[string]driver.Info{}
2022-06-07 10:13:55 +00:00
2022-06-09 09:11:46 +00:00
func RegisterDriver(config driver.Config, driver New) {
2022-08-07 05:23:15 +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-26 12:25:02 +00:00
func GetDriverNames() []string {
var driverNames []string
2022-08-30 06:39:10 +00:00
for k := range driverInfoMap {
2022-06-26 12:25:02 +00:00
driverNames = append(driverNames, k)
}
return driverNames
}
2022-08-30 06:39:10 +00:00
func GetDriverInfoMap() map[string]driver.Info {
return driverInfoMap
2022-06-14 11:44:25 +00:00
}
2022-06-09 09:11:46 +00:00
func registerDriverItems(config driver.Config, addition driver.Additional) {
2022-08-07 05:23:15 +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, config.DefaultRoot)
2022-08-30 06:39:10 +00:00
driverInfoMap[config.Name] = driver.Info{
2022-07-27 03:43:49 +00:00
Common: mainItems,
Additional: additionalItems,
2022-08-30 06:39:10 +00:00
Config: config,
}
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-07-12 10:41:16 +00:00
Name: "mount_path",
2022-06-27 06:32:21 +00:00
Type: conf.TypeString,
2022-06-08 08:20:58 +00:00
Required: true,
Help: "",
}, {
Name: "order",
2022-06-27 06:32:21 +00:00
Type: conf.TypeNumber,
2022-06-08 08:20:58 +00:00
Help: "use to sort",
2022-07-12 10:41:16 +00:00
}, {
Name: "remark",
Type: conf.TypeText,
2022-06-08 08:20:58 +00:00
}}
if !config.NoCache {
items = append(items, driver.Item{
Name: "cache_expiration",
Type: conf.TypeNumber,
Default: "30",
Required: true,
Help: "The cache expiration time for this storage",
})
}
2022-06-08 08:20:58 +00:00
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-27 06:32:21 +00:00
Type: conf.TypeBool,
2022-06-08 08:20:58 +00:00
}, {
2022-06-27 05:58:21 +00:00
Name: "webdav_policy",
2022-06-27 06:32:21 +00:00
Type: conf.TypeSelect,
2022-09-04 06:48:21 +00:00
Options: "302_redirect,use_proxy_url,native_proxy",
2022-07-23 13:19:27 +00:00
Default: "302_redirect",
2022-06-27 05:58:21 +00:00
Required: true,
2022-06-08 08:20:58 +00:00
},
}...)
2022-06-27 05:58:21 +00:00
} else {
items = append(items, driver.Item{
Name: "webdav_policy",
2022-06-27 06:32:21 +00:00
Type: conf.TypeSelect,
2022-07-23 13:19:27 +00:00
Default: "native_proxy",
2022-09-04 06:48:21 +00:00
Options: "use_proxy_url,native_proxy",
2022-06-27 05:58:21 +00:00
Required: true,
})
2022-06-08 08:20:58 +00:00
}
items = append(items, driver.Item{
Name: "down_proxy_url",
Type: conf.TypeText,
})
2022-06-08 08:20:58 +00:00
if config.LocalSort {
2022-06-09 09:11:46 +00:00
items = append(items, []driver.Item{{
Name: "order_by",
Type: conf.TypeSelect,
Options: "name,size,modified",
2022-06-08 08:20:58 +00:00
}, {
Name: "order_direction",
Type: conf.TypeSelect,
Options: "asc,desc",
2022-06-08 08:20:58 +00:00
}}...)
}
2022-06-09 09:11:46 +00:00
items = append(items, driver.Item{
Name: "extract_folder",
Type: conf.TypeSelect,
Options: "front,back",
2022-06-08 08:20:58 +00:00
})
return items
}
func getAdditionalItems(t reflect.Type, defaultRoot string) []driver.Item {
2022-06-09 09:11:46 +00:00
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, defaultRoot)...)
2022-06-26 12:18:12 +00:00
continue
}
2022-06-08 08:32:20 +00:00
tag := field.Tag
2022-08-30 13:52:06 +00:00
ignore, ok1 := tag.Lookup("ignore")
name, ok2 := tag.Lookup("json")
if (ok1 && ignore == "true") || !ok2 {
2022-06-08 08:20:58 +00:00
continue
}
2022-06-09 09:11:46 +00:00
item := driver.Item{
2022-08-30 13:52:06 +00:00
Name: name,
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"),
Options: tag.Get("options"),
2022-06-08 08:20:58 +00:00
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")
}
if item.Name == "root_folder_id" || item.Name == "root_folder_path" {
if item.Default == "" {
item.Default = defaultRoot
}
item.Required = item.Default != ""
}
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
}