fix: use `get_share_link_download_url` if can't `get_download_url` (close #1753)

pull/1831/head
Noah Hsu 2022-09-25 20:32:11 +08:00
parent 96d8a382e8
commit 5ac6a30c56
2 changed files with 41 additions and 7 deletions

View File

@ -93,6 +93,7 @@ func (d *AliyundriveShare) Link(ctx context.Context, file model.Obj, args model.
if err != nil {
return nil, err
}
var u string
if e.Code != "" {
if e.Code == "AccessTokenInvalid" {
err = d.refreshToken()
@ -100,14 +101,47 @@ func (d *AliyundriveShare) Link(ctx context.Context, file model.Obj, args model.
return nil, err
}
return d.Link(ctx, file, args)
} else if e.Code == "ForbiddenNoPermission.File" {
data = utils.MergeMap(data, base.Json{
// Only ten minutes valid
"expire_sec": 600,
"share_id": d.ShareId,
})
var resp ShareLinkResp
var e2 ErrorResp
_, err = base.RestyClient.R().
SetError(&e2).SetBody(data).SetResult(&resp).
SetHeader("content-type", "application/json").
SetHeader("Authorization", "Bearer\t"+d.AccessToken).
SetHeader("x-share-token", d.ShareToken).
Post("https://api.aliyundrive.com/v2/file/get_share_link_download_url")
if err != nil {
return nil, err
}
if e2.Code != "" {
if e2.Code == "AccessTokenInvalid" || e2.Code == "ShareLinkTokenInvalid" {
err = d.getShareToken()
if err != nil {
return nil, err
}
return d.Link(ctx, file, args)
} else {
return nil, errors.New(e2.Code + ":" + e2.Message)
}
} else {
u = resp.DownloadUrl
}
} else {
return nil, errors.New(e.Code + ":" + e.Message)
}
return nil, errors.New(e.Message)
} else {
u = utils.Json.Get(res.Body(), "url").ToString()
}
return &model.Link{
Header: http.Header{
"Referer": []string{"https://www.aliyundrive.com/"},
},
URL: utils.Json.Get(res.Body(), "url").ToString(),
URL: u,
}, nil
}

View File

@ -50,8 +50,8 @@ func fileToObj(f File) *model.ObjThumb {
}
}
//type ShareLinkResp struct {
// DownloadUrl string `json:"download_url"`
// Url string `json:"url"`
// Thumbnail string `json:"thumbnail"`
//}
type ShareLinkResp struct {
DownloadUrl string `json:"download_url"`
Url string `json:"url"`
Thumbnail string `json:"thumbnail"`
}