Skip to content

Commit baef83f

Browse files
committed
Adjust file upload to conform to DirectAdmin's API changes
1 parent 54dba82 commit baef83f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

email_forwarder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (c *UserContext) CreateEmailForwarder(domain string, user string, emails ..
3030

3131
// GetEmailForwarders (user) returns an array of email forwarders belonging to the provided domain
3232
func (c *UserContext) GetEmailForwarders(domain string) (map[string][]string, error) {
33-
var emailForwarders = make(map[string][]string)
33+
emailForwarders := make(map[string][]string)
3434

3535
if _, err := c.makeRequestOld(http.MethodGet, "API_EMAIL_FORWARDERS?domain="+domain, nil, &emailForwarders); err != nil {
3636
return nil, err

file.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package directadmin
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
7+
"mime/multipart"
68
"net/http"
79
"net/url"
810
"os"
@@ -75,7 +77,7 @@ func (c *UserContext) DownloadFileToDisk(filePath string, outputPath string) err
7577
return err
7678
}
7779

78-
return os.WriteFile(outputPath, response, 0644)
80+
return os.WriteFile(outputPath, response, 0o644)
7981
}
8082

8183
// ExtractFile unzips the given file path on the server
@@ -108,14 +110,31 @@ func (c *UserContext) ExtractFile(filePath string, file string) error {
108110
return nil
109111
}
110112

111-
// UploadFile (user) uploads the provided byte data as a file for the session user
113+
// UploadFile uploads the provided byte data as a file for the session user
112114
func (c *UserContext) UploadFile(uploadToPath string, fileData []byte) error {
113-
// add / to the beginning of uploadToPath if it doesn't exist
115+
// Add / to the beginning of uploadToPath if it doesn't exist.
114116
if uploadToPath[0] != '/' {
115117
uploadToPath = "/" + uploadToPath
116118
}
117119

118-
if _, err := c.uploadFile(http.MethodPost, "/api/filemanager/upload?path="+uploadToPath, fileData, nil, "application/json"); err != nil {
120+
body := &bytes.Buffer{}
121+
writer := multipart.NewWriter(body)
122+
123+
part, err := writer.CreateFormFile("file", filepath.Base(uploadToPath))
124+
if err != nil {
125+
return fmt.Errorf("creating form file: %w", err)
126+
}
127+
128+
if _, err = part.Write(fileData); err != nil {
129+
return fmt.Errorf("writing file data: %w", err)
130+
}
131+
132+
if err = writer.Close(); err != nil {
133+
return fmt.Errorf("finalizing form data: %w", err)
134+
}
135+
136+
// Now use this content type which includes the boundary.
137+
if _, err = c.uploadFile(http.MethodPost, "/api/filemanager/upload?dir="+filepath.Dir(uploadToPath)+"&name="+filepath.Base(uploadToPath), body.Bytes(), nil, writer.FormDataContentType()); err != nil {
119138
return err
120139
}
121140

0 commit comments

Comments
 (0)