From 5500980d632719293f87142d15d3bb15a3893b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Tue, 23 Nov 2021 15:39:09 +0800 Subject: [PATCH] :bug: fix onedrive get files --- drivers/onedrive.go | 51 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/drivers/onedrive.go b/drivers/onedrive.go index 9f8c708e..c7818bbd 100644 --- a/drivers/onedrive.go +++ b/drivers/onedrive.go @@ -135,6 +135,20 @@ func (o Onedrive) Items() []Item { Type: "string", Required: false, }, + { + Name: "order_by", + Label: "order_by", + Type: "select", + Values: "name,size,lastModifiedDateTime", + Required: false, + }, + { + Name: "order_direction", + Label: "order_direction", + Type: "select", + Values: "asc,desc", + Required: false, + }, } } @@ -161,7 +175,7 @@ func (o Onedrive) RefreshToken(account *model.Account) error { if e.Error != "" { account.Status = e.ErrorDescription return fmt.Errorf("%s", e.ErrorDescription) - }else { + } else { account.Status = "work" } account.RefreshToken, account.AccessToken = resp.RefreshToken, resp.AccessToken @@ -179,7 +193,8 @@ type OneFile struct { } type OneFiles struct { - Value []OneFile `json:"value"` + Value []OneFile `json:"value"` + NextLink string `json:"@odata.nextLink"` } type OneRespErr struct { @@ -206,18 +221,30 @@ func (o Onedrive) FormatFile(file *OneFile) *model.File { } func (o Onedrive) GetFiles(account *model.Account, path string) ([]OneFile, error) { - var files OneFiles - var e OneRespErr - _, err := oneClient.R().SetResult(&files).SetError(&e). - SetHeader("Authorization", "Bearer "+account.AccessToken). - Get(o.GetMetaUrl(account, false, path) + "/children") - if err != nil { - return nil, err + var res []OneFile + nextLink := o.GetMetaUrl(account, false, path) + "/children" + if account.OrderBy != "" { + nextLink += fmt.Sprintf("?orderby=%s", account.OrderBy) + if account.OrderDirection != "" { + nextLink += fmt.Sprintf(" %s", account.OrderDirection) + } } - if e.Error.Code != "" { - return nil, fmt.Errorf("%s", e.Error.Message) + for nextLink != "" { + var files OneFiles + var e OneRespErr + _, err := oneClient.R().SetResult(&files).SetError(&e). + SetHeader("Authorization", "Bearer "+account.AccessToken). + Get(nextLink) + if err != nil { + return nil, err + } + if e.Error.Code != "" { + return nil, fmt.Errorf("%s", e.Error.Message) + } + res = append(res, files.Value...) + nextLink = files.NextLink } - return files.Value, nil + return res, nil } func (o Onedrive) GetFile(account *model.Account, path string) (*OneFile, error) {