@@ -3,6 +3,7 @@ package LenovoNasShare
3
3
import (
4
4
"context"
5
5
"net/http"
6
+ "time"
6
7
7
8
"github.com/go-resty/resty/v2"
8
9
@@ -15,7 +16,8 @@ import (
15
16
type LenovoNasShare struct {
16
17
model.Storage
17
18
Addition
18
- stoken string
19
+ stoken string
20
+ expireAt int64
19
21
}
20
22
21
23
func (d * LenovoNasShare ) Config () driver.Config {
@@ -27,20 +29,9 @@ func (d *LenovoNasShare) GetAddition() driver.Additional {
27
29
}
28
30
29
31
func (d * LenovoNasShare ) Init (ctx context.Context ) error {
30
- if d .Host == "" {
31
- d .Host = "https://siot-share.lenovo.com.cn"
32
- }
33
- query := map [string ]string {
34
- "code" : d .ShareId ,
35
- "password" : d .SharePwd ,
36
- }
37
- resp , err := d .request (d .Host + "/oneproxy/api/share/v1/access" , http .MethodGet , func (req * resty.Request ) {
38
- req .SetQueryParams (query )
39
- }, nil )
40
- if err != nil {
32
+ if err := d .getStoken (); err != nil {
41
33
return err
42
34
}
43
- d .stoken = utils .Json .Get (resp , "data" , "stoken" ).ToString ()
44
35
return nil
45
36
}
46
37
@@ -49,6 +40,7 @@ func (d *LenovoNasShare) Drop(ctx context.Context) error {
49
40
}
50
41
51
42
func (d * LenovoNasShare ) List (ctx context.Context , dir model.Obj , args model.ListArgs ) ([]model.Obj , error ) {
43
+ d .checkStoken () // 检查stoken是否过期
52
44
files := make ([]File , 0 )
53
45
54
46
var resp Files
@@ -71,7 +63,33 @@ func (d *LenovoNasShare) List(ctx context.Context, dir model.Obj, args model.Lis
71
63
})
72
64
}
73
65
66
+ func (d * LenovoNasShare ) checkStoken () { // 检查stoken是否过期
67
+ if d .expireAt < time .Now ().Unix () {
68
+ d .getStoken ()
69
+ }
70
+ }
71
+
72
+ func (d * LenovoNasShare ) getStoken () error { // 获取stoken
73
+ if d .Host == "" {
74
+ d .Host = "https://siot-share.lenovo.com.cn"
75
+ }
76
+ query := map [string ]string {
77
+ "code" : d .ShareId ,
78
+ "password" : d .SharePwd ,
79
+ }
80
+ resp , err := d .request (d .Host + "/oneproxy/api/share/v1/access" , http .MethodGet , func (req * resty.Request ) {
81
+ req .SetQueryParams (query )
82
+ }, nil )
83
+ if err != nil {
84
+ return err
85
+ }
86
+ d .stoken = utils .Json .Get (resp , "data" , "stoken" ).ToString ()
87
+ d .expireAt = utils .Json .Get (resp , "data" , "expires_in" ).ToInt64 () + time .Now ().Unix () - 60
88
+ return nil
89
+ }
90
+
74
91
func (d * LenovoNasShare ) Link (ctx context.Context , file model.Obj , args model.LinkArgs ) (* model.Link , error ) {
92
+ d .checkStoken () // 检查stoken是否过期
75
93
query := map [string ]string {
76
94
"code" : d .ShareId ,
77
95
"stoken" : d .stoken ,
0 commit comments