2022-10-27 02:54:49 +00:00
|
|
|
package alist_v3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-20 08:15:52 +00:00
|
|
|
"errors"
|
2022-12-07 11:02:28 +00:00
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
2022-12-10 11:03:09 +00:00
|
|
|
"strings"
|
2022-10-27 02:54:49 +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/model"
|
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AListV3 struct {
|
|
|
|
model.Storage
|
|
|
|
Addition
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Config() driver.Config {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) GetAddition() driver.Additional {
|
2022-12-13 10:03:30 +00:00
|
|
|
return &d.Addition
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 10:03:30 +00:00
|
|
|
func (d *AListV3) Init(ctx context.Context) error {
|
2022-12-10 11:03:09 +00:00
|
|
|
d.Addition.Address = strings.TrimSuffix(d.Addition.Address, "/")
|
2022-10-27 02:54:49 +00:00
|
|
|
// TODO login / refresh token
|
|
|
|
//op.MustSaveDriverStorage(d)
|
2022-12-13 10:03:30 +00:00
|
|
|
return nil
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Drop(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
|
|
|
|
url := d.Address + "/api/fs/list"
|
2022-11-15 02:51:32 +00:00
|
|
|
var resp common.Resp[FsListResp]
|
2022-10-27 02:54:49 +00:00
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
2022-11-15 02:51:32 +00:00
|
|
|
SetBody(ListReq{
|
2022-11-28 05:45:25 +00:00
|
|
|
PageReq: model.PageReq{
|
2022-10-27 02:54:49 +00:00
|
|
|
Page: 1,
|
|
|
|
PerPage: 0,
|
|
|
|
},
|
|
|
|
Path: dir.GetPath(),
|
|
|
|
Password: d.Password,
|
|
|
|
Refresh: false,
|
|
|
|
}).Post(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-20 08:15:52 +00:00
|
|
|
if resp.Code != 200 {
|
|
|
|
return nil, errors.New(resp.Message)
|
|
|
|
}
|
2022-10-27 02:54:49 +00:00
|
|
|
var files []model.Obj
|
|
|
|
for _, f := range resp.Data.Content {
|
|
|
|
file := model.ObjThumb{
|
|
|
|
Object: model.Object{
|
|
|
|
Name: f.Name,
|
|
|
|
Modified: f.Modified,
|
|
|
|
Size: f.Size,
|
|
|
|
IsFolder: f.IsDir,
|
|
|
|
},
|
|
|
|
Thumbnail: model.Thumbnail{Thumbnail: f.Thumb},
|
|
|
|
}
|
|
|
|
files = append(files, &file)
|
|
|
|
}
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
|
|
|
url := d.Address + "/api/fs/get"
|
2022-11-15 02:51:32 +00:00
|
|
|
var resp common.Resp[FsGetResp]
|
2022-10-27 02:54:49 +00:00
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
2022-11-15 02:51:32 +00:00
|
|
|
SetBody(FsGetReq{
|
2022-10-27 02:54:49 +00:00
|
|
|
Path: file.GetPath(),
|
|
|
|
Password: d.Password,
|
|
|
|
}).Post(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-20 08:15:52 +00:00
|
|
|
if resp.Code != 200 {
|
|
|
|
return nil, errors.New(resp.Message)
|
|
|
|
}
|
2022-10-27 02:54:49 +00:00
|
|
|
return &model.Link{
|
|
|
|
URL: resp.Data.RawURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
|
2022-12-07 11:02:28 +00:00
|
|
|
url := d.Address + "/api/fs/mkdir"
|
|
|
|
var resp common.Resp[interface{}]
|
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
|
|
|
SetBody(MkdirOrLinkReq{
|
|
|
|
Path: path.Join(parentDir.GetPath(), dirName),
|
|
|
|
}).Post(url)
|
|
|
|
return checkResp(resp, err)
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
|
2022-12-07 11:02:28 +00:00
|
|
|
url := d.Address + "/api/fs/move"
|
|
|
|
var resp common.Resp[interface{}]
|
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
|
|
|
SetBody(MoveCopyReq{
|
2023-01-22 10:52:54 +00:00
|
|
|
SrcDir: path.Dir(srcObj.GetPath()),
|
2022-12-07 11:02:28 +00:00
|
|
|
DstDir: dstDir.GetPath(),
|
|
|
|
Names: []string{srcObj.GetName()},
|
|
|
|
}).Post(url)
|
|
|
|
return checkResp(resp, err)
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
2022-12-07 11:02:28 +00:00
|
|
|
url := d.Address + "/api/fs/rename"
|
|
|
|
var resp common.Resp[interface{}]
|
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
|
|
|
SetBody(RenameReq{
|
|
|
|
Path: srcObj.GetPath(),
|
|
|
|
Name: newName,
|
|
|
|
}).Post(url)
|
|
|
|
return checkResp(resp, err)
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
2022-12-07 11:02:28 +00:00
|
|
|
url := d.Address + "/api/fs/copy"
|
|
|
|
var resp common.Resp[interface{}]
|
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
|
|
|
SetBody(MoveCopyReq{
|
2023-01-22 10:52:54 +00:00
|
|
|
SrcDir: path.Dir(srcObj.GetPath()),
|
2022-12-07 11:02:28 +00:00
|
|
|
DstDir: dstDir.GetPath(),
|
|
|
|
Names: []string{srcObj.GetName()},
|
|
|
|
}).Post(url)
|
|
|
|
return checkResp(resp, err)
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Remove(ctx context.Context, obj model.Obj) error {
|
2022-12-07 11:02:28 +00:00
|
|
|
url := d.Address + "/api/fs/remove"
|
|
|
|
var resp common.Resp[interface{}]
|
|
|
|
_, err := base.RestyClient.R().
|
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
|
|
|
SetBody(RemoveReq{
|
2023-01-27 06:51:56 +00:00
|
|
|
Dir: path.Dir(obj.GetPath()),
|
2022-12-07 11:02:28 +00:00
|
|
|
Names: []string{obj.GetName()},
|
|
|
|
}).Post(url)
|
|
|
|
return checkResp(resp, err)
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AListV3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
2022-12-07 11:02:28 +00:00
|
|
|
url := d.Address + "/api/fs/put"
|
|
|
|
var resp common.Resp[interface{}]
|
|
|
|
fileBytes, err := io.ReadAll(stream.GetReadCloser())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-21 07:03:09 +00:00
|
|
|
_, err = base.RestyClient.R().SetContext(ctx).
|
2022-12-07 11:02:28 +00:00
|
|
|
SetResult(&resp).
|
|
|
|
SetHeader("Authorization", d.AccessToken).
|
|
|
|
SetHeader("File-Path", path.Join(dstDir.GetPath(), stream.GetName())).
|
|
|
|
SetHeader("Password", d.Password).
|
|
|
|
SetHeader("Content-Length", strconv.FormatInt(stream.GetSize(), 10)).
|
|
|
|
SetBody(fileBytes).Put(url)
|
|
|
|
return checkResp(resp, err)
|
2022-10-27 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//func (d *AList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
|
|
|
|
// return nil, errs.NotSupport
|
|
|
|
//}
|
|
|
|
|
|
|
|
var _ driver.Driver = (*AListV3)(nil)
|