guide

pull/548/head
微凉 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()
func init() {
RegisterDriver("AliDrive", &AliDrive{})
aliClient.
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").
@ -23,7 +24,23 @@ func init() {
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) {
@ -184,7 +201,7 @@ func (a AliDrive) Link(path string, account *model.Account) (string, error) {
return "", err
}
if e.Code != "" {
return "", fmt.Errorf("%s",e.Message)
return "", fmt.Errorf("%s", e.Message)
}
return resp["url"].(string), nil
} else {
@ -224,6 +241,9 @@ func (a AliDrive) Save(account *model.Account, old *model.Account) error {
if old != nil {
conf.Cron.Remove(cron.EntryID(old.CronId))
}
if account.RootFolder == "" {
account.RootFolder = "root"
}
refresh, access, err := AliRefreshToken(account.RefreshToken)
if err != nil {
return err
@ -260,7 +280,3 @@ func (a AliDrive) Save(account *model.Account, old *model.Account) error {
}
var _ Driver = (*AliDrive)(nil)
func init() {
RegisterDriver("AliDrive", &AliDrive{})
}

View File

@ -7,10 +7,24 @@ import (
)
type Driver interface {
Items() []Item
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
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{}
@ -24,12 +38,12 @@ func GetDriver(name string) (driver Driver, ok bool) {
return
}
func GetDriverNames() []string {
names := make([]string, 0)
for k, _ := range driversMap {
names = append(names, k)
func GetDrivers() map[string][]Item {
res := make(map[string][]Item, 0)
for k, v := range driversMap {
res[k] = v.Items()
}
return names
return res
}
type Json map[string]interface{}
@ -37,4 +51,4 @@ type Json map[string]interface{}
func JsonStr(j Json) string {
data, _ := json.Marshal(j)
return string(data)
}
}

View File

@ -14,33 +14,46 @@ import (
)
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) {
panic("implement me")
// unnecessary
}
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
}
// TODO sort files
func (n Native) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
fullPath := filepath.Join(account.RootFolder,path)
log.Debugf("%s-%s-%s",account.RootFolder,path,fullPath)
fullPath := filepath.Join(account.RootFolder, path)
log.Debugf("%s-%s-%s", account.RootFolder, path, 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) {
result := make([]*model.File,0)
result := make([]*model.File, 0)
files, err := ioutil.ReadDir(fullPath)
if err != nil {
return nil, nil, err
}
for _,f := range files {
if strings.HasPrefix(f.Name(),".") {
for _, f := range files {
if strings.HasPrefix(f.Name(), ".") {
continue
}
time := f.ModTime()
@ -52,14 +65,14 @@ func (n Native) Path(path string, account *model.Account) (*model.File, []*model
}
if f.IsDir() {
file.Type = conf.FOLDER
}else {
} else {
file.Type = utils.GetFileType(filepath.Ext(f.Name()))
}
result = append(result, file)
}
return nil, result, nil
}
f,err := os.Stat(fullPath)
f, err := os.Stat(fullPath)
if err != nil {
return nil, nil, err
}
@ -73,8 +86,8 @@ func (n Native) Path(path string, account *model.Account) (*model.File, []*model
return file, nil, nil
}
func (n Native) Link(path string, account *model.Account) (string,error) {
fullPath := filepath.Join(account.RootFolder,path)
func (n Native) Link(path string, account *model.Account) (string, error) {
fullPath := filepath.Join(account.RootFolder, path)
s, err := os.Stat(fullPath)
if err != nil {
return "", err
@ -82,11 +95,11 @@ func (n Native) Link(path string, account *model.Account) (string,error) {
if s.IsDir() {
return "", fmt.Errorf("can't down folder")
}
return fullPath,nil
return fullPath, nil
}
var _ Driver = (*Native)(nil)
func init() {
RegisterDriver("Native", &Native{})
}
}

View File

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

View File

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