diff --git a/main.go b/alist.go similarity index 100% rename from main.go rename to alist.go diff --git a/main_test.go b/alist_test.go similarity index 100% rename from main_test.go rename to alist_test.go diff --git a/drivers/alidrive.go b/drivers/alidrive.go index 0deace6d..9087af27 100644 --- a/drivers/alidrive.go +++ b/drivers/alidrive.go @@ -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{}) -} diff --git a/drivers/driver.go b/drivers/driver.go index 9fae5a61..787f277b 100644 --- a/drivers/driver.go +++ b/drivers/driver.go @@ -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) -} \ No newline at end of file +} diff --git a/drivers/native.go b/drivers/native.go index ccb535a3..89c0c523 100644 --- a/drivers/native.go +++ b/drivers/native.go @@ -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{}) -} \ No newline at end of file +} diff --git a/server/down.go b/server/down.go index 3467a2c1..020a065a 100644 --- a/server/down.go +++ b/server/down.go @@ -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) diff --git a/server/driver.go b/server/driver.go index 9e146492..82ec9e68 100644 --- a/server/driver.go +++ b/server/driver.go @@ -6,5 +6,5 @@ import ( ) func GetDrivers(ctx *fiber.Ctx) error { - return SuccessResp(ctx, drivers.GetDriverNames()) + return SuccessResp(ctx, drivers.GetDrivers()) } \ No newline at end of file