mirror of https://github.com/Xhofe/alist
fix(baidu_photo): cookie login fix download error (#7602)
parent
492b49d77a
commit
2d3605c684
|
@ -27,9 +27,9 @@ type BaiduPhoto struct {
|
||||||
model.Storage
|
model.Storage
|
||||||
Addition
|
Addition
|
||||||
|
|
||||||
AccessToken string
|
// AccessToken string
|
||||||
Uk int64
|
Uk int64
|
||||||
root model.Obj
|
root model.Obj
|
||||||
|
|
||||||
uploadThread int
|
uploadThread int
|
||||||
}
|
}
|
||||||
|
@ -48,9 +48,9 @@ func (d *BaiduPhoto) Init(ctx context.Context) error {
|
||||||
d.uploadThread, d.UploadThread = 3, "3"
|
d.uploadThread, d.UploadThread = 3, "3"
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := d.refreshToken(); err != nil {
|
// if err := d.refreshToken(); err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
// root
|
// root
|
||||||
if d.AlbumID != "" {
|
if d.AlbumID != "" {
|
||||||
|
@ -82,7 +82,7 @@ func (d *BaiduPhoto) GetRoot(ctx context.Context) (model.Obj, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *BaiduPhoto) Drop(ctx context.Context) error {
|
func (d *BaiduPhoto) Drop(ctx context.Context) error {
|
||||||
d.AccessToken = ""
|
// d.AccessToken = ""
|
||||||
d.Uk = 0
|
d.Uk = 0
|
||||||
d.root = nil
|
d.root = nil
|
||||||
return nil
|
return nil
|
||||||
|
@ -140,14 +140,13 @@ func (d *BaiduPhoto) Link(ctx context.Context, file model.Obj, args model.LinkAr
|
||||||
// 处理共享相册
|
// 处理共享相册
|
||||||
if d.Uk != file.Uk {
|
if d.Uk != file.Uk {
|
||||||
// 有概率无法获取到链接
|
// 有概率无法获取到链接
|
||||||
return d.linkAlbum(ctx, file, args)
|
// return d.linkAlbum(ctx, file, args)
|
||||||
|
|
||||||
// 接口被限制,只能使用cookie
|
f, err := d.CopyAlbumFile(ctx, file)
|
||||||
// f, err := d.CopyAlbumFile(ctx, file)
|
if err != nil {
|
||||||
// if err != nil {
|
return nil, err
|
||||||
// return nil, err
|
}
|
||||||
// }
|
return d.linkFile(ctx, f, args)
|
||||||
// return d.linkFile(ctx, f, args)
|
|
||||||
}
|
}
|
||||||
return d.linkFile(ctx, &file.File, args)
|
return d.linkFile(ctx, &file.File, args)
|
||||||
}
|
}
|
||||||
|
@ -292,7 +291,7 @@ func (d *BaiduPhoto) Put(ctx context.Context, dstDir model.Obj, stream model.Fil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 尝试获取之前的进度
|
// 尝试获取之前的进度
|
||||||
precreateResp, ok := base.GetUploadProgress[*PrecreateResp](d, d.AccessToken, contentMd5)
|
precreateResp, ok := base.GetUploadProgress[*PrecreateResp](d, strconv.FormatInt(d.Uk, 10), contentMd5)
|
||||||
if !ok {
|
if !ok {
|
||||||
_, err = d.Post(FILE_API_URL_V1+"/precreate", func(r *resty.Request) {
|
_, err = d.Post(FILE_API_URL_V1+"/precreate", func(r *resty.Request) {
|
||||||
r.SetContext(ctx)
|
r.SetContext(ctx)
|
||||||
|
@ -343,7 +342,7 @@ func (d *BaiduPhoto) Put(ctx context.Context, dstDir model.Obj, stream model.Fil
|
||||||
if err = threadG.Wait(); err != nil {
|
if err = threadG.Wait(); err != nil {
|
||||||
if errors.Is(err, context.Canceled) {
|
if errors.Is(err, context.Canceled) {
|
||||||
precreateResp.BlockList = utils.SliceFilter(precreateResp.BlockList, func(s int) bool { return s >= 0 })
|
precreateResp.BlockList = utils.SliceFilter(precreateResp.BlockList, func(s int) bool { return s >= 0 })
|
||||||
base.SaveUploadProgress(d, precreateResp, d.AccessToken, contentMd5)
|
base.SaveUploadProgress(d, strconv.FormatInt(d.Uk, 10), contentMd5)
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Addition struct {
|
type Addition struct {
|
||||||
RefreshToken string `json:"refresh_token" required:"true"`
|
// RefreshToken string `json:"refresh_token" required:"true"`
|
||||||
ShowType string `json:"show_type" type:"select" options:"root,root_only_album,root_only_file" default:"root"`
|
Cookie string `json:"cookie" required:"true"`
|
||||||
AlbumID string `json:"album_id"`
|
ShowType string `json:"show_type" type:"select" options:"root,root_only_album,root_only_file" default:"root"`
|
||||||
|
AlbumID string `json:"album_id"`
|
||||||
//AlbumPassword string `json:"album_password"`
|
//AlbumPassword string `json:"album_password"`
|
||||||
DeleteOrigin bool `json:"delete_origin"`
|
DeleteOrigin bool `json:"delete_origin"`
|
||||||
ClientID string `json:"client_id" required:"true" default:"iYCeC9g08h5vuP9UqvPHKKSVrKFXGa1v"`
|
// ClientID string `json:"client_id" required:"true" default:"iYCeC9g08h5vuP9UqvPHKKSVrKFXGa1v"`
|
||||||
ClientSecret string `json:"client_secret" required:"true" default:"jXiFMOPVPCWlO2M5CwWQzffpNPaGTRBG"`
|
// ClientSecret string `json:"client_secret" required:"true" default:"jXiFMOPVPCWlO2M5CwWQzffpNPaGTRBG"`
|
||||||
UploadThread string `json:"upload_thread" default:"3" help:"1<=thread<=32"`
|
UploadThread string `json:"upload_thread" default:"3" help:"1<=thread<=32"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,7 @@ import (
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/drivers/base"
|
"github.com/alist-org/alist/v3/drivers/base"
|
||||||
"github.com/alist-org/alist/v3/internal/errs"
|
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/op"
|
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
@ -27,7 +25,8 @@ const (
|
||||||
|
|
||||||
func (d *BaiduPhoto) Request(client *resty.Client, furl string, method string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
|
func (d *BaiduPhoto) Request(client *resty.Client, furl string, method string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
|
||||||
req := client.R().
|
req := client.R().
|
||||||
SetQueryParam("access_token", d.AccessToken)
|
// SetQueryParam("access_token", d.AccessToken)
|
||||||
|
SetHeader("Cookie", d.Cookie)
|
||||||
if callback != nil {
|
if callback != nil {
|
||||||
callback(req)
|
callback(req)
|
||||||
}
|
}
|
||||||
|
@ -49,10 +48,10 @@ func (d *BaiduPhoto) Request(client *resty.Client, furl string, method string, c
|
||||||
return nil, fmt.Errorf("no shared albums found")
|
return nil, fmt.Errorf("no shared albums found")
|
||||||
case 50100:
|
case 50100:
|
||||||
return nil, fmt.Errorf("illegal title, only supports 50 characters")
|
return nil, fmt.Errorf("illegal title, only supports 50 characters")
|
||||||
case -6:
|
// case -6:
|
||||||
if err = d.refreshToken(); err != nil {
|
// if err = d.refreshToken(); err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("errno: %d, refer to https://photo.baidu.com/union/doc", erron)
|
return nil, fmt.Errorf("errno: %d, refer to https://photo.baidu.com/union/doc", erron)
|
||||||
}
|
}
|
||||||
|
@ -67,29 +66,29 @@ func (d *BaiduPhoto) Request(client *resty.Client, furl string, method string, c
|
||||||
// return res.Body(), nil
|
// return res.Body(), nil
|
||||||
//}
|
//}
|
||||||
|
|
||||||
func (d *BaiduPhoto) refreshToken() error {
|
// func (d *BaiduPhoto) refreshToken() error {
|
||||||
u := "https://openapi.baidu.com/oauth/2.0/token"
|
// u := "https://openapi.baidu.com/oauth/2.0/token"
|
||||||
var resp base.TokenResp
|
// var resp base.TokenResp
|
||||||
var e TokenErrResp
|
// var e TokenErrResp
|
||||||
_, err := base.RestyClient.R().SetResult(&resp).SetError(&e).SetQueryParams(map[string]string{
|
// _, err := base.RestyClient.R().SetResult(&resp).SetError(&e).SetQueryParams(map[string]string{
|
||||||
"grant_type": "refresh_token",
|
// "grant_type": "refresh_token",
|
||||||
"refresh_token": d.RefreshToken,
|
// "refresh_token": d.RefreshToken,
|
||||||
"client_id": d.ClientID,
|
// "client_id": d.ClientID,
|
||||||
"client_secret": d.ClientSecret,
|
// "client_secret": d.ClientSecret,
|
||||||
}).Get(u)
|
// }).Get(u)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
if e.ErrorMsg != "" {
|
// if e.ErrorMsg != "" {
|
||||||
return &e
|
// return &e
|
||||||
}
|
// }
|
||||||
if resp.RefreshToken == "" {
|
// if resp.RefreshToken == "" {
|
||||||
return errs.EmptyToken
|
// return errs.EmptyToken
|
||||||
}
|
// }
|
||||||
d.AccessToken, d.RefreshToken = resp.AccessToken, resp.RefreshToken
|
// d.AccessToken, d.RefreshToken = resp.AccessToken, resp.RefreshToken
|
||||||
op.MustSaveDriverStorage(d)
|
// op.MustSaveDriverStorage(d)
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (d *BaiduPhoto) Get(furl string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
|
func (d *BaiduPhoto) Get(furl string, callback base.ReqCallback, resp interface{}) (*resty.Response, error) {
|
||||||
return d.Request(base.RestyClient, furl, http.MethodGet, callback, resp)
|
return d.Request(base.RestyClient, furl, http.MethodGet, callback, resp)
|
||||||
|
@ -363,10 +362,6 @@ func (d *BaiduPhoto) linkAlbum(ctx context.Context, file *AlbumFile, args model.
|
||||||
|
|
||||||
location := resp.Header().Get("Location")
|
location := resp.Header().Get("Location")
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
link := &model.Link{
|
link := &model.Link{
|
||||||
URL: location,
|
URL: location,
|
||||||
Header: http.Header{
|
Header: http.Header{
|
||||||
|
@ -388,36 +383,36 @@ func (d *BaiduPhoto) linkFile(ctx context.Context, file *File, args model.LinkAr
|
||||||
headers["X-Forwarded-For"] = args.IP
|
headers["X-Forwarded-For"] = args.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
// var downloadUrl struct {
|
var downloadUrl struct {
|
||||||
// Dlink string `json:"dlink"`
|
Dlink string `json:"dlink"`
|
||||||
// }
|
}
|
||||||
// _, err := d.Get(FILE_API_URL_V1+"/download", func(r *resty.Request) {
|
_, err := d.Get(FILE_API_URL_V2+"/download", func(r *resty.Request) {
|
||||||
// r.SetContext(ctx)
|
|
||||||
// r.SetHeaders(headers)
|
|
||||||
// r.SetQueryParams(map[string]string{
|
|
||||||
// "fsid": fmt.Sprint(file.Fsid),
|
|
||||||
// })
|
|
||||||
// }, &downloadUrl)
|
|
||||||
|
|
||||||
resp, err := d.Request(base.NoRedirectClient, FILE_API_URL_V1+"/download", http.MethodHead, func(r *resty.Request) {
|
|
||||||
r.SetContext(ctx)
|
r.SetContext(ctx)
|
||||||
r.SetHeaders(headers)
|
r.SetHeaders(headers)
|
||||||
r.SetQueryParams(map[string]string{
|
r.SetQueryParams(map[string]string{
|
||||||
"fsid": fmt.Sprint(file.Fsid),
|
"fsid": fmt.Sprint(file.Fsid),
|
||||||
})
|
})
|
||||||
}, nil)
|
}, &downloadUrl)
|
||||||
|
|
||||||
|
// resp, err := d.Request(base.NoRedirectClient, FILE_API_URL_V1+"/download", http.MethodHead, func(r *resty.Request) {
|
||||||
|
// r.SetContext(ctx)
|
||||||
|
// r.SetHeaders(headers)
|
||||||
|
// r.SetQueryParams(map[string]string{
|
||||||
|
// "fsid": fmt.Sprint(file.Fsid),
|
||||||
|
// })
|
||||||
|
// }, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode() != 302 {
|
// if resp.StatusCode() != 302 {
|
||||||
return nil, fmt.Errorf("not found 302 redirect")
|
// return nil, fmt.Errorf("not found 302 redirect")
|
||||||
}
|
// }
|
||||||
|
|
||||||
location := resp.Header().Get("Location")
|
// location := resp.Header().Get("Location")
|
||||||
link := &model.Link{
|
link := &model.Link{
|
||||||
URL: location,
|
URL: downloadUrl.Dlink,
|
||||||
Header: http.Header{
|
Header: http.Header{
|
||||||
"User-Agent": []string{headers["User-Agent"]},
|
"User-Agent": []string{headers["User-Agent"]},
|
||||||
"Referer": []string{"https://photo.baidu.com/"},
|
"Referer": []string{"https://photo.baidu.com/"},
|
||||||
|
|
Loading…
Reference in New Issue