Skip to content

Commit d6a4f0f

Browse files
committed
Use new API for deleting files
1 parent e9b9367 commit d6a4f0f

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

file.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ import (
66
"io"
77
"mime/multipart"
88
"net/http"
9-
"net/url"
109
"os"
1110
"path/filepath"
1211
"time"
13-
14-
"github.com/spf13/cast"
1512
)
1613

1714
type FileMetadata struct {
@@ -49,32 +46,34 @@ func (c *UserContext) CreateDirectory(path string) error {
4946

5047
// DeleteFiles (user) deletes all the specified files for the session user.
5148
func (c *UserContext) DeleteFiles(skipTrash bool, files ...string) error {
52-
var response apiGenericResponse
49+
if len(files) == 0 {
50+
return fmt.Errorf("no files provided")
51+
}
5352

54-
body := url.Values{}
55-
body.Set("button", "delete")
53+
normalized := make([]string, len(files))
5654

57-
if skipTrash {
58-
body.Set("trash", "no")
59-
} else {
60-
body.Set("trash", "yes")
61-
}
55+
for i, f := range files {
56+
if len(f) == 0 {
57+
return fmt.Errorf("empty file path provided for file %d", i+1)
58+
}
6259

63-
for index, file := range files {
64-
// add / to the beginning of filePath if it doesn't exist
65-
if file[0] != '/' {
66-
file = "/" + file
60+
if f[0] != '/' {
61+
f = "/" + f
6762
}
6863

69-
body.Set("select"+cast.ToString(index), file)
64+
normalized[i] = f
7065
}
7166

72-
if _, err := c.makeRequestOld(http.MethodPost, "FILE_MANAGER?action=multiple", body, &response); err != nil {
73-
return err
67+
body := struct {
68+
Paths []string `json:"paths"`
69+
Trash bool `json:"trash"`
70+
}{
71+
Paths: normalized,
72+
Trash: !skipTrash,
7473
}
7574

76-
if response.Success != "Files deleted" {
77-
return fmt.Errorf("failed to delete files: %v", response.Result)
75+
if _, err := c.makeRequestNew(http.MethodPost, "filemanager-actions/remove", body, nil); err != nil {
76+
return err
7877
}
7978

8079
return nil

0 commit comments

Comments
 (0)