feat: driver and account operate
This commit is contained in:
93
internal/operations/account.go
Normal file
93
internal/operations/account.go
Normal file
@ -0,0 +1,93 @@
|
||||
package operations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/driver"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/store"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Although the driver type is stored,
|
||||
// there is an account in each driver,
|
||||
// so it should actually be an account, just wrapped by the driver
|
||||
var accountsMap = map[string]driver.Driver{}
|
||||
|
||||
func GetAccountByVirtualPath(virtualPath string) (driver.Driver, error) {
|
||||
accountDriver, ok := accountsMap[virtualPath]
|
||||
if !ok {
|
||||
return nil, errors.Errorf("no virtual path for an account is: %s", virtualPath)
|
||||
}
|
||||
return accountDriver, nil
|
||||
}
|
||||
|
||||
// CreateAccount Save the account to database so account can get an id
|
||||
// then instantiate corresponding driver and save it in memory
|
||||
func CreateAccount(ctx context.Context, account model.Account) error {
|
||||
err := store.CreateAccount(&account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed create account in database")
|
||||
}
|
||||
// already has an id
|
||||
driverName := account.Driver
|
||||
driverNew, err := GetDriverNew(driverName)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get driver new")
|
||||
}
|
||||
accountDriver := driverNew()
|
||||
err = accountDriver.Init(ctx, account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed init account")
|
||||
}
|
||||
accountsMap[account.VirtualPath] = accountDriver
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateAccount update account
|
||||
// get old account first
|
||||
// drop the account then reinitialize
|
||||
func UpdateAccount(ctx context.Context, account model.Account) error {
|
||||
oldAccount, err := store.GetAccountById(account.ID)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get old account")
|
||||
}
|
||||
err = store.UpdateAccount(&account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed update account in database")
|
||||
}
|
||||
if oldAccount.VirtualPath != account.VirtualPath {
|
||||
// virtual path renamed
|
||||
delete(accountsMap, oldAccount.VirtualPath)
|
||||
}
|
||||
accountDriver, err := GetAccountByVirtualPath(oldAccount.VirtualPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get account driver")
|
||||
}
|
||||
err = accountDriver.Drop(ctx)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed drop account")
|
||||
}
|
||||
err = accountDriver.Init(ctx, account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed init account")
|
||||
}
|
||||
accountsMap[account.VirtualPath] = accountDriver
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveDriverAccount call from specific driver
|
||||
func SaveDriverAccount(driver driver.Driver) error {
|
||||
account := driver.GetAccount()
|
||||
addition := driver.GetAddition()
|
||||
bytes, err := utils.Json.Marshal(addition)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error while marshal addition")
|
||||
}
|
||||
account.Addition = string(bytes)
|
||||
err = store.UpdateAccount(&account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed update account in database")
|
||||
}
|
||||
return nil
|
||||
}
|
111
internal/operations/driver.go
Normal file
111
internal/operations/driver.go
Normal file
@ -0,0 +1,111 @@
|
||||
package operations
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/driver"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type New func() driver.Driver
|
||||
|
||||
var driverNewMap = map[string]New{}
|
||||
var driverItemsMap = map[string]driver.Items{}
|
||||
|
||||
func RegisterDriver(config driver.Config, driver New) {
|
||||
log.Infof("register driver: [%s]", config.Name)
|
||||
registerDriverItems(config, driver().GetAddition())
|
||||
driverNewMap[config.Name] = driver
|
||||
}
|
||||
|
||||
func GetDriverNew(name string) (New, error) {
|
||||
n, ok := driverNewMap[name]
|
||||
if !ok {
|
||||
return nil, errors.Errorf("no driver named: %s", name)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func registerDriverItems(config driver.Config, addition driver.Additional) {
|
||||
tAddition := reflect.TypeOf(addition)
|
||||
mainItems := getMainItems(config)
|
||||
additionalItems := getAdditionalItems(tAddition)
|
||||
driverItemsMap[config.Name] = driver.Items{mainItems, additionalItems}
|
||||
}
|
||||
|
||||
func getMainItems(config driver.Config) []driver.Item {
|
||||
items := []driver.Item{{
|
||||
Name: "virtual_path",
|
||||
Type: driver.TypeString,
|
||||
Required: true,
|
||||
Help: "",
|
||||
}, {
|
||||
Name: "index",
|
||||
Type: driver.TypeNumber,
|
||||
Help: "use to sort",
|
||||
}, {
|
||||
Name: "down_proxy_url",
|
||||
Type: driver.TypeText,
|
||||
}, {
|
||||
Name: "webdav_direct",
|
||||
Type: driver.TypeBool,
|
||||
Help: "Transfer the WebDAV of this account through the native without redirect",
|
||||
}}
|
||||
if !config.OnlyProxy && !config.OnlyLocal {
|
||||
items = append(items, []driver.Item{{
|
||||
Name: "web_proxy",
|
||||
Type: driver.TypeBool,
|
||||
}, {
|
||||
Name: "webdav_proxy",
|
||||
Type: driver.TypeBool,
|
||||
},
|
||||
}...)
|
||||
}
|
||||
if config.LocalSort {
|
||||
items = append(items, []driver.Item{{
|
||||
Name: "order_by",
|
||||
Type: driver.TypeSelect,
|
||||
Values: "name,size,modified",
|
||||
}, {
|
||||
Name: "order_direction",
|
||||
Type: driver.TypeSelect,
|
||||
Values: "ASC,DESC",
|
||||
}}...)
|
||||
}
|
||||
items = append(items, driver.Item{
|
||||
Name: "extract_folder",
|
||||
Type: driver.TypeSelect,
|
||||
Values: "front,back",
|
||||
})
|
||||
return items
|
||||
}
|
||||
|
||||
func getAdditionalItems(t reflect.Type) []driver.Item {
|
||||
var items []driver.Item
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
tag := field.Tag
|
||||
ignore, ok := tag.Lookup("ignore")
|
||||
if !ok || ignore == "false" {
|
||||
continue
|
||||
}
|
||||
item := driver.Item{
|
||||
Name: tag.Get("json"),
|
||||
Type: strings.ToLower(field.Type.Name()),
|
||||
Default: tag.Get("default"),
|
||||
Values: tag.Get("values"),
|
||||
Required: tag.Get("required") == "true",
|
||||
Help: tag.Get("help"),
|
||||
}
|
||||
if tag.Get("type") != "" {
|
||||
item.Type = tag.Get("type")
|
||||
}
|
||||
// set default type to string
|
||||
if item.Type == "" {
|
||||
item.Type = "string"
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return items
|
||||
}
|
3
internal/operations/error.go
Normal file
3
internal/operations/error.go
Normal file
@ -0,0 +1,3 @@
|
||||
package operations
|
||||
|
||||
var ()
|
Reference in New Issue
Block a user