From 215aabf2dd59fa80bd5d1c4e868a8305d1a6e609 Mon Sep 17 00:00:00 2001 From: anobodys Date: Wed, 9 Apr 2025 21:46:49 +0800 Subject: [PATCH] fix(doubao): fix file list cursor --- drivers/doubao/driver.go | 2 +- drivers/doubao/types.go | 50 ++++++++++++++++++++++++++++++++ drivers/doubao/util.go | 62 +++++++++++++++++++++++++++++----------- 3 files changed, 97 insertions(+), 17 deletions(-) diff --git a/drivers/doubao/driver.go b/drivers/doubao/driver.go index 38c4a44f..ef1db7f7 100644 --- a/drivers/doubao/driver.go +++ b/drivers/doubao/driver.go @@ -67,7 +67,7 @@ func (d *Doubao) Drop(ctx context.Context) error { func (d *Doubao) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { var files []model.Obj - fileList, err := d.getFiles(dir.GetID()) + fileList, err := d.getFiles(dir.GetID(), "") if err != nil { return nil, err } diff --git a/drivers/doubao/types.go b/drivers/doubao/types.go index 106e17b0..4264eb7d 100644 --- a/drivers/doubao/types.go +++ b/drivers/doubao/types.go @@ -1,6 +1,8 @@ package doubao import ( + "encoding/json" + "fmt" "github.com/alist-org/alist/v3/internal/model" "time" ) @@ -351,3 +353,51 @@ type VideoCommitUploadResp struct { Results []VideoCommitUpload `json:"Results"` } `json:"Result"` } + +type CommonResp struct { + Code int `json:"code"` + Msg string `json:"msg,omitempty"` + Message string `json:"message,omitempty"` // 错误情况下的消息 + Data json.RawMessage `json:"data,omitempty"` // 原始数据,稍后解析 + Error *struct { + Code int `json:"code"` + Message string `json:"message"` + Locale string `json:"locale"` + } `json:"error,omitempty"` +} + +// IsSuccess 判断响应是否成功 +func (r *CommonResp) IsSuccess() bool { + return r.Code == 0 +} + +// GetError 获取错误信息 +func (r *CommonResp) GetError() error { + if r.IsSuccess() { + return nil + } + // 优先使用message字段 + errMsg := r.Message + if errMsg == "" { + errMsg = r.Msg + } + // 如果error对象存在且有详细消息,则使用error中的信息 + if r.Error != nil && r.Error.Message != "" { + errMsg = r.Error.Message + } + + return fmt.Errorf("[doubao] API error (code: %d): %s", r.Code, errMsg) +} + +// UnmarshalData 将data字段解析为指定类型 +func (r *CommonResp) UnmarshalData(v interface{}) error { + if !r.IsSuccess() { + return r.GetError() + } + + if len(r.Data) == 0 { + return nil + } + + return json.Unmarshal(r.Data, v) +} diff --git a/drivers/doubao/util.go b/drivers/doubao/util.go index e188861a..348c0aa0 100644 --- a/drivers/doubao/util.go +++ b/drivers/doubao/util.go @@ -73,40 +73,70 @@ func (d *Doubao) request(path string, method string, callback base.ReqCallback, if callback != nil { callback(req) } - var r BaseResp - req.SetResult(&r) + + var commonResp CommonResp + res, err := req.Execute(method, reqUrl) log.Debugln(res.String()) if err != nil { return nil, err } - // 业务状态码检查(优先于HTTP状态码) - if r.Code != 0 { - return res.Body(), errors.New(r.Msg) + body := res.Body() + // 先解析为通用响应 + if err = json.Unmarshal(body, &commonResp); err != nil { + return nil, err } + // 检查响应是否成功 + if !commonResp.IsSuccess() { + return body, commonResp.GetError() + } + if resp != nil { - err = utils.Json.Unmarshal(res.Body(), resp) - if err != nil { - return nil, err + if err = json.Unmarshal(body, resp); err != nil { + return body, err } } - return res.Body(), nil + + return body, nil } -func (d *Doubao) getFiles(dirId string) ([]File, error) { +func (d *Doubao) getFiles(dirId, cursor string) (resp []File, err error) { var r NodeInfoResp - _, err := d.request("/samantha/aispace/node_info", http.MethodPost, func(req *resty.Request) { - req.SetBody(base.Json{ - "node_id": dirId, - "need_full_path": false, - }) + + var body = base.Json{ + "node_id": dirId, + } + // 如果有游标,则设置游标和大小 + if cursor != "" { + body["cursor"] = cursor + body["size"] = 50 + } else { + body["need_full_path"] = false + } + + _, err = d.request("/samantha/aispace/node_info", http.MethodPost, func(req *resty.Request) { + req.SetBody(body) }, &r) if err != nil { return nil, err } - return r.Data.Children, nil + if r.Data.Children != nil { + resp = r.Data.Children + } + + if r.Data.NextCursor != "-1" { + // 递归获取下一页 + nextFiles, err := d.getFiles(dirId, r.Data.NextCursor) + if err != nil { + return nil, err + } + + resp = append(r.Data.Children, nextFiles...) + } + + return resp, err } func (d *Doubao) getUserInfo() (UserInfo, error) {