guide

This commit is contained in:
微凉 2021-10-29 14:50:26 +08:00
parent f4969560d4
commit 57ad66b43a
7 changed files with 73 additions and 30 deletions

View File

View File

@ -16,6 +16,7 @@ import (
var aliClient = resty.New() var aliClient = resty.New()
func init() { func init() {
RegisterDriver("AliDrive", &AliDrive{})
aliClient. aliClient.
SetRetryCount(3). SetRetryCount(3).
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"). 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").
@ -23,7 +24,23 @@ func init() {
SetHeader("origin", "https://aliyundrive.com") SetHeader("origin", "https://aliyundrive.com")
} }
type AliDrive struct { type AliDrive struct {}
func (a AliDrive) Items() []Item {
return []Item{
{
Name: "refresh_token",
Label: "refresh token",
Type: "string",
Required: true,
},
{
Name: "root_folder",
Label: "root folder file_id",
Type: "string",
Required: false,
},
}
} }
func (a AliDrive) Proxy(ctx *fiber.Ctx) { func (a AliDrive) Proxy(ctx *fiber.Ctx) {
@ -184,7 +201,7 @@ func (a AliDrive) Link(path string, account *model.Account) (string, error) {
return "", err return "", err
} }
if e.Code != "" { if e.Code != "" {
return "", fmt.Errorf("%s",e.Message) return "", fmt.Errorf("%s", e.Message)
} }
return resp["url"].(string), nil return resp["url"].(string), nil
} else { } else {
@ -224,6 +241,9 @@ func (a AliDrive) Save(account *model.Account, old *model.Account) error {
if old != nil { if old != nil {
conf.Cron.Remove(cron.EntryID(old.CronId)) conf.Cron.Remove(cron.EntryID(old.CronId))
} }
if account.RootFolder == "" {
account.RootFolder = "root"
}
refresh, access, err := AliRefreshToken(account.RefreshToken) refresh, access, err := AliRefreshToken(account.RefreshToken)
if err != nil { if err != nil {
return err return err
@ -260,7 +280,3 @@ func (a AliDrive) Save(account *model.Account, old *model.Account) error {
} }
var _ Driver = (*AliDrive)(nil) var _ Driver = (*AliDrive)(nil)
func init() {
RegisterDriver("AliDrive", &AliDrive{})
}

View File

@ -7,10 +7,24 @@ import (
) )
type Driver interface { type Driver interface {
Items() []Item
Path(path string, account *model.Account) (*model.File, []*model.File, error) Path(path string, account *model.Account) (*model.File, []*model.File, error)
Link(path string, account *model.Account) (string,error) Link(path string, account *model.Account) (string, error)
Save(account *model.Account, old *model.Account) error Save(account *model.Account, old *model.Account) error
Proxy(ctx *fiber.Ctx) Proxy(ctx *fiber.Ctx)
// TODO
//MakeDir(path string, account *model.Account) error
//Move(src string, des string, account *model.Account) error
//Delete(path string) error
//Upload(file *fs.File, path string, account *model.Account) error
}
type Item struct {
Name string `json:"name"`
Label string `json:"label"`
Type string `json:"type"`
Required bool `json:"required"`
Description string `json:"description"`
} }
var driversMap = map[string]Driver{} var driversMap = map[string]Driver{}
@ -24,12 +38,12 @@ func GetDriver(name string) (driver Driver, ok bool) {
return return
} }
func GetDriverNames() []string { func GetDrivers() map[string][]Item {
names := make([]string, 0) res := make(map[string][]Item, 0)
for k, _ := range driversMap { for k, v := range driversMap {
names = append(names, k) res[k] = v.Items()
} }
return names return res
} }
type Json map[string]interface{} type Json map[string]interface{}
@ -37,4 +51,4 @@ type Json map[string]interface{}
func JsonStr(j Json) string { func JsonStr(j Json) string {
data, _ := json.Marshal(j) data, _ := json.Marshal(j)
return string(data) return string(data)
} }

View File

@ -14,33 +14,46 @@ import (
) )
type Native struct { type Native struct {
}
func (n Native) Items() []Item {
return []Item{
{
Name: "root_folder",
Label: "root folder path",
Type: "string",
Required: true,
},
}
} }
func (n Native) Proxy(ctx *fiber.Ctx) { func (n Native) Proxy(ctx *fiber.Ctx) {
panic("implement me") // unnecessary
} }
func (n Native) Save(account *model.Account, old *model.Account) error { func (n Native) Save(account *model.Account, old *model.Account) error {
log.Debugf("save a account: [%s]",account.Name) log.Debugf("save a account: [%s]", account.Name)
if !utils.Exists(account.RootFolder) {
return fmt.Errorf("[%s] not exist", account.RootFolder)
}
return nil return nil
} }
// TODO sort files // TODO sort files
func (n Native) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func (n Native) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
fullPath := filepath.Join(account.RootFolder,path) fullPath := filepath.Join(account.RootFolder, path)
log.Debugf("%s-%s-%s",account.RootFolder,path,fullPath) log.Debugf("%s-%s-%s", account.RootFolder, path, fullPath)
if !utils.Exists(fullPath) { if !utils.Exists(fullPath) {
return nil,nil,fmt.Errorf("path not found") return nil, nil, fmt.Errorf("path not found")
} }
if utils.IsDir(fullPath) { if utils.IsDir(fullPath) {
result := make([]*model.File,0) result := make([]*model.File, 0)
files, err := ioutil.ReadDir(fullPath) files, err := ioutil.ReadDir(fullPath)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
for _,f := range files { for _, f := range files {
if strings.HasPrefix(f.Name(),".") { if strings.HasPrefix(f.Name(), ".") {
continue continue
} }
time := f.ModTime() time := f.ModTime()
@ -52,14 +65,14 @@ func (n Native) Path(path string, account *model.Account) (*model.File, []*model
} }
if f.IsDir() { if f.IsDir() {
file.Type = conf.FOLDER file.Type = conf.FOLDER
}else { } else {
file.Type = utils.GetFileType(filepath.Ext(f.Name())) file.Type = utils.GetFileType(filepath.Ext(f.Name()))
} }
result = append(result, file) result = append(result, file)
} }
return nil, result, nil return nil, result, nil
} }
f,err := os.Stat(fullPath) f, err := os.Stat(fullPath)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -73,8 +86,8 @@ func (n Native) Path(path string, account *model.Account) (*model.File, []*model
return file, nil, nil return file, nil, nil
} }
func (n Native) Link(path string, account *model.Account) (string,error) { func (n Native) Link(path string, account *model.Account) (string, error) {
fullPath := filepath.Join(account.RootFolder,path) fullPath := filepath.Join(account.RootFolder, path)
s, err := os.Stat(fullPath) s, err := os.Stat(fullPath)
if err != nil { if err != nil {
return "", err return "", err
@ -82,11 +95,11 @@ func (n Native) Link(path string, account *model.Account) (string,error) {
if s.IsDir() { if s.IsDir() {
return "", fmt.Errorf("can't down folder") return "", fmt.Errorf("can't down folder")
} }
return fullPath,nil return fullPath, nil
} }
var _ Driver = (*Native)(nil) var _ Driver = (*Native)(nil)
func init() { func init() {
RegisterDriver("Native", &Native{}) RegisterDriver("Native", &Native{})
} }

View File

@ -24,7 +24,7 @@ func Down(ctx *fiber.Ctx) error {
if err != nil { if err != nil {
return ErrorResp(ctx, err, 500) return ErrorResp(ctx, err, 500)
} }
if account.Type == "native" { if account.Type == "Native" {
return ctx.SendFile(link) return ctx.SendFile(link)
} else { } else {
return ctx.Redirect(link, 302) return ctx.Redirect(link, 302)
@ -49,7 +49,7 @@ func Proxy(ctx *fiber.Ctx) error {
if err != nil { if err != nil {
return ErrorResp(ctx, err, 500) return ErrorResp(ctx, err, 500)
} }
if account.Type == "native" { if account.Type == "Native" {
return ctx.SendFile(link) return ctx.SendFile(link)
} else { } else {
driver.Proxy(ctx) driver.Proxy(ctx)

View File

@ -6,5 +6,5 @@ import (
) )
func GetDrivers(ctx *fiber.Ctx) error { func GetDrivers(ctx *fiber.Ctx) error {
return SuccessResp(ctx, drivers.GetDriverNames()) return SuccessResp(ctx, drivers.GetDrivers())
} }