package storage import ( "context" "fmt" "io" "net/http" ) // FetchURL downloads content from a URL using the provided HTTP client. // maxBytes caps the download to prevent OOM from unexpectedly large responses. // Callers control the timeout via the http.Client they pass in. func FetchURL(ctx context.Context, client *http.Client, url string, maxBytes int64) ([]byte, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, fmt.Errorf("create request: %w", err) } resp, err := client.Do(req) if err != nil { return nil, fmt.Errorf("fetch: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("fetch: HTTP %d", resp.StatusCode) } data, err := io.ReadAll(io.LimitReader(resp.Body, maxBytes)) if err != nil { return nil, fmt.Errorf("read: %w", err) } return data, nil }