alist/drivers/base/driver.go

181 lines
4.6 KiB
Go
Raw Normal View History

2021-12-06 07:55:05 +00:00
package base
2021-10-26 14:28:37 +00:00
2021-10-27 14:45:36 +00:00
import (
"github.com/Xhofe/alist/model"
2021-11-15 12:15:25 +00:00
"github.com/go-resty/resty/v2"
2021-12-06 07:55:05 +00:00
log "github.com/sirupsen/logrus"
2021-11-15 12:15:25 +00:00
"net/http"
"time"
2021-10-27 14:45:36 +00:00
)
2021-10-26 14:28:37 +00:00
2021-11-29 08:42:46 +00:00
type DriverConfig struct {
Name string
2021-12-30 13:39:17 +00:00
OnlyProxy bool // 必须使用代理(本机或者其他机器)
OnlyLocal bool // 必须本机返回的
ApiProxy bool // 使用API中转的
NoNeedSetLink bool // 不需要设置链接的
NoCors bool // 不可以跨域
LocalSort bool // 本地排序
2021-12-19 12:00:53 +00:00
}
type Args struct {
Path string
IP string
2021-11-29 08:42:46 +00:00
}
2021-10-26 14:28:37 +00:00
type Driver interface {
2021-12-19 12:00:53 +00:00
// Config 配置
2021-11-29 08:42:46 +00:00
Config() DriverConfig
2021-12-19 12:00:53 +00:00
// Items 账号所需参数
2021-10-29 06:50:26 +00:00
Items() []Item
2021-12-19 12:00:53 +00:00
// Save 保存时处理
Save(account *model.Account, old *model.Account) error
2021-12-19 12:00:53 +00:00
// File 取文件
2021-11-27 11:40:36 +00:00
File(path string, account *model.Account) (*model.File, error)
2021-12-19 12:00:53 +00:00
// Files 取文件夹
2021-11-27 11:40:36 +00:00
Files(path string, account *model.Account) ([]model.File, error)
2021-12-19 12:00:53 +00:00
// Link 取链接
Link(args Args, account *model.Account) (*Link, error)
// Path 取路径(文件或文件夹)
2021-11-27 11:40:36 +00:00
Path(path string, account *model.Account) (*model.File, []model.File, error)
2022-02-02 10:03:54 +00:00
// Deprecated Proxy 代理处理
//Proxy(r *http.Request, account *model.Account)
2021-12-19 12:00:53 +00:00
// Preview 预览
2021-11-02 15:53:05 +00:00
Preview(path string, account *model.Account) (interface{}, error)
2021-12-19 12:00:53 +00:00
// MakeDir 创建文件夹
2021-12-05 07:22:19 +00:00
MakeDir(path string, account *model.Account) error
2021-12-19 12:00:53 +00:00
// Move 移动/改名
2021-12-05 07:22:19 +00:00
Move(src string, dst string, account *model.Account) error
2022-01-03 12:06:36 +00:00
// Rename 改名
Rename(src string, dst string, account *model.Account) error
2021-12-19 12:00:53 +00:00
// Copy 拷贝
2021-12-05 07:22:19 +00:00
Copy(src string, dst string, account *model.Account) error
2021-12-19 12:00:53 +00:00
// Delete 删除
2021-12-05 07:22:19 +00:00
Delete(path string, account *model.Account) error
2021-12-19 12:00:53 +00:00
// Upload 上传
2021-12-05 07:22:19 +00:00
Upload(file *model.FileStream, account *model.Account) error
2021-12-19 12:00:53 +00:00
// TODO
//Search(path string, keyword string, account *model.Account) ([]*model.File, error)
2021-10-29 06:50:26 +00:00
}
type Item struct {
Name string `json:"name"`
Label string `json:"label"`
Type string `json:"type"`
2022-02-01 06:28:21 +00:00
Default string `json:"default"`
2021-11-02 15:53:05 +00:00
Values string `json:"values"`
2021-10-29 06:50:26 +00:00
Required bool `json:"required"`
Description string `json:"description"`
2021-10-26 14:28:37 +00:00
}
var driversMap = map[string]Driver{}
2021-11-30 01:37:51 +00:00
func RegisterDriver(driver Driver) {
2021-12-06 07:55:05 +00:00
log.Infof("register driver: [%s]", driver.Config().Name)
2021-11-30 01:37:51 +00:00
driversMap[driver.Config().Name] = driver
2021-10-26 14:28:37 +00:00
}
func GetDriver(name string) (driver Driver, ok bool) {
driver, ok = driversMap[name]
return
}
2021-12-31 06:05:35 +00:00
func GetDriversMap() map[string]Driver {
return driversMap
2021-12-25 08:44:32 +00:00
}
2021-10-29 06:50:26 +00:00
func GetDrivers() map[string][]Item {
2022-01-29 16:55:12 +00:00
res := make(map[string][]Item)
2021-10-29 06:50:26 +00:00
for k, v := range driversMap {
2022-02-17 09:49:03 +00:00
webdavDirect := Item{
Name: "webdav_direct",
Label: "webdav direct",
Type: TypeBool,
Required: true,
Description: "Transfer the WebDAV of this account through the native",
}
2021-11-29 08:42:46 +00:00
if v.Config().OnlyProxy {
2022-02-17 09:49:03 +00:00
res[k] = append([]Item{
webdavDirect,
}, v.Items()...)
2021-11-29 08:42:46 +00:00
} else {
res[k] = append([]Item{
{
Name: "proxy",
Label: "proxy",
Type: TypeBool,
2021-11-29 08:42:46 +00:00
Required: true,
2021-12-08 12:00:52 +00:00
Description: "web proxy",
2021-11-29 08:42:46 +00:00
},
{
Name: "webdav_proxy",
Label: "webdav proxy",
Type: TypeBool,
2021-11-29 08:42:46 +00:00
Required: true,
Description: "Transfer the WebDAV of this account through the server",
},
2022-02-17 09:49:03 +00:00
webdavDirect,
2021-11-29 08:42:46 +00:00
}, v.Items()...)
}
2021-12-19 09:10:20 +00:00
res[k] = append([]Item{
{
Name: "down_proxy_url",
Label: "down_proxy_url",
Type: TypeString,
},
2022-01-16 08:38:41 +00:00
{
Name: "extract_folder",
Label: "extract_folder",
Values: "front,back",
Type: TypeSelect,
},
2021-12-19 12:00:53 +00:00
}, res[k]...)
if v.Config().ApiProxy {
2021-12-19 17:00:53 +00:00
res[k] = append([]Item{
{
Name: "api_proxy_url",
Label: "api_proxy_url",
Type: TypeString,
},
}, res[k]...)
2021-12-19 12:00:53 +00:00
}
if v.Config().LocalSort {
res[k] = append(res[k], []Item{
{
Name: "order_by",
Label: "order_by",
Type: TypeSelect,
Values: "name,size,updated_at",
Required: false,
},
{
Name: "order_direction",
Label: "order_direction",
Type: TypeSelect,
Values: "ASC,DESC",
Required: false,
},
}...)
}
2021-10-26 14:28:37 +00:00
}
2021-10-29 06:50:26 +00:00
return res
2021-10-26 14:28:37 +00:00
}
2021-10-27 14:45:36 +00:00
2021-11-27 11:40:36 +00:00
var NoRedirectClient *resty.Client
2021-12-07 12:16:34 +00:00
var RestyClient = resty.New()
var HttpClient = &http.Client{}
var UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
var DefaultTimeout = time.Second * 20
2021-11-15 12:15:25 +00:00
func init() {
2021-11-27 11:40:36 +00:00
NoRedirectClient = resty.New().SetRedirectPolicy(
2021-11-15 12:15:25 +00:00
resty.RedirectPolicyFunc(func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}),
)
NoRedirectClient.SetHeader("user-agent", UserAgent)
RestyClient.SetHeader("user-agent", UserAgent)
2021-12-19 09:54:28 +00:00
RestyClient.SetRetryCount(3)
RestyClient.SetTimeout(DefaultTimeout)
2021-11-15 12:15:25 +00:00
}