Skip to content

Commit e0ee737

Browse files
authored
fix(driver/wps): fetch all files via multiple API invocations (#2139)
fix(driver/wps): wps list all files in one request
1 parent 0673a74 commit e0ee737

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

drivers/wps/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type FileInfo struct {
4141
}
4242

4343
type filesResp struct {
44-
Files []FileInfo `json:"files"`
44+
Files []FileInfo `json:"files"`
45+
NextOffset int `json:"next_offset"`
4546
}
4647

4748
type downloadResp struct {

drivers/wps/util.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,29 @@ func (d *Wps) getGroups(ctx context.Context) ([]Group, error) {
274274

275275
func (d *Wps) getFiles(ctx context.Context, groupID, parentID int64) ([]FileInfo, error) {
276276
var resp filesResp
277-
url := fmt.Sprintf("%s/api/v5/groups/%d/files", d.driveHost()+d.drivePrefix(), groupID)
278-
r, err := d.request(ctx).
279-
SetQueryParam("parentid", strconv.FormatInt(parentID, 10)).
280-
SetResult(&resp).
281-
SetError(&resp).
282-
Get(url)
283-
if err != nil {
284-
return nil, err
285-
}
286-
if r != nil && r.IsError() {
287-
return nil, fmt.Errorf("http error: %d", r.StatusCode())
277+
var files []FileInfo
278+
next_offset := 0
279+
for range 50 {
280+
url := fmt.Sprintf("%s/api/v5/groups/%d/files", d.driveHost()+d.drivePrefix(), groupID)
281+
r, err := d.request(ctx).
282+
SetQueryParam("parentid", strconv.FormatInt(parentID, 10)).
283+
SetQueryParam("offset", fmt.Sprint(next_offset)).
284+
SetResult(&resp).
285+
SetError(&resp).
286+
Get(url)
287+
if err != nil {
288+
return nil, err
289+
}
290+
if r != nil && r.IsError() {
291+
return nil, fmt.Errorf("http error: %d", r.StatusCode())
292+
}
293+
files = append(files, resp.Files...)
294+
if resp.NextOffset == -1 {
295+
break
296+
}
297+
next_offset = resp.NextOffset
288298
}
289-
return resp.Files, nil
299+
return files, nil
290300
}
291301

292302
func parseTime(v int64) time.Time {

0 commit comments

Comments
 (0)