alist/drivers/123/123.go

177 lines
4.3 KiB
Go
Raw Normal View History

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
"errors"
2021-11-27 11:40:36 +00:00
"fmt"
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"
2021-12-19 09:54:28 +00:00
log "github.com/sirupsen/logrus"
2021-11-27 11:40:36 +00:00
"path/filepath"
"strconv"
)
func (driver Pan123) Login(account *model.Account) error {
2021-12-19 09:54:28 +00:00
url := "https://www.123pan.com/api/user/sign_in"
if account.APIProxyUrl != "" {
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
}
2021-11-27 11:40:36 +00:00
var resp Pan123TokenResp
2021-12-19 09:54:28 +00:00
_, err := base.RestyClient.R().
2021-11-27 11:40:36 +00:00
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,
2021-12-19 09:54:28 +00:00
}).Post(url)
2021-11-27 11:40:36 +00:00
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 *File) *model.File {
2021-11-27 11:40:36 +00:00
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,
}
2022-04-08 13:51:21 +00:00
f.Type = file.GetType()
2021-11-27 11:40:36 +00:00
return f
}
func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]File, error) {
2021-11-27 11:40:36 +00:00
next := "0"
res := make([]File, 0)
2021-11-27 11:40:36 +00:00
for next != "-1" {
var resp Pan123Files
2021-12-19 09:54:28 +00:00
query := map[string]string{
"driveId": "0",
"limit": "100",
"next": next,
"orderBy": account.OrderBy,
"orderDirection": account.OrderDirection,
"parentFileId": parentId,
"trashed": "false",
}
_, err := driver.Request("https://www.123pan.com/api/file/list",
2021-12-19 12:32:47 +00:00
base.Get, nil, query, nil, &resp, false, account)
2021-11-27 11:40:36 +00:00
if err != nil {
return nil, err
}
next = resp.Data.Next
res = append(res, resp.Data.InfoList...)
}
return res, nil
}
2021-12-19 12:32:47 +00:00
func (driver Pan123) Request(url string, method int, headers, query map[string]string, data *base.Json, resp interface{}, proxy bool, account *model.Account) ([]byte, error) {
2021-12-19 09:54:28 +00:00
rawUrl := url
2021-12-19 12:55:29 +00:00
if account.APIProxyUrl != "" && proxy {
2021-12-19 09:54:28 +00:00
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
}
req := base.RestyClient.R()
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
2021-12-19 12:32:47 +00:00
if headers != nil {
req.SetHeaders(headers)
}
2021-12-19 09:54:28 +00:00
if query != nil {
req.SetQueryParams(query)
}
if data != nil {
req.SetBody(data)
}
if resp != nil {
req.SetResult(resp)
}
var res *resty.Response
var err error
switch method {
case base.Get:
res, err = req.Get(url)
case base.Post:
res, err = req.Post(url)
default:
return nil, base.ErrNotSupport
}
2021-12-11 09:44:33 +00:00
if err != nil {
return nil, err
}
2021-12-19 09:54:28 +00:00
log.Debug(res.String())
2021-12-11 09:44:33 +00:00
body := res.Body()
2021-12-19 09:54:28 +00:00
code := jsoniter.Get(body, "code").ToInt()
if code != 0 {
if code == 401 {
err := driver.Login(account)
if err != nil {
return nil, err
}
2021-12-19 12:32:47 +00:00
return driver.Request(rawUrl, method, headers, query, data, resp, proxy, account)
2021-12-19 09:54:28 +00:00
}
2021-12-11 09:44:33 +00:00
return nil, errors.New(jsoniter.Get(body, "message").ToString())
}
return body, nil
}
2021-12-19 09:54:28 +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
//}
func (driver Pan123) GetFile(path string, account *model.Account) (*File, error) {
2021-11-27 11:40:36 +00:00
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)
parentFiles, _ := parentFiles_.([]File)
2021-11-27 11:40:36 +00:00
for _, file := range parentFiles {
if file.FileName == name {
//if file.Type != conf.FOLDER {
// return &file, err
//} else {
// return nil, base.ErrNotFile
//}
return &file, nil
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
}
2022-01-03 14:29:32 +00:00
//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-12-11 09:44:33 +00:00
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
}