2023-02-23 12:45:57 +00:00
|
|
|
package aliyundrive_open
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-01 13:50:25 +00:00
|
|
|
"errors"
|
2023-07-05 05:51:46 +00:00
|
|
|
"fmt"
|
2023-02-23 12:45:57 +00:00
|
|
|
"net/http"
|
2023-04-01 06:54:29 +00:00
|
|
|
"time"
|
2023-02-23 12:45:57 +00:00
|
|
|
|
2023-07-28 07:55:39 +00:00
|
|
|
"github.com/Xhofe/rateg"
|
2023-02-23 12:45:57 +00:00
|
|
|
"github.com/alist-org/alist/v3/drivers/base"
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AliyundriveOpen struct {
|
|
|
|
model.Storage
|
|
|
|
Addition
|
|
|
|
|
2023-03-20 07:00:02 +00:00
|
|
|
DriveId string
|
2023-05-02 14:06:03 +00:00
|
|
|
|
2023-06-11 10:08:59 +00:00
|
|
|
limitList func(ctx context.Context, data base.Json) (*Files, error)
|
2023-05-02 14:06:03 +00:00
|
|
|
limitLink func(ctx context.Context, file model.Obj) (*model.Link, error)
|
2025-01-18 15:26:58 +00:00
|
|
|
ref *AliyundriveOpen
|
2023-02-23 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AliyundriveOpen) Config() driver.Config {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AliyundriveOpen) GetAddition() driver.Additional {
|
|
|
|
return &d.Addition
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AliyundriveOpen) Init(ctx context.Context) error {
|
2023-08-01 13:50:25 +00:00
|
|
|
if d.LIVPDownloadFormat == "" {
|
|
|
|
d.LIVPDownloadFormat = "jpeg"
|
|
|
|
}
|
2023-08-02 07:50:01 +00:00
|
|
|
if d.DriveType == "" {
|
|
|
|
d.DriveType = "default"
|
|
|
|
}
|
2023-02-23 12:45:57 +00:00
|
|
|
res, err := d.request("/adrive/v1.0/user/getDriveInfo", http.MethodPost, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-02 07:50:01 +00:00
|
|
|
d.DriveId = utils.Json.Get(res, d.DriveType+"_drive_id").ToString()
|
2023-07-28 07:55:39 +00:00
|
|
|
d.limitList = rateg.LimitFnCtx(d.list, rateg.LimitFnOption{
|
|
|
|
Limit: 4,
|
|
|
|
Bucket: 1,
|
|
|
|
})
|
|
|
|
d.limitLink = rateg.LimitFnCtx(d.link, rateg.LimitFnOption{
|
|
|
|
Limit: 1,
|
|
|
|
Bucket: 1,
|
|
|
|
})
|
2023-02-23 12:45:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-18 15:26:58 +00:00
|
|
|
func (d *AliyundriveOpen) InitReference(storage driver.Driver) error {
|
|
|
|
refStorage, ok := storage.(*AliyundriveOpen)
|
|
|
|
if ok {
|
|
|
|
d.ref = refStorage
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errs.NotSupport
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:45:57 +00:00
|
|
|
func (d *AliyundriveOpen) Drop(ctx context.Context) error {
|
2025-01-18 15:26:58 +00:00
|
|
|
d.ref = nil
|
2023-02-23 12:45:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-11 10:08:59 +00:00
|
|
|
func (d *AliyundriveOpen) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
2023-07-05 05:51:46 +00:00
|
|
|
if d.limitList == nil {
|
|
|
|
return nil, fmt.Errorf("driver not init")
|
|
|
|
}
|
2023-06-11 10:08:59 +00:00
|
|
|
files, err := d.getFiles(ctx, dir.GetID())
|
2023-02-23 12:45:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return utils.SliceConvert(files, func(src File) (model.Obj, error) {
|
|
|
|
return fileToObj(src), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:06:03 +00:00
|
|
|
func (d *AliyundriveOpen) link(ctx context.Context, file model.Obj) (*model.Link, error) {
|
2023-02-23 12:45:57 +00:00
|
|
|
res, err := d.request("/adrive/v1.0/openFile/getDownloadUrl", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
2023-02-25 09:54:36 +00:00
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"file_id": file.GetID(),
|
2023-12-10 05:17:56 +00:00
|
|
|
"expire_sec": 14400,
|
2023-02-23 12:45:57 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url := utils.Json.Get(res, "url").ToString()
|
2023-08-01 13:50:25 +00:00
|
|
|
if url == "" {
|
|
|
|
if utils.Ext(file.GetName()) != "livp" {
|
|
|
|
return nil, errors.New("get download url failed: " + string(res))
|
|
|
|
}
|
|
|
|
url = utils.Json.Get(res, "streamsUrl", d.LIVPDownloadFormat).ToString()
|
|
|
|
}
|
2023-11-23 13:49:16 +00:00
|
|
|
exp := time.Minute
|
2023-02-23 12:45:57 +00:00
|
|
|
return &model.Link{
|
2023-05-02 08:12:40 +00:00
|
|
|
URL: url,
|
|
|
|
Expiration: &exp,
|
2023-02-23 12:45:57 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:06:03 +00:00
|
|
|
func (d *AliyundriveOpen) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
2023-07-05 05:51:46 +00:00
|
|
|
if d.limitLink == nil {
|
|
|
|
return nil, fmt.Errorf("driver not init")
|
|
|
|
}
|
2023-05-02 14:06:03 +00:00
|
|
|
return d.limitLink(ctx, file)
|
|
|
|
}
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
func (d *AliyundriveOpen) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
|
|
|
|
nowTime, _ := getNowTime()
|
|
|
|
newDir := File{CreatedAt: nowTime, UpdatedAt: nowTime}
|
2023-02-23 12:45:57 +00:00
|
|
|
_, err := d.request("/adrive/v1.0/openFile/create", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"parent_file_id": parentDir.GetID(),
|
|
|
|
"name": dirName,
|
|
|
|
"type": "folder",
|
|
|
|
"check_name_mode": "refuse",
|
2023-08-09 08:13:09 +00:00
|
|
|
}).SetResult(&newDir)
|
2023-02-23 12:45:57 +00:00
|
|
|
})
|
2023-08-09 08:13:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fileToObj(newDir), nil
|
2023-02-23 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
func (d *AliyundriveOpen) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
|
|
|
|
var resp MoveOrCopyResp
|
2023-02-23 12:45:57 +00:00
|
|
|
_, err := d.request("/adrive/v1.0/openFile/move", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"file_id": srcObj.GetID(),
|
|
|
|
"to_parent_file_id": dstDir.GetID(),
|
|
|
|
"check_name_mode": "refuse", // optional:ignore,auto_rename,refuse
|
|
|
|
//"new_name": "newName", // The new name to use when a file of the same name exists
|
2023-08-09 08:13:09 +00:00
|
|
|
}).SetResult(&resp)
|
2023-02-23 12:45:57 +00:00
|
|
|
})
|
2023-08-09 08:13:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Exist {
|
|
|
|
return nil, errors.New("existence of files with the same name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if srcObj, ok := srcObj.(*model.ObjThumb); ok {
|
|
|
|
srcObj.ID = resp.FileID
|
|
|
|
srcObj.Modified = time.Now()
|
|
|
|
return srcObj, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2023-02-23 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
func (d *AliyundriveOpen) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
|
|
|
|
var newFile File
|
2023-02-23 12:45:57 +00:00
|
|
|
_, err := d.request("/adrive/v1.0/openFile/update", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"file_id": srcObj.GetID(),
|
|
|
|
"name": newName,
|
2023-08-09 08:13:09 +00:00
|
|
|
}).SetResult(&newFile)
|
2023-02-23 12:45:57 +00:00
|
|
|
})
|
2023-08-09 08:13:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fileToObj(newFile), nil
|
2023-02-23 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AliyundriveOpen) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
|
|
|
_, err := d.request("/adrive/v1.0/openFile/copy", http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(base.Json{
|
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"file_id": srcObj.GetID(),
|
|
|
|
"to_parent_file_id": dstDir.GetID(),
|
|
|
|
"auto_rename": true,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AliyundriveOpen) Remove(ctx context.Context, obj model.Obj) error {
|
2023-03-08 11:19:13 +00:00
|
|
|
uri := "/adrive/v1.0/openFile/recyclebin/trash"
|
|
|
|
if d.RemoveWay == "delete" {
|
|
|
|
uri = "/adrive/v1.0/openFile/delete"
|
|
|
|
}
|
|
|
|
_, err := d.request(uri, http.MethodPost, func(req *resty.Request) {
|
2023-02-23 12:45:57 +00:00
|
|
|
req.SetBody(base.Json{
|
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"file_id": obj.GetID(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-09 08:13:09 +00:00
|
|
|
func (d *AliyundriveOpen) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
|
2023-07-15 11:33:46 +00:00
|
|
|
return d.upload(ctx, dstDir, stream, up)
|
2023-02-23 12:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AliyundriveOpen) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
|
|
|
|
var resp base.Json
|
|
|
|
var uri string
|
|
|
|
data := base.Json{
|
|
|
|
"drive_id": d.DriveId,
|
|
|
|
"file_id": args.Obj.GetID(),
|
|
|
|
}
|
|
|
|
switch args.Method {
|
|
|
|
case "video_preview":
|
|
|
|
uri = "/adrive/v1.0/openFile/getVideoPreviewPlayInfo"
|
|
|
|
data["category"] = "live_transcoding"
|
2023-12-10 05:17:56 +00:00
|
|
|
data["url_expire_sec"] = 14400
|
2023-02-23 12:45:57 +00:00
|
|
|
default:
|
|
|
|
return nil, errs.NotSupport
|
|
|
|
}
|
|
|
|
_, err := d.request(uri, http.MethodPost, func(req *resty.Request) {
|
|
|
|
req.SetBody(data).SetResult(&resp)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ driver.Driver = (*AliyundriveOpen)(nil)
|
2023-08-09 08:13:09 +00:00
|
|
|
var _ driver.MkdirResult = (*AliyundriveOpen)(nil)
|
|
|
|
var _ driver.MoveResult = (*AliyundriveOpen)(nil)
|
|
|
|
var _ driver.RenameResult = (*AliyundriveOpen)(nil)
|
|
|
|
var _ driver.PutResult = (*AliyundriveOpen)(nil)
|