From aaa8c440fe115e187b783754b9471e9ab67e9a0c Mon Sep 17 00:00:00 2001 From: Zexi Date: Fri, 13 Jan 2023 21:20:21 +0800 Subject: [PATCH] fix(seafile): token refresh (#3010) * docs: add Seafile support * fix: Seafile token refresh --- README_cn.md | 1 + drivers/seafile/util.go | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README_cn.md b/README_cn.md index f44c4cd8..1d8e7788 100644 --- a/README_cn.md +++ b/README_cn.md @@ -53,6 +53,7 @@ - [x] FTP / SFTP - [x] [PikPak](https://www.mypikpak.com/) - [x] [S3](https://aws.amazon.com/cn/s3/) + - [x] [Seafile](https://seafile.com/) - [x] [又拍云对象存储](https://www.upyun.com/products/file-storage) - [x] WebDav(支持无API的OneDrive/SharePoint) - [x] Teambition([中国](https://www.teambition.com/ ),[国际](https://us.teambition.com/ )) diff --git a/drivers/seafile/util.go b/drivers/seafile/util.go index 7714cd0a..6f9fe7a3 100644 --- a/drivers/seafile/util.go +++ b/drivers/seafile/util.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/alist-org/alist/v3/drivers/base" + "github.com/go-resty/resty/v2" ) func (d *Seafile) getToken() error { @@ -35,11 +36,21 @@ func (d *Seafile) request(method string, pathname string, callback base.ReqCallb if len(noRedirect) > 0 && noRedirect[0] { req = base.NoRedirectClient.R() } - req.SetHeader("Authorization", d.authorization) - callback(req) - res, err := req.Execute(method, full) - if err != nil { - return nil, err + var res resty.Response + for i := 0; i < 2; i++ { + req.SetHeader("Authorization", d.authorization) + callback(req) + res, err := req.Execute(method, full) + if err != nil { + return nil, err + } + if res.StatusCode() != 401 { // Unauthorized + break + } + err = d.getToken() + if err != nil { + return nil, err + } } if res.StatusCode() >= 400 { return nil, fmt.Errorf("request failed: %s", res.String())