feat: Add file download functionality and ASCII art preview for camera snapshots

- Implemented DownloadFile method in client.go to download files with authentication.
- Added ascii.go for converting images to ASCII art with configurable parameters.
- Enhanced main.go to include a new option for capturing and displaying snapshots as ASCII art.
- Introduced non-interactive mode for onvif-cli, allowing command execution via command-line arguments.
- Updated documentation to include usage examples for non-interactive mode and scripting.
- Added error handling and improved user prompts for better user experience.
This commit is contained in:
ProtoTess
2025-11-18 04:13:44 +00:00
parent b62a4281b4
commit 3082840445
9 changed files with 1547 additions and 123 deletions
+39
View File
@@ -3,6 +3,7 @@ package onvif
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
@@ -201,3 +202,41 @@ func (c *Client) GetCredentials() (string, string) {
defer c.mu.RUnlock()
return c.username, c.password
}
// DownloadFile downloads a file from the given URL with authentication
// Returns the raw file bytes
func (c *Client) DownloadFile(ctx context.Context, url string) ([]byte, error) {
// Create a new HTTP request with context
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Add authentication if credentials are provided
if c.username != "" {
req.SetBasicAuth(c.username, c.password)
}
// Set User-Agent header
req.Header.Set("User-Agent", "onvif-go-client")
// Execute the request
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("download request failed: %w", err)
}
defer resp.Body.Close()
// Check HTTP status code
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("download failed with status code %d", resp.StatusCode)
}
// Read all data from response body
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return data, nil
}