mirror of
https://github.com/AlistGo/alist.git
synced 2025-12-19 11:00:06 +08:00
add more wps func
This commit is contained in:
parent
e37a8e9f93
commit
e17baab2d5
@ -113,23 +113,102 @@ func (d *Wps) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*m
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Wps) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
|
func (d *Wps) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
|
||||||
return errs.NotSupport
|
basePath := "/"
|
||||||
|
if parentDir != nil {
|
||||||
|
if p := parentDir.GetPath(); p != "" {
|
||||||
|
basePath = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node, err := d.resolvePath(ctx, basePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if node.kind != "group" && node.kind != "folder" {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
parentID := int64(0)
|
||||||
|
if node.file != nil && node.kind == "folder" {
|
||||||
|
parentID = node.file.ID
|
||||||
|
}
|
||||||
|
return d.createFolder(ctx, node.group.GroupID, parentID, dirName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Wps) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
|
func (d *Wps) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
|
||||||
return errs.NotSupport
|
if srcObj == nil || dstDir == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
srcNode, err := d.resolvePath(ctx, srcObj.GetPath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (srcNode.kind != "file" && srcNode.kind != "folder") || srcNode.file == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
dstNode, err := d.resolvePath(ctx, dstDir.GetPath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dstNode.kind != "group" && dstNode.kind != "folder" {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
targetParentID := int64(0)
|
||||||
|
if dstNode.file != nil && dstNode.kind == "folder" {
|
||||||
|
targetParentID = dstNode.file.ID
|
||||||
|
}
|
||||||
|
return d.moveFile(ctx, srcNode.group.GroupID, srcNode.file.ID, dstNode.group.GroupID, targetParentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Wps) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
func (d *Wps) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
|
||||||
return errs.NotSupport
|
if srcObj == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
node, err := d.resolvePath(ctx, srcObj.GetPath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (node.kind != "file" && node.kind != "folder") || node.file == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
return d.renameFile(ctx, node.group.GroupID, node.file.ID, newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Wps) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
func (d *Wps) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
|
||||||
return errs.NotSupport
|
if srcObj == nil || dstDir == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
srcNode, err := d.resolvePath(ctx, srcObj.GetPath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (srcNode.kind != "file" && srcNode.kind != "folder") || srcNode.file == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
dstNode, err := d.resolvePath(ctx, dstDir.GetPath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if dstNode.kind != "group" && dstNode.kind != "folder" {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
targetParentID := int64(0)
|
||||||
|
if dstNode.file != nil && dstNode.kind == "folder" {
|
||||||
|
targetParentID = dstNode.file.ID
|
||||||
|
}
|
||||||
|
return d.copyFile(ctx, srcNode.group.GroupID, srcNode.file.ID, dstNode.group.GroupID, targetParentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Wps) Remove(ctx context.Context, obj model.Obj) error {
|
func (d *Wps) Remove(ctx context.Context, obj model.Obj) error {
|
||||||
return errs.NotSupport
|
if obj == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
node, err := d.resolvePath(ctx, obj.GetPath())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (node.kind != "file" && node.kind != "folder") || node.file == nil {
|
||||||
|
return errs.NotSupport
|
||||||
|
}
|
||||||
|
return d.deleteFile(ctx, node.group.GroupID, node.file.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Wps) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
func (d *Wps) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Addition struct {
|
type Addition struct {
|
||||||
|
driver.RootPath
|
||||||
Cookie string `json:"cookie" required:"true" help:"kso_sid=xxxxx"`
|
Cookie string `json:"cookie" required:"true" help:"kso_sid=xxxxx"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -49,6 +49,39 @@ type downloadResp struct {
|
|||||||
Result string `json:"result"`
|
Result string `json:"result"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type opResp struct {
|
||||||
|
Result string `json:"result"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type renameReq struct {
|
||||||
|
Fname string `json:"fname"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type mkdirReq struct {
|
||||||
|
GroupID int64 `json:"groupid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ParentID int64 `json:"parentid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type moveReq struct {
|
||||||
|
FileIDs []int64 `json:"fileids"`
|
||||||
|
TargetGroupID int64 `json:"target_groupid"`
|
||||||
|
TargetParentID int64 `json:"target_parentid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type deleteReq struct {
|
||||||
|
FileIDs []int64 `json:"fileids"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type copyReq struct {
|
||||||
|
FileIDs []int64 `json:"fileids"`
|
||||||
|
GroupID int64 `json:"groupid"`
|
||||||
|
TargetGroupID int64 `json:"target_groupid"`
|
||||||
|
TargetParentID int64 `json:"target_parentid"`
|
||||||
|
DuplicatedNameModel int `json:"duplicated_name_model"`
|
||||||
|
}
|
||||||
|
|
||||||
type Obj struct {
|
type Obj struct {
|
||||||
id string
|
id string
|
||||||
name string
|
name string
|
||||||
|
|||||||
@ -65,6 +65,66 @@ func (d *Wps) getFiles(ctx context.Context, groupID, parentID int64) ([]FileInfo
|
|||||||
return resp.Files, nil
|
return resp.Files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkResult(result, msg string) error {
|
||||||
|
if result == "" || result == "ok" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if msg != "" {
|
||||||
|
return fmt.Errorf("%s: %s", result, msg)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%s", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Wps) renameFile(ctx context.Context, groupID, fileID int64, name string) error {
|
||||||
|
var resp opResp
|
||||||
|
url := fmt.Sprintf("%s/3rd/drive/api/v3/groups/%d/files/%d", endpoint, groupID, fileID)
|
||||||
|
_, err := d.request(ctx).SetBody(renameReq{Fname: name}).SetResult(&resp).Put(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return checkResult(resp.Result, resp.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Wps) createFolder(ctx context.Context, groupID, parentID int64, name string) error {
|
||||||
|
var resp opResp
|
||||||
|
url := endpoint + "/3rd/drive/api/v5/files/folder"
|
||||||
|
_, err := d.request(ctx).SetBody(mkdirReq{GroupID: groupID, Name: name, ParentID: parentID}).SetResult(&resp).Post(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return checkResult(resp.Result, resp.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Wps) moveFile(ctx context.Context, groupID, fileID, targetGroupID, targetParentID int64) error {
|
||||||
|
var resp opResp
|
||||||
|
url := fmt.Sprintf("%s/3rd/drive/api/v3/groups/%d/files/batch/move", endpoint, groupID)
|
||||||
|
_, err := d.request(ctx).SetBody(moveReq{FileIDs: []int64{fileID}, TargetGroupID: targetGroupID, TargetParentID: targetParentID}).SetResult(&resp).Post(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return checkResult(resp.Result, resp.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Wps) deleteFile(ctx context.Context, groupID, fileID int64) error {
|
||||||
|
var resp opResp
|
||||||
|
url := fmt.Sprintf("%s/3rd/drive/api/v3/groups/%d/files/batch/delete", endpoint, groupID)
|
||||||
|
_, err := d.request(ctx).SetBody(deleteReq{FileIDs: []int64{fileID}}).SetResult(&resp).Post(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return checkResult(resp.Result, resp.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Wps) copyFile(ctx context.Context, groupID, fileID, targetGroupID, targetParentID int64) error {
|
||||||
|
var resp opResp
|
||||||
|
url := fmt.Sprintf("%s/3rd/drive/api/v3/groups/%d/files/batch/copy", endpoint, groupID)
|
||||||
|
_, err := d.request(ctx).SetBody(copyReq{FileIDs: []int64{fileID}, GroupID: groupID, TargetGroupID: targetGroupID, TargetParentID: targetParentID, DuplicatedNameModel: 1}).SetResult(&resp).Post(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return checkResult(resp.Result, resp.Msg)
|
||||||
|
}
|
||||||
|
|
||||||
func parseTime(v int64) time.Time {
|
func parseTime(v int64) time.Time {
|
||||||
if v <= 0 {
|
if v <= 0 {
|
||||||
return time.Time{}
|
return time.Time{}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user