Documentation
ΒΆ
Index ΒΆ
- type DownloadStats
- func Download(ctx context.Context, url, dest string) (*DownloadStats, error)
- func DownloadToMemory(ctx context.Context, url string) ([]byte, *DownloadStats, error)
- func DownloadToWriter(ctx context.Context, url string, w io.Writer) (*DownloadStats, error)
- func DownloadWithOptions(ctx context.Context, url, dest string, opts *Options) (*DownloadStats, error)
- func DownloadWithResume(ctx context.Context, url, dest string) (*DownloadStats, error)
- type Downloader
- func (d *Downloader) Download(ctx context.Context, url, dest string, opts *Options) (*DownloadStats, error)
- func (d *Downloader) DownloadToWriter(ctx context.Context, url string, w io.Writer, opts *Options) (*DownloadStats, error)
- func (d *Downloader) GetFileInfo(ctx context.Context, url string) (*FileInfo, error)
- func (d *Downloader) On(event events.EventType, handler events.EventListener)
- func (d *Downloader) RegisterProtocol(handler protocols.ProtocolHandler) error
- func (d *Downloader) SetStorageBackend(name string, backend storage.StorageBackend) error
- func (d *Downloader) UseMiddleware(m middleware.Middleware)
- func (d *Downloader) UsePlugin(p plugin.Plugin) error
- type FileInfo
- type Options
- type Progress
- type ProgressCallback
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
This section is empty.
Types ΒΆ
type DownloadStats ΒΆ
type DownloadStats struct {
// URL is the source URL that was downloaded.
URL string
// Filename is the destination filename or path.
Filename string
// TotalSize is the total size of the downloaded file in bytes.
TotalSize int64
// BytesDownloaded is the number of bytes successfully downloaded.
BytesDownloaded int64
// StartTime is when the download started.
StartTime time.Time
// EndTime is when the download completed or failed.
EndTime time.Time
// Duration is the total time taken for the download.
Duration time.Duration
// AverageSpeed is the average download speed in bytes per second.
AverageSpeed int64
// Retries is the number of retry attempts that were made.
Retries int
// Success indicates whether the download completed successfully.
Success bool
// Error contains any error that occurred during download.
Error error
// Resumed indicates whether this download was resumed from a partial file.
Resumed bool
// ChunksUsed indicates the number of concurrent chunks used for download.
ChunksUsed int
}
DownloadStats contains statistics about a download operation.
func Download ΒΆ
func Download(ctx context.Context, url, dest string) (*DownloadStats, error)
Download downloads a file from URL to destination path.
Example:
ctx := context.Background()
stats, err := gdl.Download(ctx, "https://example.com/file.zip", "./downloads/file.zip")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Downloaded %d bytes in %v\n", stats.BytesDownloaded, stats.Duration)
Example ΒΆ
Example demonstrates basic usage.
package main
import (
"context"
"fmt"
"github.com/forest6511/gdl"
)
func main() {
ctx := context.Background()
_, err := gdl.Download(ctx, "https://example.com/file.txt", "downloaded.txt")
if err != nil {
fmt.Printf("Download failed: %v\n", err)
}
}
func DownloadToMemory ΒΆ
DownloadToMemory downloads to memory and returns bytes.
Example:
ctx := context.Background()
data, err := gdl.DownloadToMemory(ctx, "https://example.com/api/data.json")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Downloaded %d bytes\n", len(data))
Example ΒΆ
Example demonstrates downloading to memory.
package main
import (
"context"
"fmt"
"github.com/forest6511/gdl"
)
func main() {
ctx := context.Background()
data, _, err := gdl.DownloadToMemory(ctx, "https://example.com/data.json")
if err != nil {
fmt.Printf("Download failed: %v\n", err)
return
}
fmt.Printf("Downloaded %d bytes\n", len(data))
}
func DownloadToWriter ΒΆ
DownloadToWriter downloads to an io.Writer.
Example:
ctx := context.Background()
var buf bytes.Buffer
err := gdl.DownloadToWriter(ctx, "https://example.com/data.txt", &buf)
if err != nil {
log.Fatal(err)
}
fmt.Println("Downloaded data:", buf.String())
Example ΒΆ
Example demonstrates downloading to a custom writer.
package main
import (
"context"
"fmt"
"os"
"github.com/forest6511/gdl"
"github.com/forest6511/gdl/pkg/validation"
)
func init() {
validation.SetConfig(validation.TestConfig())
}
func main() {
ctx := context.Background()
file, err := os.Create("output.txt")
if err != nil {
fmt.Printf("Failed to create file: %v\n", err)
return
}
defer func() { _ = file.Close() }()
_, err = gdl.DownloadToWriter(ctx, "https://example.com/content.txt", file)
if err != nil {
fmt.Printf("Download failed: %v\n", err)
}
}
func DownloadWithOptions ΒΆ
func DownloadWithOptions(ctx context.Context, url, dest string, opts *Options) (*DownloadStats, error)
DownloadWithOptions downloads with custom options.
Example:
ctx := context.Background()
opts := &gdl.Options{
MaxConcurrency: 4,
EnableResume: true,
ProgressCallback: func(p gdl.Progress) {
fmt.Printf("Downloaded: %.2f%%\n", p.Percentage)
},
}
err := gdl.DownloadWithOptions(ctx, "https://example.com/file.zip", "./file.zip", opts)
Example ΒΆ
Example demonstrates download with options.
package main
import (
"context"
"fmt"
"github.com/forest6511/gdl"
)
func main() {
ctx := context.Background()
opts := &gdl.Options{
MaxConcurrency: 4,
EnableResume: true,
ProgressCallback: func(p gdl.Progress) {
fmt.Printf("Downloaded: %.2f%%\n", p.Percentage)
},
}
_, err := gdl.DownloadWithOptions(ctx, "https://example.com/file.zip", "file.zip", opts)
if err != nil {
fmt.Printf("Download failed: %v\n", err)
}
}
func DownloadWithResume ΒΆ
func DownloadWithResume(ctx context.Context, url, dest string) (*DownloadStats, error)
DownloadWithResume downloads a file with resume support.
Example:
ctx := context.Background()
err := gdl.DownloadWithResume(ctx, "https://example.com/large-file.zip", "./large-file.zip")
if err != nil {
log.Fatal(err)
}
// If interrupted, running again will resume from where it left off
type Downloader ΒΆ
type Downloader struct {
// contains filtered or unexported fields
}
Downloader provides an extensible download client with plugin support.
func NewDownloader ΒΆ
func NewDownloader() *Downloader
NewDownloader creates a new Downloader with plugin support.
func (*Downloader) Download ΒΆ
func (d *Downloader) Download(ctx context.Context, url, dest string, opts *Options) (*DownloadStats, error)
Download downloads a file using the configured plugins and middleware.
func (*Downloader) DownloadToWriter ΒΆ
func (d *Downloader) DownloadToWriter(ctx context.Context, url string, w io.Writer, opts *Options) (*DownloadStats, error)
DownloadToWriter downloads to an io.Writer with plugin support.
func (*Downloader) GetFileInfo ΒΆ
GetFileInfo retrieves file information with plugin support.
func (*Downloader) On ΒΆ
func (d *Downloader) On(event events.EventType, handler events.EventListener)
On registers an event listener.
func (*Downloader) RegisterProtocol ΒΆ
func (d *Downloader) RegisterProtocol(handler protocols.ProtocolHandler) error
RegisterProtocol registers a custom protocol handler.
func (*Downloader) SetStorageBackend ΒΆ
func (d *Downloader) SetStorageBackend(name string, backend storage.StorageBackend) error
SetStorageBackend sets the storage backend.
func (*Downloader) UseMiddleware ΒΆ
func (d *Downloader) UseMiddleware(m middleware.Middleware)
UseMiddleware adds middleware to the chain.
type FileInfo ΒΆ
type FileInfo struct {
Size int64
Filename string
ContentType string
LastModified time.Time
SupportsRanges bool
}
FileInfo contains information about a remote file.
func GetFileInfo ΒΆ
GetFileInfo retrieves file information without downloading.
Example:
ctx := context.Background()
info, err := gdl.GetFileInfo(ctx, "https://example.com/file.zip")
if err != nil {
log.Fatal(err)
}
fmt.Printf("File: %s, Size: %d bytes, Type: %s\n", info.Filename, info.Size, info.ContentType)
type Options ΒΆ
type Options struct {
ProgressCallback ProgressCallback
MaxConcurrency int
ChunkSize int64
EnableResume bool
RetryAttempts int
Timeout time.Duration
UserAgent string
Headers map[string]string
CreateDirs bool
OverwriteExisting bool
Quiet bool
Verbose bool
MaxRate int64 // Maximum download rate in bytes per second (0 = unlimited)
}
Options defines download options.
type Progress ΒΆ
type Progress struct {
TotalSize int64
BytesDownloaded int64
Speed int64
Percentage float64
TimeElapsed time.Duration
TimeRemaining time.Duration
}
Progress represents the download progress.
type ProgressCallback ΒΆ
type ProgressCallback func(Progress)
ProgressCallback is a function that receives progress updates.
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gdl
command
Package main provides the command-line interface for the gdl download tool.
|
Package main provides the command-line interface for the gdl download tool. |
|
examples
|
|
|
01_basic_download
command
Package main demonstrates basic download functionality using the gdl library.
|
Package main demonstrates basic download functionality using the gdl library. |
|
02_concurrent_download
command
Package main demonstrates concurrent download functionality.
|
Package main demonstrates concurrent download functionality. |
|
03_progress_tracking
command
Package main demonstrates advanced progress tracking functionality.
|
Package main demonstrates advanced progress tracking functionality. |
|
04_resume_functionality
command
Package main demonstrates resume functionality for interrupted downloads.
|
Package main demonstrates resume functionality for interrupted downloads. |
|
05_error_handling
command
Package main demonstrates comprehensive error handling capabilities.
|
Package main demonstrates comprehensive error handling capabilities. |
|
integration
command
Package main provides a comprehensive demonstration of all gdl features This program shows both library and CLI integration working together
|
Package main provides a comprehensive demonstration of all gdl features This program shows both library and CLI integration working together |
|
library_api
command
|
|
|
parity_verification
command
|
|
|
plugins/auth/oauth2
command
|
|
|
plugins/storage/gcs
command
|
|
|
internal
|
|
|
core
Package core provides the core implementation of the gdl download functionality.
|
Package core provides the core implementation of the gdl download functionality. |
|
network
Package network provides network diagnostics and health checking capabilities.
|
Package network provides network diagnostics and health checking capabilities. |
|
recovery
Package recovery provides intelligent failure analysis and recovery mechanisms.
|
Package recovery provides intelligent failure analysis and recovery mechanisms. |
|
resume
Package resume provides functionality for resuming interrupted downloads.
|
Package resume provides functionality for resuming interrupted downloads. |
|
retry
Package retry provides retry strategies and mechanisms for handling transient failures.
|
Package retry provides retry strategies and mechanisms for handling transient failures. |
|
testing
Package testing provides error simulation tools for testing download scenarios.
|
Package testing provides error simulation tools for testing download scenarios. |
|
pkg
|
|
|
config
Package config provides configuration management for the gdl download tool.
|
Package config provides configuration management for the gdl download tool. |
|
errors
Package errors defines custom error types and sentinel errors for the gdl download library.
|
Package errors defines custom error types and sentinel errors for the gdl download library. |
|
help
Package help provides context-sensitive help and guidance for the gdl CLI tool.
|
Package help provides context-sensitive help and guidance for the gdl CLI tool. |
|
progress
Package progress provides progress tracking functionality for downloads.
|
Package progress provides progress tracking functionality for downloads. |
|
ratelimit
Package ratelimit provides bandwidth throttling functionality for downloads.
|
Package ratelimit provides bandwidth throttling functionality for downloads. |
|
types
Package types defines the core types and interfaces for the gdl download library.
|
Package types defines the core types and interfaces for the gdl download library. |
|
ui
Package ui provides user interface formatting and interaction utilities.
|
Package ui provides user interface formatting and interaction utilities. |
|
validation
Package validation provides input validation functions for public APIs.
|
Package validation provides input validation functions for public APIs. |