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-13 07:53:26 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
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"
|
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 {
|
2021-12-19 12:32:47 +00:00
|
|
|
Name string
|
|
|
|
OnlyProxy bool
|
|
|
|
NoLink bool // 必须本机返回的
|
|
|
|
ApiProxy bool // 使用API中转的
|
|
|
|
NeedSetLink 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 保存时处理
|
2021-11-17 07:21:51 +00:00
|
|
|
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)
|
2021-12-19 12:00:53 +00:00
|
|
|
// Proxy 代理处理
|
2021-11-17 08:37:12 +00:00
|
|
|
Proxy(c *gin.Context, 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
|
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"`
|
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-10-29 06:50:26 +00:00
|
|
|
func GetDrivers() map[string][]Item {
|
|
|
|
res := make(map[string][]Item, 0)
|
|
|
|
for k, v := range driversMap {
|
2021-11-29 08:42:46 +00:00
|
|
|
if v.Config().OnlyProxy {
|
|
|
|
res[k] = v.Items()
|
|
|
|
} else {
|
|
|
|
res[k] = append([]Item{
|
2021-12-08 12:00:52 +00:00
|
|
|
//{
|
|
|
|
// Name: "allow_proxy",
|
|
|
|
// Label: "allow_proxy",
|
|
|
|
// Type: TypeBool,
|
|
|
|
// Required: true,
|
|
|
|
// Description: "allow proxy",
|
|
|
|
//},
|
2021-11-29 08:42:46 +00:00
|
|
|
{
|
|
|
|
Name: "proxy",
|
|
|
|
Label: "proxy",
|
2021-12-06 09:49:20 +00:00
|
|
|
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",
|
2021-12-06 09:49:20 +00:00
|
|
|
Type: TypeBool,
|
2021-11-29 08:42:46 +00:00
|
|
|
Required: true,
|
|
|
|
Description: "Transfer the WebDAV of this account through the server",
|
|
|
|
},
|
|
|
|
}, v.Items()...)
|
|
|
|
}
|
2021-12-19 09:10:20 +00:00
|
|
|
res[k] = append([]Item{
|
|
|
|
{
|
|
|
|
Name: "down_proxy_url",
|
|
|
|
Label: "down_proxy_url",
|
|
|
|
Type: TypeString,
|
|
|
|
},
|
2021-12-19 12:00:53 +00:00
|
|
|
}, res[k]...)
|
|
|
|
if v.Config().ApiProxy {
|
|
|
|
res[k] = append(res[k], Item{
|
2021-12-19 09:10:20 +00:00
|
|
|
Name: "api_proxy_url",
|
|
|
|
Label: "api_proxy_url",
|
|
|
|
Type: TypeString,
|
2021-12-19 12:00:53 +00:00
|
|
|
})
|
|
|
|
}
|
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{}
|
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
|
|
|
|
}),
|
|
|
|
)
|
2021-12-19 09:54:28 +00:00
|
|
|
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
|
|
|
NoRedirectClient.SetHeader("user-agent", userAgent)
|
|
|
|
RestyClient.SetHeader("user-agent", userAgent)
|
|
|
|
RestyClient.SetRetryCount(3)
|
2021-11-15 12:15:25 +00:00
|
|
|
}
|