diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 73d25f012c2..48f529b30f8 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -37,8 +37,8 @@ func (d *Driver) Drop(ctx context.Context) error { return nil } -func (d *Driver) GetAccount() model.Account { - return d.Account +func (d *Driver) GetAddition() driver.Additional { + return d.Addition } func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) { diff --git a/drivers/local/meta.go b/drivers/local/meta.go index 59533496bdf..d3346417cec 100644 --- a/drivers/local/meta.go +++ b/drivers/local/meta.go @@ -3,7 +3,7 @@ package local import "github.com/alist-org/alist/v3/internal/driver" type Addition struct { - RootFolder string `json:"root_folder" type:"string" desc:"root folder path" default:"/"` + RootFolder string `json:"root_folder" type:"string" help:"root folder path" default:"/"` } var config = driver.Config{ diff --git a/internal/driver/addition.go b/internal/driver/addition.go index ba1c64733b4..1356c8901e6 100644 --- a/internal/driver/addition.go +++ b/internal/driver/addition.go @@ -1,7 +1,6 @@ package driver -type Additional interface { -} +type Additional interface{} type Item struct { Name string `json:"name"` @@ -9,5 +8,10 @@ type Item struct { Default string `json:"default"` Values string `json:"values"` Required bool `json:"required"` - Desc string `json:"desc"` + Help string `json:"help"` +} + +type Items struct { + Main []Item `json:"main"` + Additional []Item `json:"additional"` } diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 08e738a0f4b..c77bb8adc44 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -19,6 +19,7 @@ type Meta interface { Drop(ctx context.Context) error // GetAccount transform additional field to string and assign to account's addition GetAccount() model.Account + GetAddition() Additional } type Other interface { diff --git a/internal/driver/manage.go b/internal/driver/manage.go index 20e3500f820..ff7251ad9e0 100644 --- a/internal/driver/manage.go +++ b/internal/driver/manage.go @@ -2,13 +2,95 @@ package driver import ( log "github.com/sirupsen/logrus" + "reflect" ) type New func() Driver var driversMap = map[string]New{} +var driverItemsMap = map[string]Items{} func RegisterDriver(config Config, driver New) { log.Infof("register driver: [%s]", config.Name) + registerDriverItems(config, driver().GetAddition()) driversMap[config.Name] = driver } + +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", + Type: "string", + Required: true, + Help: "", + }, { + Name: "index", + Type: "int", + Help: "use to sort", + }, { + Name: "down_proxy_url", + Type: "text", + }, { + Name: "webdav_direct", + Type: "bool", + Help: "Transfer the WebDAV of this account through the native without redirect", + }} + if !config.OnlyProxy && !config.OnlyLocal { + items = append(items, []Item{{ + Name: "web_proxy", + Type: "bool", + }, { + Name: "webdav_proxy", + Type: "bool", + }, + }...) + } + if config.LocalSort { + items = append(items, []Item{{ + Name: "order_by", + Type: "select", + Values: "name,size,updated_at", + }, { + Name: "order_direction", + Type: "select", + Values: "ASC,DESC", + }}...) + } + items = append(items, Item{ + Name: "extract_folder", + Values: "front,back", + Type: "select", + }) + return items +} + +func getAdditionalItems(t reflect.Type) []Item { + var items []Item + for i := 0; i < t.NumField(); i++ { + tag := t.Field(i).Tag + ignore, ok := tag.Lookup("ignore") + if !ok || ignore == "false" { + continue + } + item := Item{ + Name: tag.Get("json"), + Type: tag.Get("type"), + Default: tag.Get("default"), + Values: tag.Get("values"), + Required: tag.Get("required") == "true", + Help: tag.Get("help"), + } + // set default type to string + if item.Type == "" { + item.Type = "string" + } + items = append(items, item) + } + return items +} diff --git a/internal/model/account.go b/internal/model/account.go index ddc1d793261..c82e4f3f38e 100644 --- a/internal/model/account.go +++ b/internal/model/account.go @@ -24,3 +24,7 @@ type Proxy struct { WebdavDirect bool `json:"webdav_direct"` DownProxyUrl string `json:"down_proxy_url"` } + +func (a Account) GetAccount() Account { + return a +}