Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from yorks/master
Browse files Browse the repository at this point in the history
-rt 参数支持保存RefreshToken的文件路径
  • Loading branch information
LinkLeong authored Nov 10, 2021
2 parents b49dad3 + 4e64950 commit 0241cd9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
> 选择对应的版本进行下载,进入到解压目录执行
```bash
./webdav -rt="your refreshToken"
# 或者
echo "your refreshToken" > /path/to/save/refreshToken
./webdav -rt /path/to/save/refreshToken
```

# 参数说明
```bash
-rt
阿里云盘的refreshToken,获取方式见下文
阿里云盘的refreshToken,获取方式见下文。或者包含refreshToken的文件路径。
-port
非必填,服务器端口号,默认为8085
-user
Expand Down
17 changes: 17 additions & 0 deletions aliyun/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package aliyun
import (
"encoding/json"
"fmt"
"os"
"io/ioutil"
"go-aliyun-webdav/aliyun/cache"
"go-aliyun-webdav/aliyun/model"
"go-aliyun-webdav/aliyun/net"
Expand Down Expand Up @@ -111,6 +113,14 @@ func GetFile(w http.ResponseWriter, url string, token string, rangeStr string, i
}

func RefreshToken(refreshToken string) model.RefreshTokenModel {
path := refreshToken
if _, err := os.Stat(path); err == nil {
buf, _ := ioutil.ReadFile(path)
refreshToken = string(buf)
if(len(refreshToken) >= 32){
refreshToken = refreshToken[:32] // refreshToken is only 32 bit?? FIXME
}
}
rs := net.Post(model.APIREFRESHTOKENURL, "", []byte(`{"refresh_token":"`+refreshToken+`"}`))
var refresh model.RefreshTokenModel
if len(rs) > 0 {
Expand All @@ -122,6 +132,13 @@ func RefreshToken(refreshToken string) model.RefreshTokenModel {
} else {
fmt.Println("刷新token失败")
}

if _, err := os.Stat(path); err == nil {
if(refreshToken != refresh.RefreshToken){
content := []byte(refresh.RefreshToken)
ioutil.WriteFile(path, content, 0600)
}
}
return refresh

}
Expand Down

0 comments on commit 0241cd9

Please sign in to comment.