2022-09-02 14:46:31 +00:00
|
|
|
|
package baidu_netdisk
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/md5"
|
|
|
|
|
"encoding/hex"
|
2023-08-11 06:23:30 +00:00
|
|
|
|
"errors"
|
2023-08-09 08:13:09 +00:00
|
|
|
|
"io"
|
|
|
|
|
"math"
|
2023-08-11 06:23:30 +00:00
|
|
|
|
"net/url"
|
2023-08-09 08:13:09 +00:00
|
|
|
|
stdpath "path"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-08-07 05:44:28 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/drivers/base"
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2023-08-09 08:13:09 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/pkg/errgroup"
|
2023-08-07 05:44:28 +00:00
|
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
|
"github.com/avast/retry-go"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-09-02 14:46:31 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BaiduNetdisk struct {
|
|
|
|
|
model.Storage
|
|
|
|
|
Addition
|
2023-08-09 08:13:09 +00:00
|
|
|
|
|
|
|
|
|
uploadThread int
|
2023-10-20 13:06:25 +00:00
|
|
|
|
vipType int // 会员类型,0普通用户(4G/4M)、1普通会员(10G/16M)、2超级会员(20G/32M)
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) Config() driver.Config {
|
|
|
|
|
return config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) GetAddition() driver.Additional {
|
2022-12-13 10:03:30 +00:00
|
|
|
|
return &d.Addition
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 10:03:30 +00:00
|
|
|
|
func (d *BaiduNetdisk) Init(ctx context.Context) error {
|
2023-08-09 08:13:09 +00:00
|
|
|
|
d.uploadThread, _ = strconv.Atoi(d.UploadThread)
|
|
|
|
|
if d.uploadThread < 1 || d.uploadThread > 32 {
|
|
|
|
|
d.uploadThread, d.UploadThread = 3, "3"
|
|
|
|
|
}
|
2023-08-11 06:23:30 +00:00
|
|
|
|
|
|
|
|
|
if _, err := url.Parse(d.UploadAPI); d.UploadAPI == "" || err != nil {
|
|
|
|
|
d.UploadAPI = "https://d.pcs.baidu.com"
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 10:31:42 +00:00
|
|
|
|
res, err := d.get("/xpan/nas", map[string]string{
|
|
|
|
|
"method": "uinfo",
|
|
|
|
|
}, nil)
|
|
|
|
|
log.Debugf("[baidu] get uinfo: %s", string(res))
|
2023-10-20 13:06:25 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
d.vipType = utils.Json.Get(res, "vip_type").ToInt()
|
|
|
|
|
return nil
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) Drop(ctx context.Context) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
|
|
|
|
files, err := d.getFiles(dir.GetPath())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-09-04 16:26:04 +00:00
|
|
|
|
return utils.SliceConvert(files, func(src File) (model.Obj, error) {
|
|
|
|
|
return fileToObj(src), nil
|
|
|
|
|
})
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
|
|
|
|
if d.DownloadAPI == "crack" {
|
|
|
|
|
return d.linkCrack(file, args)
|
|
|
|
|
}
|
|
|
|
|
return d.linkOfficial(file, args)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
func (d *BaiduNetdisk) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
|
|
|
|
|
var newDir File
|
2023-08-27 13:14:23 +00:00
|
|
|
|
_, err := d.create(stdpath.Join(parentDir.GetPath(), dirName), 0, 1, "", "", &newDir, 0, 0)
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return fileToObj(newDir), nil
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
func (d *BaiduNetdisk) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
|
2022-09-02 14:46:31 +00:00
|
|
|
|
data := []base.Json{
|
|
|
|
|
{
|
|
|
|
|
"path": srcObj.GetPath(),
|
|
|
|
|
"dest": dstDir.GetPath(),
|
|
|
|
|
"newname": srcObj.GetName(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
_, err := d.manage("move", data)
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if srcObj, ok := srcObj.(*model.ObjThumb); ok {
|
|
|
|
|
srcObj.SetPath(stdpath.Join(dstDir.GetPath(), srcObj.GetName()))
|
|
|
|
|
srcObj.Modified = time.Now()
|
|
|
|
|
return srcObj, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
func (d *BaiduNetdisk) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
|
2022-09-02 14:46:31 +00:00
|
|
|
|
data := []base.Json{
|
|
|
|
|
{
|
|
|
|
|
"path": srcObj.GetPath(),
|
|
|
|
|
"newname": newName,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
_, err := d.manage("rename", data)
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if srcObj, ok := srcObj.(*model.ObjThumb); ok {
|
|
|
|
|
srcObj.SetPath(stdpath.Join(stdpath.Dir(srcObj.GetPath()), newName))
|
|
|
|
|
srcObj.Name = newName
|
|
|
|
|
srcObj.Modified = time.Now()
|
|
|
|
|
return srcObj, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
|
data := []base.Json{
|
|
|
|
|
{
|
|
|
|
|
"path": srcObj.GetPath(),
|
2022-12-31 08:43:22 +00:00
|
|
|
|
"dest": dstDir.GetPath(),
|
|
|
|
|
"newname": srcObj.GetName(),
|
2022-09-02 14:46:31 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
_, err := d.manage("copy", data)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *BaiduNetdisk) Remove(ctx context.Context, obj model.Obj) error {
|
|
|
|
|
data := []string{obj.GetPath()}
|
|
|
|
|
_, err := d.manage("delete", data)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 06:46:35 +00:00
|
|
|
|
func (d *BaiduNetdisk) PutRapid(ctx context.Context, dstDir model.Obj, stream model.FileStreamer) (model.Obj, error) {
|
|
|
|
|
contentMd5 := stream.GetHash().GetHash(utils.MD5)
|
|
|
|
|
if len(contentMd5) < utils.MD5.Width {
|
|
|
|
|
return nil, errors.New("invalid hash")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
streamSize := stream.GetSize()
|
2023-10-10 10:08:27 +00:00
|
|
|
|
path := stdpath.Join(dstDir.GetPath(), stream.GetName())
|
2023-09-06 06:46:35 +00:00
|
|
|
|
mtime := stream.ModTime().Unix()
|
|
|
|
|
ctime := stream.CreateTime().Unix()
|
|
|
|
|
blockList, _ := utils.Json.MarshalToString([]string{contentMd5})
|
|
|
|
|
|
|
|
|
|
var newFile File
|
2023-10-10 10:08:27 +00:00
|
|
|
|
_, err := d.create(path, streamSize, 0, "", blockList, &newFile, mtime, ctime)
|
2023-09-06 06:46:35 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return fileToObj(newFile), nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
func (d *BaiduNetdisk) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
|
2023-09-06 06:46:35 +00:00
|
|
|
|
// rapid upload
|
|
|
|
|
if newObj, err := d.PutRapid(ctx, dstDir, stream); err == nil {
|
|
|
|
|
return newObj, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 13:14:23 +00:00
|
|
|
|
tempFile, err := stream.CacheFullInTempFile()
|
2022-09-10 06:11:06 +00:00
|
|
|
|
if err != nil {
|
2023-08-09 08:13:09 +00:00
|
|
|
|
return nil, err
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-07 05:44:28 +00:00
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
streamSize := stream.GetSize()
|
2023-10-20 13:06:25 +00:00
|
|
|
|
sliceSize := d.getSliceSize()
|
|
|
|
|
count := int(math.Max(math.Ceil(float64(streamSize)/float64(sliceSize)), 1))
|
|
|
|
|
lastBlockSize := streamSize % sliceSize
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if streamSize > 0 && lastBlockSize == 0 {
|
2023-10-20 13:06:25 +00:00
|
|
|
|
lastBlockSize = sliceSize
|
2023-08-09 08:13:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 05:44:28 +00:00
|
|
|
|
//cal md5 for first 256k data
|
|
|
|
|
const SliceSize int64 = 256 * 1024
|
2022-09-02 14:46:31 +00:00
|
|
|
|
// cal md5
|
2023-08-09 08:13:09 +00:00
|
|
|
|
blockList := make([]string, 0, count)
|
2023-10-20 13:06:25 +00:00
|
|
|
|
byteSize := sliceSize
|
2023-08-09 08:13:09 +00:00
|
|
|
|
fileMd5H := md5.New()
|
|
|
|
|
sliceMd5H := md5.New()
|
|
|
|
|
sliceMd5H2 := md5.New()
|
|
|
|
|
slicemd5H2Write := utils.LimitWriter(sliceMd5H2, SliceSize)
|
|
|
|
|
|
|
|
|
|
for i := 1; i <= count; i++ {
|
|
|
|
|
if utils.IsCanceled(ctx) {
|
|
|
|
|
return nil, ctx.Err()
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if i == count {
|
|
|
|
|
byteSize = lastBlockSize
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
_, err := io.CopyN(io.MultiWriter(fileMd5H, sliceMd5H, slicemd5H2Write), tempFile, byteSize)
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
|
return nil, err
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
blockList = append(blockList, hex.EncodeToString(sliceMd5H.Sum(nil)))
|
|
|
|
|
sliceMd5H.Reset()
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
contentMd5 := hex.EncodeToString(fileMd5H.Sum(nil))
|
|
|
|
|
sliceMd5 := hex.EncodeToString(sliceMd5H2.Sum(nil))
|
|
|
|
|
blockListStr, _ := utils.Json.MarshalToString(blockList)
|
2023-10-10 10:08:27 +00:00
|
|
|
|
path := stdpath.Join(dstDir.GetPath(), stream.GetName())
|
2023-08-27 13:14:23 +00:00
|
|
|
|
mtime := stream.ModTime().Unix()
|
|
|
|
|
ctime := stream.CreateTime().Unix()
|
2023-08-09 08:13:09 +00:00
|
|
|
|
|
2023-08-11 06:23:30 +00:00
|
|
|
|
// step.1 预上传
|
|
|
|
|
// 尝试获取之前的进度
|
|
|
|
|
precreateResp, ok := base.GetUploadProgress[*PrecreateResp](d, d.AccessToken, contentMd5)
|
|
|
|
|
if !ok {
|
|
|
|
|
params := map[string]string{
|
|
|
|
|
"method": "precreate",
|
|
|
|
|
}
|
2023-10-10 10:08:27 +00:00
|
|
|
|
form := map[string]string{
|
|
|
|
|
"path": path,
|
|
|
|
|
"size": strconv.FormatInt(streamSize, 10),
|
|
|
|
|
"isdir": "0",
|
|
|
|
|
"autoinit": "1",
|
|
|
|
|
"rtype": "3",
|
|
|
|
|
"block_list": blockListStr,
|
|
|
|
|
"content-md5": contentMd5,
|
|
|
|
|
"slice-md5": sliceMd5,
|
|
|
|
|
}
|
|
|
|
|
joinTime(form, ctime, mtime)
|
|
|
|
|
|
|
|
|
|
log.Debugf("[baidu_netdisk] precreate data: %s", form)
|
|
|
|
|
_, err = d.postForm("/xpan/file", params, form, &precreateResp)
|
2022-09-02 14:46:31 +00:00
|
|
|
|
if err != nil {
|
2023-08-09 08:13:09 +00:00
|
|
|
|
return nil, err
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-11 06:23:30 +00:00
|
|
|
|
log.Debugf("%+v", precreateResp)
|
|
|
|
|
if precreateResp.ReturnType == 2 {
|
|
|
|
|
//rapid upload, since got md5 match from baidu server
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return fileToObj(precreateResp.File), nil
|
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
}
|
|
|
|
|
// step.2 上传分片
|
|
|
|
|
threadG, upCtx := errgroup.NewGroupWithContext(ctx, d.uploadThread,
|
|
|
|
|
retry.Attempts(3),
|
|
|
|
|
retry.Delay(time.Second),
|
|
|
|
|
retry.DelayType(retry.BackOffDelay))
|
2023-08-11 06:23:30 +00:00
|
|
|
|
for i, partseq := range precreateResp.BlockList {
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if utils.IsCanceled(upCtx) {
|
|
|
|
|
break
|
2022-09-11 09:09:48 +00:00
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
|
2023-10-20 13:06:25 +00:00
|
|
|
|
i, partseq, offset, byteSize := i, partseq, int64(partseq)*sliceSize, sliceSize
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if partseq+1 == count {
|
|
|
|
|
byteSize = lastBlockSize
|
|
|
|
|
}
|
|
|
|
|
threadG.Go(func(ctx context.Context) error {
|
|
|
|
|
params := map[string]string{
|
|
|
|
|
"method": "upload",
|
|
|
|
|
"access_token": d.AccessToken,
|
|
|
|
|
"type": "tmpfile",
|
|
|
|
|
"path": path,
|
|
|
|
|
"uploadid": precreateResp.Uploadid,
|
|
|
|
|
"partseq": strconv.Itoa(partseq),
|
|
|
|
|
}
|
|
|
|
|
err := d.uploadSlice(ctx, params, stream.GetName(), io.NewSectionReader(tempFile, offset, byteSize))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-11-06 08:56:55 +00:00
|
|
|
|
up(float64(threadG.Success()) * 100 / float64(len(precreateResp.BlockList)))
|
2023-08-11 06:23:30 +00:00
|
|
|
|
precreateResp.BlockList[i] = -1
|
2023-08-09 08:13:09 +00:00
|
|
|
|
return nil
|
|
|
|
|
})
|
2022-09-02 14:46:31 +00:00
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if err = threadG.Wait(); err != nil {
|
2023-08-11 06:23:30 +00:00
|
|
|
|
// 如果属于用户主动取消,则保存上传进度
|
|
|
|
|
if errors.Is(err, context.Canceled) {
|
|
|
|
|
precreateResp.BlockList = utils.SliceFilter(precreateResp.BlockList, func(s int) bool { return s >= 0 })
|
|
|
|
|
base.SaveUploadProgress(d, precreateResp, d.AccessToken, contentMd5)
|
|
|
|
|
}
|
2023-08-09 08:13:09 +00:00
|
|
|
|
return nil, err
|
2023-08-07 05:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
// step.3 创建文件
|
|
|
|
|
var newFile File
|
2023-10-10 10:08:27 +00:00
|
|
|
|
_, err = d.create(path, streamSize, 0, precreateResp.Uploadid, blockListStr, &newFile, mtime, ctime)
|
2023-08-09 08:13:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return fileToObj(newFile), nil
|
|
|
|
|
}
|
2023-09-06 06:46:35 +00:00
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
|
func (d *BaiduNetdisk) uploadSlice(ctx context.Context, params map[string]string, fileName string, file io.Reader) error {
|
2023-08-07 05:44:28 +00:00
|
|
|
|
res, err := base.RestyClient.R().
|
|
|
|
|
SetContext(ctx).
|
2023-08-09 08:13:09 +00:00
|
|
|
|
SetQueryParams(params).
|
|
|
|
|
SetFileReader("file", fileName, file).
|
2023-08-11 06:23:30 +00:00
|
|
|
|
Post(d.UploadAPI + "/rest/2.0/pcs/superfile2")
|
2023-08-07 05:44:28 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
log.Debugln(res.RawResponse.Status + res.String())
|
|
|
|
|
errCode := utils.Json.Get(res.Body(), "error_code").ToInt()
|
|
|
|
|
errNo := utils.Json.Get(res.Body(), "errno").ToInt()
|
|
|
|
|
if errCode != 0 || errNo != 0 {
|
|
|
|
|
return errs.NewErr(errs.StreamIncomplete, "error in uploading to baidu, will retry. response=%s", res.String())
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-09-02 14:46:31 +00:00
|
|
|
|
|
|
|
|
|
var _ driver.Driver = (*BaiduNetdisk)(nil)
|