diff --git a/drivers/alist_v2/driver.go b/drivers/alist_v2/driver.go new file mode 100644 index 00000000..61290b57 --- /dev/null +++ b/drivers/alist_v2/driver.go @@ -0,0 +1,126 @@ +package alist_v2 + +import ( + "context" + + "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/alist-org/alist/v3/server/common" +) + +type AListV2 struct { + model.Storage + Addition +} + +func (d *AListV2) Config() driver.Config { + return config +} + +func (d *AListV2) GetAddition() driver.Additional { + return d.Addition +} + +func (d *AListV2) Init(ctx context.Context, storage model.Storage) error { + d.Storage = storage + err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) + if err != nil { + return err + } + // TODO login / refresh token + //op.MustSaveDriverStorage(d) + return err +} + +func (d *AListV2) Drop(ctx context.Context) error { + return nil +} + +func (d *AListV2) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { + url := d.Address + "/api/public/path" + var resp common.Resp[PathResp] + _, err := base.RestyClient.R(). + SetResult(&resp). + SetHeader("Authorization", d.AccessToken). + SetBody(PathReq{ + PageNum: 0, + PageSize: 0, + Path: dir.GetPath(), + Password: d.Password, + }).Post(url) + if err != nil { + return nil, err + } + var files []model.Obj + for _, f := range resp.Data.Files { + file := model.ObjThumb{ + Object: model.Object{ + Name: f.Name, + Modified: *f.UpdatedAt, + Size: f.Size, + IsFolder: f.Type == 1, + }, + Thumbnail: model.Thumbnail{Thumbnail: f.Thumbnail}, + } + files = append(files, &file) + } + return files, nil +} + +//func (d *AList) Get(ctx context.Context, path string) (model.Obj, error) { +// // this is optional +// return nil, errs.NotImplement +//} + +func (d *AListV2) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { + url := d.Address + "/api/public/path" + var resp common.Resp[PathResp] + _, err := base.RestyClient.R(). + SetResult(&resp). + SetHeader("Authorization", d.AccessToken). + SetBody(PathReq{ + PageNum: 0, + PageSize: 0, + Path: file.GetPath(), + Password: d.Password, + }).Post(url) + if err != nil { + return nil, err + } + return &model.Link{ + URL: resp.Data.Files[0].Url, + }, nil +} + +func (d *AListV2) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error { + return errs.NotImplement +} + +func (d *AListV2) Move(ctx context.Context, srcObj, dstDir model.Obj) error { + return errs.NotImplement +} + +func (d *AListV2) Rename(ctx context.Context, srcObj model.Obj, newName string) error { + return errs.NotImplement +} + +func (d *AListV2) Copy(ctx context.Context, srcObj, dstDir model.Obj) error { + return errs.NotImplement +} + +func (d *AListV2) Remove(ctx context.Context, obj model.Obj) error { + return errs.NotImplement +} + +func (d *AListV2) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error { + return errs.NotImplement +} + +//func (d *AList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) { +// return nil, errs.NotSupport +//} + +var _ driver.Driver = (*AListV2)(nil) diff --git a/drivers/alist_v2/meta.go b/drivers/alist_v2/meta.go new file mode 100644 index 00000000..209fc25f --- /dev/null +++ b/drivers/alist_v2/meta.go @@ -0,0 +1,26 @@ +package alist_v2 + +import ( + "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/op" +) + +type Addition struct { + driver.RootPath + Address string `json:"url" required:"true"` + Password string `json:"password"` + AccessToken string `json:"access_token"` +} + +var config = driver.Config{ + Name: "AList V2", + LocalSort: true, + NoUpload: true, + DefaultRoot: "/", +} + +func init() { + op.RegisterDriver(config, func() driver.Driver { + return &AListV2{} + }) +} diff --git a/drivers/alist_v2/types.go b/drivers/alist_v2/types.go new file mode 100644 index 00000000..b2317fbf --- /dev/null +++ b/drivers/alist_v2/types.go @@ -0,0 +1,31 @@ +package alist_v2 + +import ( + "time" +) + +type File struct { + Id string `json:"-"` + Name string `json:"name"` + Size int64 `json:"size"` + Type int `json:"type"` + Driver string `json:"driver"` + UpdatedAt *time.Time `json:"updated_at"` + Thumbnail string `json:"thumbnail"` + Url string `json:"url"` + SizeStr string `json:"size_str"` + TimeStr string `json:"time_str"` +} + +type PathResp struct { + Type string `json:"type"` + //Meta Meta `json:"meta"` + Files []File `json:"files"` +} + +type PathReq struct { + PageNum int `json:"page_num"` + PageSize int `json:"page_size"` + Password string `json:"password"` + Path string `json:"path"` +} diff --git a/drivers/alist_v2/util.go b/drivers/alist_v2/util.go new file mode 100644 index 00000000..e6682193 --- /dev/null +++ b/drivers/alist_v2/util.go @@ -0,0 +1 @@ +package alist_v2 diff --git a/drivers/all.go b/drivers/all.go index b7b06fc9..306ac79a 100644 --- a/drivers/all.go +++ b/drivers/all.go @@ -6,6 +6,7 @@ import ( _ "github.com/alist-org/alist/v3/drivers/139" _ "github.com/alist-org/alist/v3/drivers/189" _ "github.com/alist-org/alist/v3/drivers/189pc" + _ "github.com/alist-org/alist/v3/drivers/alist_v2" _ "github.com/alist-org/alist/v3/drivers/alist_v3" _ "github.com/alist-org/alist/v3/drivers/aliyundrive" _ "github.com/alist-org/alist/v3/drivers/aliyundrive_share"