alist/drivers/123/driver.go

348 lines
9.0 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
"encoding/hex"
"encoding/xml"
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/gin-gonic/gin"
2021-12-11 09:44:33 +00:00
jsoniter "github.com/json-iterator/go"
2021-11-27 11:40:36 +00:00
log "github.com/sirupsen/logrus"
2021-12-18 07:22:56 +00:00
"net/url"
2021-11-27 11:40:36 +00:00
"path/filepath"
2021-12-11 09:44:33 +00:00
"strconv"
"strings"
"time"
2021-11-27 11:40:36 +00:00
)
2021-12-08 14:58:44 +00:00
type Pan123 struct{}
2021-11-27 11:40:36 +00:00
2021-12-06 07:55:05 +00:00
func (driver Pan123) Config() base.DriverConfig {
return base.DriverConfig{
Name: "123Pan",
2021-11-29 08:42:46 +00:00
}
}
2021-12-06 07:55:05 +00:00
func (driver Pan123) Items() []base.Item {
return []base.Item{
2021-11-27 11:40:36 +00:00
{
Name: "username",
Label: "username",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 11:40:36 +00:00
Required: true,
Description: "account username/phone number",
},
{
Name: "password",
Label: "password",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 11:40:36 +00:00
Required: true,
Description: "account password",
},
{
Name: "root_folder",
Label: "root folder file_id",
2021-12-06 07:55:05 +00:00
Type: base.TypeString,
2021-11-27 11:40:36 +00:00
Required: false,
},
{
Name: "order_by",
Label: "order_by",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 11:40:36 +00:00
Values: "name,fileId,updateAt,createAt",
Required: true,
},
{
Name: "order_direction",
Label: "order_direction",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 11:40:36 +00:00
Values: "asc,desc",
Required: true,
},
}
}
func (driver Pan123) Save(account *model.Account, old *model.Account) error {
if account.RootFolder == "" {
account.RootFolder = "0"
}
err := driver.Login(account)
return err
}
func (driver Pan123) File(path string, account *model.Account) (*model.File, error) {
path = utils.ParsePath(path)
if path == "/" {
return &model.File{
Id: account.RootFolder,
Name: account.Name,
Size: 0,
Type: conf.FOLDER,
2021-11-30 01:37:51 +00:00
Driver: driver.Config().Name,
2021-11-27 11:40:36 +00:00
UpdatedAt: account.UpdatedAt,
}, nil
}
dir, name := filepath.Split(path)
files, err := driver.Files(dir, account)
if err != nil {
return nil, err
}
for _, file := range files {
if file.Name == name {
return &file, nil
}
}
2021-12-06 07:55:05 +00:00
return nil, base.ErrPathNotFound
2021-11-27 11:40:36 +00:00
}
func (driver Pan123) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
var rawFiles []Pan123File
2021-12-08 14:58:44 +00:00
cache, err := base.GetCache(path, account)
2021-11-27 11:40:36 +00:00
if err == nil {
rawFiles, _ = cache.([]Pan123File)
} else {
file, err := driver.File(path, account)
if err != nil {
return nil, err
}
rawFiles, err = driver.GetFiles(file.Id, account)
if err != nil {
return nil, err
}
if len(rawFiles) > 0 {
2021-12-08 14:58:44 +00:00
_ = base.SetCache(path, rawFiles, account)
2021-11-27 11:40:36 +00:00
}
}
files := make([]model.File, 0)
for _, file := range rawFiles {
files = append(files, *driver.FormatFile(&file))
}
return files, nil
}
2021-12-19 12:00:53 +00:00
func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, error) {
2021-12-19 12:32:47 +00:00
log.Debugf("%+v", args)
2021-12-19 12:00:53 +00:00
file, err := driver.GetFile(utils.ParsePath(args.Path), account)
2021-11-27 11:40:36 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-27 11:40:36 +00:00
}
var resp Pan123DownResp
2021-12-19 12:32:47 +00:00
var headers map[string]string
if args.IP != "" && args.IP != "::1" {
headers = map[string]string{
//"X-Real-IP": "1.1.1.1",
"X-Forwarded-For": args.IP,
}
}
2021-12-19 09:54:28 +00:00
data := base.Json{
"driveId": 0,
"etag": file.Etag,
"fileId": file.FileId,
"fileName": file.FileName,
"s3keyFlag": file.S3KeyFlag,
"size": file.Size,
"type": file.Type,
}
_, err = driver.Request("https://www.123pan.com/api/file/download_info",
2021-12-19 12:55:29 +00:00
base.Post, headers, nil, &data, &resp, false, account)
2021-12-19 09:54:28 +00:00
//_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
// SetBody().Post("https://www.123pan.com/api/file/download_info")
2021-11-27 11:40:36 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-27 11:40:36 +00:00
}
2021-12-08 14:58:44 +00:00
u, err := url.Parse(resp.Data.DownloadUrl)
2021-11-29 08:06:49 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-29 08:06:49 +00:00
}
2021-12-08 14:58:44 +00:00
u_ := fmt.Sprintf("https://%s%s", u.Host, u.Path)
2021-12-06 07:55:05 +00:00
res, err := base.NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
2021-11-29 08:06:49 +00:00
if err != nil {
2021-12-09 11:24:34 +00:00
return nil, err
2021-11-29 08:06:49 +00:00
}
log.Debug(res.String())
2021-12-19 09:54:28 +00:00
link := base.Link{
Url: resp.Data.DownloadUrl,
2021-11-29 08:06:49 +00:00
}
2021-12-19 12:00:53 +00:00
if res.StatusCode() == 302 {
link.Url = res.Header().Get("location")
}
2021-12-09 11:24:34 +00:00
return &link, nil
2021-11-27 11:40:36 +00:00
}
func (driver Pan123) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
log.Debugf("pan123 path: %s", path)
file, err := driver.File(path, account)
if err != nil {
return nil, nil, err
}
2021-12-09 11:24:34 +00:00
if !file.IsDir() {
2021-11-27 11:40:36 +00:00
return file, nil, nil
}
files, err := driver.Files(path, account)
if err != nil {
return nil, nil, err
}
return nil, files, nil
}
func (driver Pan123) Proxy(c *gin.Context, account *model.Account) {
c.Request.Header.Del("origin")
}
func (driver Pan123) Preview(path string, account *model.Account) (interface{}, error) {
2021-12-06 07:55:05 +00:00
return nil, base.ErrNotSupport
2021-12-05 07:22:19 +00:00
}
func (driver Pan123) MakeDir(path string, account *model.Account) error {
2021-12-11 09:44:33 +00:00
dir, name := filepath.Split(path)
parentFile, err := driver.File(dir, account)
if err != nil {
return err
}
if !parentFile.IsDir() {
return base.ErrNotFolder
}
parentFileId, _ := strconv.Atoi(parentFile.Id)
data := base.Json{
"driveId": 0,
"etag": "",
"fileName": name,
"parentFileId": parentFileId,
"size": 0,
"type": 1,
}
2021-12-19 09:54:28 +00:00
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
2021-12-19 12:32:47 +00:00
base.Post, nil, nil, &data, nil, false, account)
2021-12-11 09:44:33 +00:00
return err
2021-12-05 07:22:19 +00:00
}
func (driver Pan123) Move(src string, dst string, account *model.Account) error {
2022-01-03 12:06:36 +00:00
dstDir, _ := filepath.Split(dst)
2021-12-11 09:44:33 +00:00
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
fileId, _ := strconv.Atoi(srcFile.Id)
2022-01-03 12:06:36 +00:00
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
}
parentFileId, _ := strconv.Atoi(dstDirFile.Id)
data := base.Json{
"fileId": fileId,
"parentFileId": parentFileId,
2021-12-11 09:44:33 +00:00
}
2022-01-03 12:06:36 +00:00
_, err = driver.Request("https://www.123pan.com/api/file/mod_pid",
base.Post, nil, nil, &data, nil, false, account)
return err
}
func (driver Pan123) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
2021-12-11 09:44:33 +00:00
if err != nil {
2022-01-03 12:06:36 +00:00
return err
2021-12-11 09:44:33 +00:00
}
2022-01-03 12:06:36 +00:00
fileId, _ := strconv.Atoi(srcFile.Id)
data := base.Json{
"driveId": 0,
"fileId": fileId,
"fileName": dstName,
}
_, err = driver.Request("https://www.123pan.com/api/file/rename",
base.Post, nil, nil, &data, nil, false, account)
2021-12-11 09:44:33 +00:00
return err
2021-12-05 07:22:19 +00:00
}
func (driver Pan123) Copy(src string, dst string, account *model.Account) error {
2021-12-11 09:44:33 +00:00
return base.ErrNotSupport
2021-12-05 07:22:19 +00:00
}
func (driver Pan123) Delete(path string, account *model.Account) error {
2021-12-11 09:44:33 +00:00
file, err := driver.GetFile(path, account)
if err != nil {
return err
}
data := base.Json{
"driveId": 0,
"operation": true,
"fileTrashInfoList": file,
}
2021-12-19 09:54:28 +00:00
_, err = driver.Request("https://www.123pan.com/api/file/trash",
2021-12-19 12:32:47 +00:00
base.Post, nil, nil, &data, nil, false, account)
2021-12-11 09:44:33 +00:00
return err
}
type UploadResp struct {
XMLName xml.Name `xml:"InitiateMultipartUploadResult"`
Bucket string `xml:"Bucket"`
Key string `xml:"Key"`
UploadId string `xml:"UploadId"`
2021-12-05 07:22:19 +00:00
}
2021-12-11 09:44:33 +00:00
// TODO unfinished
2021-12-05 07:22:19 +00:00
func (driver Pan123) Upload(file *model.FileStream, account *model.Account) error {
2021-12-30 16:28:50 +00:00
return base.ErrNotImplement
2021-12-11 09:44:33 +00:00
parentFile, err := driver.File(file.ParentPath, account)
if err != nil {
return err
}
if !parentFile.IsDir() {
return base.ErrNotFolder
}
parentFileId, _ := strconv.Atoi(parentFile.Id)
data := base.Json{
"driveId": 0,
"duplicate": true,
"etag": RandStr(32), //maybe file's md5
"fileName": file.GetFileName(),
"parentFileId": parentFileId,
"size": file.GetSize(),
"type": 0,
}
2021-12-19 09:54:28 +00:00
res, err := driver.Request("https://www.123pan.com/api/file/upload_request",
2021-12-19 12:32:47 +00:00
base.Post, nil, nil, &data, nil, false, account)
2021-12-19 09:54:28 +00:00
//res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
2021-12-11 09:44:33 +00:00
if err != nil {
return err
}
baseUrl := fmt.Sprintf("https://file.123pan.com/%s/%s", jsoniter.Get(res, "data.Bucket").ToString(), jsoniter.Get(res, "data.Key").ToString())
var resp UploadResp
kSecret := jsoniter.Get(res, "data.SecretAccessKey").ToString()
nowTimeStr := time.Now().String()
2021-12-19 09:54:28 +00:00
Date := strings.ReplaceAll(strings.Split(nowTimeStr, "T")[0], "-", "")
2021-12-11 09:44:33 +00:00
StringToSign := fmt.Sprintf("%s\n%s\n%s\n%s",
"AWS4-HMAC-SHA256",
nowTimeStr,
fmt.Sprintf("%s/us-east-1/s3/aws4_request", Date),
2021-12-19 09:54:28 +00:00
)
2021-12-11 09:44:33 +00:00
kDate := HMAC("AWS4"+kSecret, Date)
kRegion := HMAC(kDate, "us-east-1")
kService := HMAC(kRegion, "s3")
kSigning := HMAC(kService, "aws4_request")
2021-12-19 09:54:28 +00:00
_, err = base.RestyClient.R().SetResult(&resp).SetHeaders(map[string]string{
2021-12-11 09:44:33 +00:00
"Authorization": fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=%s",
jsoniter.Get(res, "data.AccessKeyId"),
Date,
hex.EncodeToString([]byte(HMAC(StringToSign, kSigning)))),
"X-Amz-Content-Sha256": "UNSIGNED-PAYLOAD",
"X-Amz-Date": nowTimeStr,
"x-amz-security-token": jsoniter.Get(res, "data.SessionToken").ToString(),
}).Post(fmt.Sprintf("%s?uploads", baseUrl))
if err != nil {
return err
}
2021-12-06 07:55:05 +00:00
return base.ErrNotImplement
2021-11-27 11:40:36 +00:00
}
2021-12-08 14:58:44 +00:00
var _ base.Driver = (*Pan123)(nil)