2022-09-01 14:13:37 +00:00
|
|
|
package pikpak
|
2022-08-31 09:32:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/drivers/base"
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2023-09-06 06:46:35 +00:00
|
|
|
hash_extend "github.com/alist-org/alist/v3/pkg/utils/hash"
|
2022-08-31 09:32:57 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PikPak struct {
|
|
|
|
model.Storage
|
|
|
|
Addition
|
|
|
|
RefreshToken string
|
|
|
|
AccessToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Config() driver.Config {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) GetAddition() driver.Additional {
|
2022-12-13 10:03:30 +00:00
|
|
|
return &d.Addition
|
2022-08-31 09:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 10:03:30 +00:00
|
|
|
func (d *PikPak) Init(ctx context.Context) error {
|
2022-08-31 09:32:57 +00:00
|
|
|
return d.login()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Drop(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
|
|
|
files, err := d.getFiles(dir.GetID())
|
|
|
|
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-08-31 09:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
|
|
|
var resp File
|
|
|
|
_, err := d.request(fmt.Sprintf("https://api-drive.mypikpak.com/drive/v1/files/%s?_magic=2021&thumbnail_size=SIZE_LARGE", file.GetID()),
|
|
|
|
http.MethodGet, nil, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
link := model.Link{
|
|
|
|
URL: resp.WebContentLink,
|
|
|
|
}
|
2023-07-11 05:40:58 +00:00
|
|
|
if !d.DisableMediaLink && len(resp.Medias) > 0 && resp.Medias[0].Link.Url != "" {
|
2022-08-31 09:32:57 +00:00
|
|
|
log.Debugln("use media link")
|
|
|
|
link.URL = resp.Medias[0].Link.Url
|
|
|
|
}
|
|
|
|
return &link, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
|
|
|
|
_, err := d.request("https://api-drive.mypikpak.com/drive/v1/files", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"kind": "drive#folder",
|
|
|
|
"parent_id": parentDir.GetID(),
|
|
|
|
"name": dirName,
|
|
|
|
})
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
_, err := d.request("https://api-drive.mypikpak.com/drive/v1/files:batchMove", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"ids": []string{srcObj.GetID()},
|
|
|
|
"to": base.Json{
|
|
|
|
"parent_id": dstDir.GetID(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
|
|
|
_, err := d.request("https://api-drive.mypikpak.com/drive/v1/files/"+srcObj.GetID(), http.MethodPatch, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"name": newName,
|
|
|
|
})
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
_, err := d.request("https://api-drive.mypikpak.com/drive/v1/files:batchCopy", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"ids": []string{srcObj.GetID()},
|
|
|
|
"to": base.Json{
|
|
|
|
"parent_id": dstDir.GetID(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Remove(ctx context.Context, obj model.Obj) error {
|
|
|
|
_, err := d.request("https://api-drive.mypikpak.com/drive/v1/files:batchTrash", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"ids": []string{obj.GetID()},
|
|
|
|
})
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *PikPak) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
2023-09-06 06:46:35 +00:00
|
|
|
hi := stream.GetHash()
|
|
|
|
sha1Str := hi.GetHash(hash_extend.GCID)
|
|
|
|
if len(sha1Str) < hash_extend.GCID.Width {
|
|
|
|
tFile, err := stream.CacheFullInTempFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sha1Str, err = utils.HashFile(hash_extend.GCID, tFile, stream.GetSize())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-31 09:32:57 +00:00
|
|
|
}
|
2023-09-06 06:46:35 +00:00
|
|
|
|
2023-07-11 14:19:21 +00:00
|
|
|
var resp UploadTaskData
|
2022-08-31 09:32:57 +00:00
|
|
|
res, err := d.request("https://api-drive.mypikpak.com/drive/v1/files", http.MethodPost, func(req *resty.Request) {
|
2023-07-11 14:19:21 +00:00
|
|
|
req.SetBody(base.Json{
|
|
|
|
"kind": "drive#file",
|
|
|
|
"name": stream.GetName(),
|
|
|
|
"size": stream.GetSize(),
|
|
|
|
"hash": strings.ToUpper(sha1Str),
|
|
|
|
"upload_type": "UPLOAD_TYPE_RESUMABLE",
|
|
|
|
"objProvider": base.Json{"provider": "UPLOAD_TYPE_UNKNOWN"},
|
|
|
|
"parent_id": dstDir.GetID(),
|
|
|
|
"folder_type": "NORMAL",
|
|
|
|
})
|
|
|
|
}, &resp)
|
2022-08-31 09:32:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-11 14:19:21 +00:00
|
|
|
|
|
|
|
// 秒传成功
|
|
|
|
if resp.Resumable == nil {
|
2022-09-10 05:25:52 +00:00
|
|
|
log.Debugln(string(res))
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-11 14:19:21 +00:00
|
|
|
|
|
|
|
params := resp.Resumable.Params
|
|
|
|
endpoint := strings.Join(strings.Split(params.Endpoint, ".")[1:], ".")
|
2022-08-31 09:32:57 +00:00
|
|
|
cfg := &aws.Config{
|
2023-07-11 14:19:21 +00:00
|
|
|
Credentials: credentials.NewStaticCredentials(params.AccessKeyID, params.AccessKeySecret, params.SecurityToken),
|
2022-08-31 09:32:57 +00:00
|
|
|
Region: aws.String("pikpak"),
|
|
|
|
Endpoint: &endpoint,
|
|
|
|
}
|
|
|
|
ss, err := session.NewSession(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uploader := s3manager.NewUploader(ss)
|
|
|
|
input := &s3manager.UploadInput{
|
2023-07-11 14:19:21 +00:00
|
|
|
Bucket: ¶ms.Bucket,
|
|
|
|
Key: ¶ms.Key,
|
2023-09-06 06:46:35 +00:00
|
|
|
Body: stream,
|
2022-08-31 09:32:57 +00:00
|
|
|
}
|
2022-12-21 07:03:09 +00:00
|
|
|
_, err = uploader.UploadWithContext(ctx, input)
|
2022-08-31 09:32:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ driver.Driver = (*PikPak)(nil)
|