Skip to content

Commit 84b3f7b

Browse files
committed
Support additional file manager functions
1 parent 5757d7c commit 84b3f7b

File tree

2 files changed

+77
-26
lines changed

2 files changed

+77
-26
lines changed

admin.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ package directadmin
22

33
import "net/http"
44

5-
// Admin inherits Reseller which inherits User
6-
type Admin struct {
7-
Reseller
8-
}
5+
type (
6+
// Admin inherits Reseller which inherits User
7+
Admin struct {
8+
Reseller
9+
}
910

10-
type AdminContext struct {
11-
ResellerContext
12-
}
11+
AdminContext struct {
12+
ResellerContext
13+
}
1314

14-
type convertAccount struct {
15-
Account string `json:"account,omitempty"`
16-
Creator string `json:"creator,omitempty"`
17-
}
15+
convertAccount struct {
16+
Account string `json:"account,omitempty"`
17+
Creator string `json:"creator,omitempty"`
18+
}
19+
)
1820

1921
func (c *AdminContext) ConvertResellerToUser(username string, reseller string) error {
2022
if _, err := c.makeRequestNew(http.MethodPost, "convert-reseller-to-user", convertAccount{Account: username, Creator: reseller}, nil); err != nil {

file.go

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,47 @@ import (
99
"net/url"
1010
"os"
1111
"path/filepath"
12+
"time"
1213

1314
"github.com/spf13/cast"
1415
)
1516

16-
// CheckFileExists (user) checks if the given file exists on the server
17-
func (c *UserContext) CheckFileExists(filePath string) error {
18-
var response apiGenericResponse
17+
type FileMetadata struct {
18+
AccessTime time.Time `json:"accessTime"`
19+
BirthTime time.Time `json:"birthTime"`
20+
ChangeTime time.Time `json:"changeTime"`
21+
GID int `json:"gid"`
22+
Group string `json:"group"`
23+
Mode string `json:"mode"`
24+
ModifyTime time.Time `json:"modifyTime"`
25+
Name string `json:"name"`
26+
SizeBytes int `json:"sizeBytes"`
27+
Symlink struct {
28+
Resolved string `json:"resolved"`
29+
Target string `json:"target"`
30+
} `json:"symlink"`
31+
Type string `json:"type"`
32+
UID int `json:"uid"`
33+
UnixMode int `json:"unixMode"`
34+
User string `json:"user"`
35+
}
1936

20-
if _, err := c.makeRequestOld(http.MethodGet, "FILE_MANAGER?action=exists&path="+filePath, nil, &response); err != nil {
21-
return err
37+
// CreateDirectory (user) creates the given path, including any missing parent directories.
38+
func (c *UserContext) CreateDirectory(path string) error {
39+
var response apiGenericResponseN
40+
41+
body := map[string]string{
42+
"path": path,
2243
}
2344

24-
if response.Success != "File exists check" {
25-
return fmt.Errorf("file doesn't exist: %v", response.Error)
45+
if _, err := c.makeRequestNew(http.MethodPost, "/api/filemanager-actions/mkdir", body, &response); err != nil {
46+
return err
2647
}
2748

2849
return nil
2950
}
3051

31-
// DeleteFiles (user) deletes all the specified files for the session user
52+
// DeleteFiles (user) deletes all the specified files for the session user.
3253
func (c *UserContext) DeleteFiles(skipTrash bool, files ...string) error {
3354
var response apiGenericResponse
3455

@@ -61,12 +82,12 @@ func (c *UserContext) DeleteFiles(skipTrash bool, files ...string) error {
6182
return nil
6283
}
6384

64-
// DownloadFile (user) downloads the given file path from the server
85+
// DownloadFile (user) downloads the given file path from the server.
6586
func (c *UserContext) DownloadFile(filePath string) ([]byte, error) {
6687
return c.makeRequestNew(http.MethodGet, "filemanager/download?path="+filePath, nil, nil)
6788
}
6889

69-
// DownloadFileToDisk (user) wraps DownloadFile and writes the output to the given path
90+
// DownloadFileToDisk (user) wraps DownloadFile and writes the output to the given path.
7091
func (c *UserContext) DownloadFileToDisk(filePath string, outputPath string) error {
7192
if outputPath == "" {
7293
return fmt.Errorf("no file path provided")
@@ -80,16 +101,16 @@ func (c *UserContext) DownloadFileToDisk(filePath string, outputPath string) err
80101
return os.WriteFile(outputPath, response, 0o644)
81102
}
82103

83-
// ExtractFile unzips the given file path on the server
104+
// ExtractFile unzips the given file path on the server.
84105
func (c *UserContext) ExtractFile(filePath string, file string) error {
85106
var response apiGenericResponse
86107

87-
// add / to the beginning of filePath if it doesn't exist
108+
// Prepend / to the filePath if it doesn't exist.
88109
if filePath[0] != '/' {
89110
filePath = "/" + filePath
90111
}
91112

92-
// add / to the beginning of file if it doesn't exist
113+
// Prepend / to the file if it doesn't exist.
93114
if file[0] != '/' {
94115
file = "/" + file
95116
}
@@ -110,9 +131,37 @@ func (c *UserContext) ExtractFile(filePath string, file string) error {
110131
return nil
111132
}
112133

134+
// GetFileMetadata (user) retrieves file metadata for the given path.
135+
func (c *UserContext) GetFileMetadata(filePath string) (*FileMetadata, error) {
136+
var response *FileMetadata
137+
138+
if _, err := c.makeRequestNew(http.MethodGet, "/api/filemanager/metadata?path="+filePath, nil, &response); err != nil {
139+
return nil, err
140+
}
141+
142+
return response, nil
143+
}
144+
145+
// MovePath (user) moves the given file or directory to the new destination.
146+
func (c *UserContext) MovePath(source string, destination string, overwrite bool) error {
147+
var response apiGenericResponseN
148+
149+
body := map[string]string{
150+
"destination": destination,
151+
"overwrite": fmt.Sprintf("%t", overwrite),
152+
"source": source,
153+
}
154+
155+
if _, err := c.makeRequestNew(http.MethodPost, "/api/filemanager-actions/move", body, &response); err != nil {
156+
return err
157+
}
158+
159+
return nil
160+
}
161+
113162
// UploadFile uploads the provided byte data as a file for the session user
114163
func (c *UserContext) UploadFile(uploadToPath string, fileData []byte) error {
115-
// Add / to the beginning of uploadToPath if it doesn't exist.
164+
// Prepend / to uploadToPath if it doesn't exist.
116165
if uploadToPath[0] != '/' {
117166
uploadToPath = "/" + uploadToPath
118167
}
@@ -141,7 +190,7 @@ func (c *UserContext) UploadFile(uploadToPath string, fileData []byte) error {
141190
return nil
142191
}
143192

144-
// UploadFileFromDisk (user) uploads the provided file for the session user
193+
// UploadFileFromDisk (user) uploads the provided file for the session user.
145194
//
146195
// Example: UploadFileFromDisk("/domains/domain.tld/public_html/file.zip", "file.zip")
147196
func (c *UserContext) UploadFileFromDisk(uploadToPath string, localFilePath string) error {

0 commit comments

Comments
 (0)