Skip to content

Commit

Permalink
feat: add cache for xunlei
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Mar 4, 2022
1 parent 34ed05c commit 6faecbd
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions drivers/xunlei/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,19 @@ func (driver XunLeiCloud) File(path string, account *model.Account) (*model.File
}

func (driver XunLeiCloud) Files(path string, account *model.Account) ([]model.File, error) {
cache, err := base.GetCache(path, account)
if err == nil {
files, _ := cache.([]model.File)
return files, nil
}
file, err := driver.File(utils.ParsePath(path), account)
if err != nil {
return nil, err
}

var fileList FileList
url := fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files?parent_id=%s&page_token=%s&with_audit=true&filters=%s", file.Id, "", url.QueryEscape(`{"phase": {"eq": "PHASE_TYPE_COMPLETE"}, "trashed":{"eq":false}}`))
if err = GetState(account).Request("GET", url, nil, &fileList, account); err != nil {
u := fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files?parent_id=%s&page_token=%s&with_audit=true&filters=%s", file.Id, "", url.QueryEscape(`{"phase": {"eq": "PHASE_TYPE_COMPLETE"}, "trashed":{"eq":false}}`))
if err = GetState(account).Request("GET", u, nil, &fileList, account); err != nil {
return nil, err
}

Expand All @@ -106,6 +111,9 @@ func (driver XunLeiCloud) Files(path string, account *model.Account) ([]model.Fi
files = append(files, *driver.formatFile(&file))
}
}
if len(files) > 0 {
_ = base.SetCache(path, files, account)
}
return files, nil
}

Expand Down Expand Up @@ -134,15 +142,15 @@ func (driver XunLeiCloud) Link(args base.Args, account *model.Account) (*base.Li
if file.Type == conf.FOLDER {
return nil, base.ErrNotFile
}
var lfile Files
if err = GetState(account).Request("GET", fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files/%s?&with_audit=true", file.Id), nil, &lfile, account); err != nil {
var lFile Files
if err = GetState(account).Request("GET", fmt.Sprintf("https://api-pan.xunlei.com/drive/v1/files/%s?&with_audit=true", file.Id), nil, &lFile, account); err != nil {
return nil, err
}
return &base.Link{
Headers: []base.Header{
{Name: "User-Agent", Value: base.UserAgent},
},
Url: lfile.WebContentLink,
Url: lFile.WebContentLink,
}, nil
}

Expand Down

2 comments on commit 6faecbd

@foxxorcat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加缓存后有一个bug,对文件执行删除增加等操作时必须加延迟,不然可能刷新不出来。

@xhofe
Copy link
Collaborator Author

@xhofe xhofe commented on 6faecbd Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加缓存后有一个bug,对文件执行删除增加等操作时必须加延迟,不然可能刷新不出来。

没关系,列表才是更常用的操作,没有缓存目录深的时候每一级都要请求请求频繁且响应慢,且删除刷新不出来的时候可以右键刷新的。

Please sign in to comment.