files
This commit is contained in:
124
client/client.go
124
client/client.go
@@ -5,7 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -602,3 +604,125 @@ func (c *Client) ClickElement(tabID, selector string, selectionTimeout, actionTi
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UploadFileToContainer uploads a file from the client to the container
|
||||
// localPath: path to the file on the client machine
|
||||
// containerPath: optional path where to store the file in the container (defaults to /tmp/filename)
|
||||
func (c *Client) UploadFileToContainer(localPath, containerPath string) (string, error) {
|
||||
// Open the local file
|
||||
file, err := os.Open(localPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open local file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Get file info
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get file info: %w", err)
|
||||
}
|
||||
|
||||
// Create multipart form
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
|
||||
// Add the file field
|
||||
fileWriter, err := writer.CreateFormFile("file", fileInfo.Name())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create form file: %w", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(fileWriter, file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to copy file data: %w", err)
|
||||
}
|
||||
|
||||
// Add the path field if specified
|
||||
if containerPath != "" {
|
||||
err = writer.WriteField("path", containerPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to write path field: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = writer.Close()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to close multipart writer: %w", err)
|
||||
}
|
||||
|
||||
// Send the request
|
||||
req, err := http.NewRequest("POST", c.serverURL+"/upload", &body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return "", fmt.Errorf("upload failed with status %d: %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
// Parse the response
|
||||
var response Response
|
||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
if !response.Success {
|
||||
return "", fmt.Errorf("upload failed: %s", response.Error)
|
||||
}
|
||||
|
||||
// Extract the target path from response
|
||||
data, ok := response.Data.(map[string]interface{})
|
||||
if !ok {
|
||||
return "", fmt.Errorf("invalid response data format")
|
||||
}
|
||||
|
||||
targetPath, ok := data["target_path"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("target_path not found in response")
|
||||
}
|
||||
|
||||
return targetPath, nil
|
||||
}
|
||||
|
||||
// DownloadFileFromContainer downloads a file from the container to the client
|
||||
// containerPath: path to the file in the container
|
||||
// localPath: path where to save the file on the client machine
|
||||
func (c *Client) DownloadFileFromContainer(containerPath, localPath string) error {
|
||||
// Create the request
|
||||
url := fmt.Sprintf("%s/download?path=%s", c.serverURL, containerPath)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send download request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("download failed with status %d: %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
// Create the local file
|
||||
localFile, err := os.Create(localPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create local file: %w", err)
|
||||
}
|
||||
defer localFile.Close()
|
||||
|
||||
// Copy the response body to the local file
|
||||
_, err = io.Copy(localFile, resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to save file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user