persona-community-5/pkg/storage/fetch.go
jordan bd2f591b98
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/manual/woodpecker Pipeline was successful
Initialize project from skeleton template
2026-02-24 07:39:46 +00:00

32 lines
891 B
Go

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
}