download

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client performs HTTP downloads with retry logic, resumption, and validation.

func NewClient

func NewClient(logger *slog.Logger) *Client

NewClient creates a new download client with the given logger.

func (*Client) Download

func (c *Client) Download(ctx context.Context, opts DownloadOptions) (*DownloadResult, error)

Download downloads a file from the given URL to the destination path. It supports resumable downloads, retries with exponential backoff, and checksum validation.

type DownloadOptions

type DownloadOptions struct {
	URL              string
	DestPath         string
	ExpectedChecksum string // SHA256 hex string, empty to skip validation
	ExpectedSize     int64  // 0 to skip size check
	Headers          map[string]string
	RetryCount       int // 0 defaults to 3
	OnProgress       ProgressFunc
}

DownloadOptions contains configuration for a single download.

type DownloadResult

type DownloadResult struct {
	Path     string        // Path to the downloaded file
	Size     int64         // Final file size in bytes
	SHA256   string        // SHA256 checksum in hex
	Resumed  bool          // Whether the download was resumed
	Attempts int           // Number of attempts made
	Duration time.Duration // Total download duration
}

DownloadResult contains the result of a successful download.

type HTTPError

type HTTPError struct {
	StatusCode int
	Status     string
	Body       string
}

HTTPError represents an HTTP error response.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Job

type Job struct {
	URL              string
	DestPath         string
	ExpectedChecksum string
	ExpectedSize     int64
	Headers          map[string]string
}

Job represents a single download job.

type Pool

type Pool struct {
	OnProgress func(destPath string, bytesDownloaded, totalBytes int64)
	OnComplete func(destPath string, size int64, success bool, errMsg string)
	// contains filtered or unexported fields
}

Pool manages concurrent downloads using a worker pool pattern.

func NewPool

func NewPool(client *Client, workers int, logger *slog.Logger) *Pool

NewPool creates a new download pool with the specified number of worker goroutines.

func (*Pool) Execute

func (p *Pool) Execute(ctx context.Context, jobs []Job) []Result

Execute submits a batch of jobs to the pool and waits for all to complete. The returned results maintain the same order as the input jobs. If the context is cancelled, all workers stop processing immediately.

type ProgressFunc

type ProgressFunc func(bytesDownloaded, totalBytes int64)

ProgressFunc is called periodically to report download progress. bytesDownloaded is the number of bytes downloaded so far, totalBytes is the total size of the download (or 0 if unknown).

type Result

type Result struct {
	Job      Job
	Success  bool
	Error    error
	Download *DownloadResult
	// contains filtered or unexported fields
}

Result represents the result of a download job.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL