alist/drivers/base/driver.go

96 lines
2.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-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-08 02:33:26 +00:00
Name string
OnlyProxy bool
NeedHeader bool
2021-11-29 08:42:46 +00:00
}
2021-10-26 14:28:37 +00:00
type Driver interface {
2021-11-29 08:42:46 +00:00
Config() DriverConfig
2021-10-29 06:50:26 +00:00
Items() []Item
Save(account *model.Account, old *model.Account) error
2021-11-27 11:40:36 +00:00
File(path string, account *model.Account) (*model.File, error)
Files(path string, account *model.Account) ([]model.File, error)
2021-10-29 06:50:26 +00:00
Link(path string, account *model.Account) (string, error)
2021-11-27 11:40:36 +00:00
Path(path string, account *model.Account) (*model.File, []model.File, error)
2021-11-17 08:37:12 +00:00
Proxy(c *gin.Context, account *model.Account)
2021-11-02 15:53:05 +00:00
Preview(path string, account *model.Account) (interface{}, error)
2021-10-29 06:50:26 +00:00
// TODO
2021-11-17 08:25:32 +00:00
//Search(path string, keyword string, account *model.Account) ([]*model.File, error)
2021-12-05 07:22:19 +00:00
MakeDir(path string, account *model.Account) error
Move(src string, dst string, account *model.Account) error
Copy(src string, dst string, account *model.Account) error
Delete(path string, account *model.Account) error
Upload(file *model.FileStream, account *model.Account) 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{
{
Name: "proxy",
Label: "proxy",
Type: TypeBool,
2021-11-29 08:42:46 +00:00
Required: true,
Description: "allow proxy",
},
{
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",
},
}, v.Items()...)
}
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-11-29 08:06:49 +00:00
NoRedirectClient.SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
2021-11-15 12:15:25 +00:00
}