Skip to content

Commit

Permalink
Use new API for uploading files
Browse files Browse the repository at this point in the history
Better handling around empty responses from the new API
  • Loading branch information
wolveix committed Jun 27, 2024
1 parent a5170af commit a202103
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
62 changes: 25 additions & 37 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,62 +29,50 @@ func (c *UserContext) CheckFileExists(filePath string) error {
}

// CreateFile (user) creates the provided file for the session user
func (c *UserContext) CreateFile(uploadToPath string, filePath string) error {
return c.CreateFiles(uploadToPath, filePath)
}

// CreateFiles (user) creates the provided files for the session user
func (c *UserContext) CreateFiles(uploadToPath string, filePaths ...string) error {
if len(filePaths) == 0 {
return fmt.Errorf("no files provided")
}

counter := 0
//
// Example: CreateFile("/domains/domain.tld/public_html/file.zip", "file.zip")
func (c *UserContext) CreateFile(uploadToPath string, localFilePath string) error {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)

writer.FormDataContentType()

for _, filePath := range filePaths {
var err error
filePath, err = filepath.Abs(filePath)
if err != nil {
return fmt.Errorf("failed to resolve file: %w", err)
}
var err error

file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()
localFilePath, err = filepath.Abs(localFilePath)
if err != nil {
return fmt.Errorf("failed to resolve file: %w", err)
}

part, err := writer.CreateFormFile("file"+cast.ToString(counter), filepath.Base(file.Name()))
if err != nil {
return fmt.Errorf("failed to create file in form: %w", err)
}
file, err := os.Open(localFilePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer file.Close()

if _, err = io.Copy(part, file); err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
part, err := writer.CreateFormFile("file", filepath.Base(file.Name()))
if err != nil {
return fmt.Errorf("failed to create file in form: %w", err)
}

writer.Close()
if _, err = io.Copy(part, file); err != nil {
return fmt.Errorf("failed to create file: %w", err)
}

var response apiGenericResponse
if err = writer.Close(); err != nil {
return err
}

var response apiGenericResponseN

// add / to the beginning of uploadToPath if it doesn't exist
if uploadToPath[0] != '/' {
uploadToPath = "/" + uploadToPath
}

if _, err := c.uploadFile(http.MethodPost, "/CMD_FILE_MANAGER?action=upload&path="+uploadToPath, body.Bytes(), &response, writer.FormDataContentType()); err != nil {
if _, err = c.uploadFile(http.MethodPost, "/api/filemanager/upload?path="+uploadToPath, body.Bytes(), &response, writer.FormDataContentType()); err != nil {
return err
}

if response.Success != "Upload successful" {
return fmt.Errorf("failed to create file: %v", response.Result)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (c *UserContext) uploadFile(method string, endpoint string, data []byte, ob
return nil, fmt.Errorf("error making request: %w", err)
}

if resp != nil && object != nil {
if resp != nil && len(resp) > 0 && object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
Expand Down

0 comments on commit a202103

Please sign in to comment.