2021-12-06 07:55:05 +00:00
|
|
|
package _23
|
2021-11-27 11:40:36 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-11 09:44:33 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
2021-11-27 11:40:36 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/Xhofe/alist/conf"
|
2021-12-06 07:55:05 +00:00
|
|
|
"github.com/Xhofe/alist/drivers/base"
|
2021-11-27 11:40:36 +00:00
|
|
|
"github.com/Xhofe/alist/model"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
|
|
|
"github.com/go-resty/resty/v2"
|
2021-12-11 09:44:33 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"math/rand"
|
2021-11-27 11:40:36 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var pan123Client = resty.New()
|
|
|
|
|
2021-12-18 07:22:56 +00:00
|
|
|
type BaseResp struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:40:36 +00:00
|
|
|
type Pan123TokenResp struct {
|
2021-12-18 07:22:56 +00:00
|
|
|
BaseResp
|
2021-11-27 11:40:36 +00:00
|
|
|
Data struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pan123File struct {
|
|
|
|
FileName string `json:"FileName"`
|
|
|
|
Size int64 `json:"Size"`
|
|
|
|
UpdateAt *time.Time `json:"UpdateAt"`
|
|
|
|
FileId int64 `json:"FileId"`
|
|
|
|
Type int `json:"Type"`
|
|
|
|
Etag string `json:"Etag"`
|
|
|
|
S3KeyFlag string `json:"S3KeyFlag"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pan123Files struct {
|
2021-12-18 07:22:56 +00:00
|
|
|
BaseResp
|
2021-11-27 11:40:36 +00:00
|
|
|
Data struct {
|
|
|
|
InfoList []Pan123File `json:"InfoList"`
|
|
|
|
Next string `json:"Next"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pan123DownResp struct {
|
2021-12-18 07:22:56 +00:00
|
|
|
BaseResp
|
2021-11-27 11:40:36 +00:00
|
|
|
Data struct {
|
|
|
|
DownloadUrl string `json:"DownloadUrl"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver Pan123) Login(account *model.Account) error {
|
|
|
|
var resp Pan123TokenResp
|
|
|
|
_, err := pan123Client.R().
|
|
|
|
SetResult(&resp).
|
2021-12-06 07:55:05 +00:00
|
|
|
SetBody(base.Json{
|
2021-11-27 11:40:36 +00:00
|
|
|
"passport": account.Username,
|
|
|
|
"password": account.Password,
|
|
|
|
}).Post("https://www.123pan.com/api/user/sign_in")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
err = fmt.Errorf(resp.Message)
|
|
|
|
account.Status = resp.Message
|
|
|
|
} else {
|
|
|
|
account.Status = "work"
|
|
|
|
account.AccessToken = resp.Data.Token
|
|
|
|
}
|
|
|
|
_ = model.SaveAccount(account)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver Pan123) FormatFile(file *Pan123File) *model.File {
|
|
|
|
f := &model.File{
|
|
|
|
Id: strconv.FormatInt(file.FileId, 10),
|
|
|
|
Name: file.FileName,
|
|
|
|
Size: file.Size,
|
2021-11-30 01:37:51 +00:00
|
|
|
Driver: driver.Config().Name,
|
2021-11-27 11:40:36 +00:00
|
|
|
UpdatedAt: file.UpdateAt,
|
|
|
|
}
|
|
|
|
if file.Type == 1 {
|
|
|
|
f.Type = conf.FOLDER
|
|
|
|
} else {
|
|
|
|
f.Type = utils.GetFileType(filepath.Ext(file.FileName))
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123File, error) {
|
|
|
|
next := "0"
|
|
|
|
res := make([]Pan123File, 0)
|
|
|
|
for next != "-1" {
|
|
|
|
var resp Pan123Files
|
|
|
|
_, err := pan123Client.R().SetResult(&resp).
|
|
|
|
SetHeader("authorization", "Bearer "+account.AccessToken).
|
|
|
|
SetQueryParams(map[string]string{
|
|
|
|
"driveId": "0",
|
|
|
|
"limit": "100",
|
|
|
|
"next": next,
|
|
|
|
"orderBy": account.OrderBy,
|
|
|
|
"orderDirection": account.OrderDirection,
|
|
|
|
"parentFileId": parentId,
|
|
|
|
"trashed": "false",
|
|
|
|
}).Get("https://www.123pan.com/api/file/list")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Code != 0 {
|
|
|
|
if resp.Code == 401 {
|
|
|
|
err := driver.Login(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return driver.GetFiles(parentId, account)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf(resp.Message)
|
|
|
|
}
|
|
|
|
next = resp.Data.Next
|
|
|
|
res = append(res, resp.Data.InfoList...)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 09:44:33 +00:00
|
|
|
func (driver Pan123) Post(url string, data base.Json, account *model.Account) ([]byte, error) {
|
|
|
|
res, err := pan123Client.R().
|
|
|
|
SetHeader("authorization", "Bearer "+account.AccessToken).
|
|
|
|
SetBody(data).Post(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body := res.Body()
|
|
|
|
if jsoniter.Get(body, "code").ToInt() != 0 {
|
|
|
|
return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:40:36 +00:00
|
|
|
func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
|
|
|
dir, name := filepath.Split(path)
|
2021-11-28 07:10:06 +00:00
|
|
|
dir = utils.ParsePath(dir)
|
2021-11-27 11:40:36 +00:00
|
|
|
_, err := driver.Files(dir, account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-08 14:58:44 +00:00
|
|
|
parentFiles_, _ := base.GetCache(dir, account)
|
2021-11-27 11:40:36 +00:00
|
|
|
parentFiles, _ := parentFiles_.([]Pan123File)
|
|
|
|
for _, file := range parentFiles {
|
|
|
|
if file.FileName == name {
|
2021-11-28 07:10:06 +00:00
|
|
|
if file.Type != conf.FOLDER {
|
2021-11-27 11:40:36 +00:00
|
|
|
return &file, err
|
|
|
|
} else {
|
2021-12-06 07:55:05 +00:00
|
|
|
return nil, base.ErrNotFile
|
2021-11-27 11:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 07:55:05 +00:00
|
|
|
return nil, base.ErrPathNotFound
|
2021-11-27 11:40:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 09:44:33 +00:00
|
|
|
func RandStr(length int) string {
|
|
|
|
str := "123456789abcdefghijklmnopqrstuvwxyz"
|
|
|
|
bytes := []byte(str)
|
|
|
|
var result []byte
|
|
|
|
rand.Seed(time.Now().UnixNano()+ int64(rand.Intn(100)))
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
result = append(result, bytes[rand.Intn(len(bytes))])
|
|
|
|
}
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func HMAC(message string, secret string) string {
|
|
|
|
key := []byte(secret)
|
|
|
|
h := hmac.New(sha256.New, key)
|
|
|
|
h.Write([]byte(message))
|
|
|
|
// fmt.Println(h.Sum(nil))
|
|
|
|
//sha := hex.EncodeToString(h.Sum(nil))
|
|
|
|
// fmt.Println(sha)
|
|
|
|
//return sha
|
|
|
|
return string(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:40:36 +00:00
|
|
|
func init() {
|
2021-12-06 07:55:05 +00:00
|
|
|
base.RegisterDriver(&Pan123{})
|
2021-11-27 11:40:36 +00:00
|
|
|
pan123Client.SetRetryCount(3)
|
|
|
|
}
|