-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
148 lines (115 loc) · 3.73 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package directadmin
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"github.com/spf13/cast"
)
// CheckFileExists (user) checks if the given file exists on the server
func (c *UserContext) CheckFileExists(filePath string) error {
var response apiGenericResponse
if _, err := c.makeRequestOld(http.MethodGet, "FILE_MANAGER?action=exists&path="+filePath, nil, &response); err != nil {
return err
}
if response.Success != "File exists check" {
return fmt.Errorf("file doesn't exist: %v", response.Error)
}
return nil
}
// DeleteFiles (user) deletes all the specified files for the session user
func (c *UserContext) DeleteFiles(skipTrash bool, files ...string) error {
var response apiGenericResponse
body := url.Values{}
body.Set("button", "delete")
if skipTrash {
body.Set("trash", "no")
} else {
body.Set("trash", "yes")
}
for index, file := range files {
// add / to the beginning of filePath if it doesn't exist
if file[0] != '/' {
file = "/" + file
}
body.Set("select"+cast.ToString(index), file)
}
if _, err := c.makeRequestOld(http.MethodPost, "FILE_MANAGER?action=multiple", body, &response); err != nil {
return err
}
if response.Success != "Files deleted" {
return fmt.Errorf("failed to delete files: %v", response.Result)
}
return nil
}
// DownloadFile (user) downloads the given file path from the server
func (c *UserContext) DownloadFile(filePath string) ([]byte, error) {
return c.makeRequestNew(http.MethodGet, "filemanager/download?path="+filePath, nil, nil)
}
// DownloadFileToDisk (user) wraps DownloadFile and writes the output to the given path
func (c *UserContext) DownloadFileToDisk(filePath string, outputPath string) error {
if outputPath == "" {
return fmt.Errorf("no file path provided")
}
response, err := c.DownloadFile(filePath)
if err != nil {
return err
}
return os.WriteFile(outputPath, response, 0644)
}
// ExtractFile unzips the given file path on the server
func (c *UserContext) ExtractFile(filePath string, file string) error {
var response apiGenericResponse
// add / to the beginning of filePath if it doesn't exist
if filePath[0] != '/' {
filePath = "/" + filePath
}
// add / to the beginning of file if it doesn't exist
if file[0] != '/' {
file = "/" + file
}
body := url.Values{}
body.Set("directory", filePath)
body.Set("path", file)
body.Set("page", "2")
if _, err := c.makeRequestOld(http.MethodPost, "FILE_MANAGER?action=extract", body, &response); err != nil {
return err
}
if response.Success != "File Extracted" {
return fmt.Errorf("failed to extract file: %v", response.Result)
}
return nil
}
// UploadFile (user) uploads the provided byte data as a file for the session user
func (c *UserContext) UploadFile(uploadToPath string, fileData []byte) error {
// add / to the beginning of uploadToPath if it doesn't exist
if uploadToPath[0] != '/' {
uploadToPath = "/" + uploadToPath
}
if _, err := c.uploadFile(http.MethodPost, "/api/filemanager/upload?path="+uploadToPath, fileData, nil, "application/json"); err != nil {
return err
}
return nil
}
// UploadFileFromDisk (user) uploads the provided file for the session user
//
// Example: UploadFileFromDisk("/domains/domain.tld/public_html/file.zip", "file.zip")
func (c *UserContext) UploadFileFromDisk(uploadToPath string, localFilePath string) error {
var err error
localFilePath, err = filepath.Abs(localFilePath)
if err != nil {
return fmt.Errorf("failed to resolve file: %w", err)
}
file, err := os.Open(localFilePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
fileData, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
}
return c.UploadFile(uploadToPath, fileData)
}