alist/drivers/189/driver.go

429 lines
10 KiB
Go
Raw Normal View History

2021-12-06 07:55:05 +00:00
package _89
2021-11-27 11:40:36 +00:00
import (
2021-12-10 14:25:09 +00:00
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/hex"
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-10 14:25:09 +00:00
jsoniter "github.com/json-iterator/go"
2021-11-27 11:40:36 +00:00
log "github.com/sirupsen/logrus"
2021-12-10 14:25:09 +00:00
"io"
"math"
"net/http"
2021-11-27 11:40:36 +00:00
"path/filepath"
2021-12-10 14:25:09 +00:00
"strconv"
"strings"
2021-11-27 11:40:36 +00:00
)
2021-12-10 14:25:09 +00:00
type Cloud189 struct{}
2021-11-27 11:40:36 +00:00
2021-12-06 07:55:05 +00:00
func (driver Cloud189) Config() base.DriverConfig {
return base.DriverConfig{
Name: "189Cloud",
2021-11-29 08:42:46 +00:00
}
}
2021-12-06 07:55:05 +00:00
func (driver Cloud189) 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: true,
},
//{
// Name: "internal_type",
// Label: "189cloud type",
// Type: base.TypeSelect,
// Required: true,
// Values: "Personal,Family",
//},
//{
// Name: "site_id",
// Label: "family id",
// Type: base.TypeString,
//},
2021-11-27 11:40:36 +00:00
{
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,size,lastOpTime,createdDate",
Required: true,
},
{
Name: "order_direction",
Label: "desc",
2021-12-06 07:55:05 +00:00
Type: base.TypeSelect,
2021-11-27 11:40:36 +00:00
Values: "true,false",
Required: true,
},
}
}
func (driver Cloud189) Save(account *model.Account, old *model.Account) error {
if old != nil && old.Name != account.Name {
delete(client189Map, old.Name)
}
if err := driver.Login(account); err != nil {
account.Status = err.Error()
_ = model.SaveAccount(account)
return err
}
account.Status = "work"
err := model.SaveAccount(account)
if err != nil {
return err
}
return nil
}
func (driver Cloud189) 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 Cloud189) Files(path string, account *model.Account) ([]model.File, error) {
path = utils.ParsePath(path)
var rawFiles []Cloud189File
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.([]Cloud189File)
} 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 Cloud189) Link(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(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
}
if file.Type == conf.FOLDER {
2021-12-09 11:24:34 +00:00
return nil, base.ErrNotFile
2021-11-27 11:40:36 +00:00
}
var resp Cloud189Down
u := "https://cloud.189.cn/api/open/file/getFileDownloadUrl.action"
body, err := driver.Request(u, base.Get, map[string]string{
"fileId": file.Id,
}, nil, nil, 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
}
err = utils.Json.Unmarshal(body, &resp)
if err != nil {
return nil, err
2021-11-27 11:40:36 +00:00
}
if resp.ResCode != 0 {
2021-12-09 11:24:34 +00:00
return nil, fmt.Errorf(resp.ResMessage)
2021-11-27 11:40:36 +00:00
}
2021-12-06 07:55:05 +00:00
res, err := base.NoRedirectClient.R().Get(resp.FileDownloadUrl)
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-09 11:24:34 +00:00
link := base.Link{}
2021-11-27 11:40:36 +00:00
if res.StatusCode() == 302 {
2021-12-09 11:24:34 +00:00
link.Url = res.Header().Get("location")
2021-12-10 14:25:09 +00:00
} else {
2021-12-09 11:24:34 +00:00
link.Url = resp.FileDownloadUrl
2021-11-27 11:40:36 +00:00
}
2021-12-09 11:24:34 +00:00
return &link, nil
2021-11-27 11:40:36 +00:00
}
func (driver Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
log.Debugf("189 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 Cloud189) Proxy(ctx *gin.Context, account *model.Account) {
ctx.Request.Header.Del("Origin")
}
func (driver Cloud189) 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 Cloud189) MakeDir(path string, account *model.Account) error {
2021-12-10 14:25:09 +00:00
dir, name := filepath.Split(path)
parent, err := driver.File(dir, account)
if err != nil {
return err
}
if !parent.IsDir() {
return base.ErrNotFolder
}
form := map[string]string{
"parentFolderId": parent.Id,
"folderName": name,
}
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", base.Post, nil, form, nil, account)
2021-12-10 14:25:09 +00:00
return err
2021-12-05 07:22:19 +00:00
}
func (driver Cloud189) Move(src string, dst string, account *model.Account) error {
2021-12-10 14:25:09 +00:00
dstDir, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
2022-01-03 12:06:36 +00:00
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
2021-12-10 14:25:09 +00:00
}
2022-01-03 12:06:36 +00:00
isFolder := 0
if srcFile.IsDir() {
isFolder = 1
2021-12-10 14:25:09 +00:00
}
2022-01-03 12:06:36 +00:00
taskInfos := []base.Json{
{
"fileId": srcFile.Id,
"fileName": dstName,
"isFolder": isFolder,
},
}
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
2022-01-03 12:06:36 +00:00
if err != nil {
return err
}
form := map[string]string{
"type": "MOVE",
"targetFolderId": dstDirFile.Id,
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", base.Post, nil, form, nil, account)
2022-01-03 12:06:36 +00:00
return err
}
func (driver Cloud189) Rename(src string, dst string, account *model.Account) error {
_, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
url := "https://cloud.189.cn/api/open/file/renameFile.action"
idKey := "fileId"
nameKey := "destFileName"
if srcFile.IsDir() {
url = "https://cloud.189.cn/api/open/file/renameFolder.action"
idKey = "folderId"
nameKey = "destFolderName"
}
form := map[string]string{
idKey: srcFile.Id,
nameKey: dstName,
}
_, err = driver.Request(url, base.Post, nil, form, nil, account)
2021-12-10 14:25:09 +00:00
return err
2021-12-05 07:22:19 +00:00
}
func (driver Cloud189) Copy(src string, dst string, account *model.Account) error {
2021-12-10 14:25:09 +00:00
dstDir, dstName := filepath.Split(dst)
srcFile, err := driver.File(src, account)
if err != nil {
return err
}
dstDirFile, err := driver.File(dstDir, account)
if err != nil {
return err
}
isFolder := 0
if srcFile.IsDir() {
isFolder = 1
}
taskInfos := []base.Json{
{
"fileId": srcFile.Id,
"fileName": dstName,
"isFolder": isFolder,
},
}
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
2021-12-10 14:25:09 +00:00
if err != nil {
return err
}
form := map[string]string{
"type": "COPY",
"targetFolderId": dstDirFile.Id,
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", base.Post, nil, form, nil, account)
2021-12-10 14:25:09 +00:00
return err
2021-12-05 07:22:19 +00:00
}
func (driver Cloud189) Delete(path string, account *model.Account) error {
2021-12-10 14:25:09 +00:00
path = utils.ParsePath(path)
file, err := driver.File(path, account)
if err != nil {
return err
}
isFolder := 0
if file.IsDir() {
isFolder = 1
}
taskInfos := []base.Json{
{
"fileId": file.Id,
"fileName": file.Name,
"isFolder": isFolder,
},
}
taskInfosBytes, err := utils.Json.Marshal(taskInfos)
2021-12-10 14:25:09 +00:00
if err != nil {
return err
}
form := map[string]string{
"type": "DELETE",
"targetFolderId": "",
"taskInfos": string(taskInfosBytes),
}
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", base.Post, nil, form, nil, account)
2021-12-10 14:25:09 +00:00
return err
2021-12-05 07:22:19 +00:00
}
2021-12-10 14:25:09 +00:00
// Upload Error: decrypt encryptionText failed
2021-12-05 07:22:19 +00:00
func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) error {
2021-12-30 16:28:50 +00:00
return base.ErrNotImplement
2021-12-10 14:25:09 +00:00
const DEFAULT uint64 = 10485760
var count = int64(math.Ceil(float64(file.GetSize()) / float64(DEFAULT)))
var finish uint64 = 0
parentFile, err := driver.File(file.ParentPath, account)
if err != nil {
return err
}
if !parentFile.IsDir() {
return base.ErrNotFolder
}
res, err := driver.UploadRequest("/person/initMultiUpload", map[string]string{
"parentFolderId": parentFile.Id,
2021-12-19 12:00:53 +00:00
"fileName": file.Name,
"fileSize": strconv.FormatInt(int64(file.Size), 10),
"sliceSize": strconv.FormatInt(int64(DEFAULT), 10),
"lazyCheck": "1",
}, account)
2021-12-10 14:25:09 +00:00
if err != nil {
return err
}
uploadFileId := jsoniter.Get(res, "data.uploadFileId").ToString()
var i int64
var byteSize uint64
md5s := make([]string, 0)
md5Sum := md5.New()
for i = 1; i <= count; i++ {
byteSize = file.GetSize() - finish
if DEFAULT < byteSize {
byteSize = DEFAULT
}
log.Debugf("%d,%d", byteSize, finish)
byteData := make([]byte, byteSize)
n, err := io.ReadFull(file, byteData)
log.Debug(err, n)
if err != nil {
return err
}
finish += uint64(n)
md5Bytes := getMd5(byteData)
md5Str := hex.EncodeToString(md5Bytes)
md5Base64 := base64.StdEncoding.EncodeToString(md5Bytes)
md5s = append(md5s, md5Str)
md5Sum.Write(byteData)
res, err = driver.UploadRequest("/person/getMultiUploadUrls", map[string]string{
2021-12-19 12:00:53 +00:00
"partInfo": fmt.Sprintf("%s-%s", strconv.FormatInt(i, 10), md5Base64),
2021-12-10 14:25:09 +00:00
"uploadFileId": uploadFileId,
2021-12-19 12:00:53 +00:00
}, account)
2021-12-10 14:25:09 +00:00
if err != nil {
return err
}
2021-12-19 12:00:53 +00:00
uploadData := jsoniter.Get(res, "uploadUrls.partNumber_"+strconv.FormatInt(i, 10))
headers := strings.Split(uploadData.Get("requestHeader").ToString(), "&")
2021-12-10 14:25:09 +00:00
req, err := http.NewRequest("PUT", uploadData.Get("requestURL").ToString(), bytes.NewBuffer(byteData))
if err != nil {
return err
}
2021-12-19 12:00:53 +00:00
for _, header := range headers {
2021-12-10 14:25:09 +00:00
kv := strings.Split(header, "=")
2021-12-19 12:00:53 +00:00
req.Header.Set(kv[0], strings.Join(kv[1:], "="))
2021-12-10 14:25:09 +00:00
}
res, err := base.HttpClient.Do(req)
if err != nil {
return err
}
log.Debugf("%+v", res)
}
id := md5Sum.Sum(nil)
2021-12-19 12:00:53 +00:00
res, err = driver.UploadRequest("/person/commitMultiUploadFile", map[string]string{
2021-12-10 14:25:09 +00:00
"uploadFileId": uploadFileId,
2021-12-19 12:00:53 +00:00
"fileMd5": hex.EncodeToString(id),
"sliceMd5": utils.GetMD5Encode(strings.Join(md5s, "\n")),
"lazyCheck": "1",
}, account)
2021-12-10 14:25:09 +00:00
return err
2021-11-27 11:40:36 +00:00
}
2021-12-10 14:25:09 +00:00
var _ base.Driver = (*Cloud189)(nil)