Skip to content

Commit 79d683b

Browse files
xrgzsForSourceCodeAnalysis
authored andcommitted
fix(123open): get direct link (OpenListTeam#1185)
* fix(123open): correct query parameter name from 'fileId' to 'fileID' in getDirectLink function Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * fix(123open): change SpaceTempExpr type from 'string' to 'int64' in UserInfoResp struct Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * fix(123open): comment out unused fields in UserInfoResp struct Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> * fix(123open): add getUID method and cache UID Signed-off-by: MadDogOwner <xiaoran@xrgzs.top> --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent 1d24122 commit 79d683b

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

drivers/123_open/driver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
type Open123 struct {
2424
model.Storage
2525
Addition
26+
UID uint64
2627
}
2728

2829
func (d *Open123) Config() driver.Config {
@@ -96,15 +97,15 @@ func (d *Open123) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
9697
}, nil
9798
}
9899

99-
u, err := d.getUserInfo()
100+
uid, err := d.getUID()
100101
if err != nil {
101102
return nil, err
102103
}
103104

104105
duration := time.Duration(d.DirectLinkValidDuration) * time.Minute
105106

106107
newURL, err := d.SignURL(res.Data.URL, d.DirectLinkPrivateKey,
107-
u.Data.UID, duration)
108+
uid, duration)
108109
if err != nil {
109110
return nil, err
110111
}

drivers/123_open/util.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,55 @@ func (d *Open123) getFiles(parentFileId int64, limit int, lastFileId int64) (*Fi
145145
resp := &FileListResp{}
146146

147147
_, err := d.Request(baseURL+fileListAPI, http.MethodGet, func(req *resty.Request) {
148+
// 生成随机数(建议使用UUID,不能包含中划线(-))
149+
rand := strings.ReplaceAll(uuid.New().String(), "-", "")
150+
151+
// 解析URL
152+
objURL, err := url.Parse(originURL)
153+
if err != nil {
154+
return "", err
155+
}
156+
157+
// 待签名字符串,格式:path-timestamp-rand-uid-privateKey
158+
unsignedStr := fmt.Sprintf("%s-%d-%s-%d-%s", objURL.Path, ts, rand, uid, privateKey)
159+
md5Hash := md5.Sum([]byte(unsignedStr))
160+
// 生成鉴权参数,格式:timestamp-rand-uid-md5hash
161+
authKey := fmt.Sprintf("%d-%s-%d-%x", ts, rand, uid, md5Hash)
162+
163+
// 添加鉴权参数到URL查询参数
164+
v := objURL.Query()
165+
v.Add("auth_key", authKey)
166+
objURL.RawQuery = v.Encode()
167+
168+
return objURL.String(), nil
169+
}
170+
171+
func (d *Open123) getUserInfo() (*UserInfoResp, error) {
172+
var resp UserInfoResp
173+
174+
if _, err := d.Request(UserInfo, http.MethodGet, nil, &resp); err != nil {
175+
return nil, err
176+
}
177+
178+
return &resp, nil
179+
}
180+
181+
func (d *Open123) getUID() (uint64, error) {
182+
if d.UID != 0 {
183+
return d.UID, nil
184+
}
185+
resp, err := d.getUserInfo()
186+
if err != nil {
187+
return 0, err
188+
}
189+
d.UID = resp.Data.UID
190+
return resp.Data.UID, nil
191+
}
192+
193+
func (d *Open123) getFiles(parentFileId int64, limit int, lastFileId int64) (*FileListResp, error) {
194+
var resp FileListResp
195+
196+
_, err := d.Request(FileList, http.MethodGet, func(req *resty.Request) {
148197
req.SetQueryParams(
149198
map[string]string{
150199
"parentFileId": strconv.FormatInt(parentFileId, 10),
@@ -176,7 +225,7 @@ func (d *Open123) getDirectLink(fileId int64) (*DirectLinkResp, error) {
176225

177226
_, err := d.Request(DirectLink, http.MethodGet, func(req *resty.Request) {
178227
req.SetQueryParams(map[string]string{
179-
"fileId": strconv.FormatInt(fileId, 10),
228+
"fileID": strconv.FormatInt(fileId, 10),
180229
})
181230
}, &resp)
182231
if err != nil {

0 commit comments

Comments
 (0)