Documentation
¶
Index ¶
- Constants
- Variables
- type Downloader
- func (d *Downloader) OnDownloadCanceled(f func(filename string))
- func (d *Downloader) OnDownloadFinished(f func(filename string))
- func (d *Downloader) OnDownloadStart(f func(total int64, filename string))
- func (d *Downloader) OnProgress(f func(loaded int64, total int64, rate string))
- func (d *Downloader) Pause() error
- func (d *Downloader) Resume() error
- func (d *Downloader) Start() error
- func (d *Downloader) Stop() error
- type OptionFunc
- func WithBaseDir(basedir string) OptionFunc
- func WithConcurrency(concurrency int) OptionFunc
- func WithFileName(path string) OptionFunc
- func WithHTTPClient(client *http.Client) OptionFunc
- func WithProxy(proxyURL string) OptionFunc
- func WithResume(resume bool) OptionFunc
- func WithSystemProxy() OptionFunc
- type Options
Constants ¶
const ( // DefaultConcurrency 默认并发下载数 DefaultConcurrency = 0 // 0表示使用runtime.NumCPU() // DefaultBaseDir 默认缓存目录 DefaultBaseDir = "downloader_cache" // DefaultBufferSize 默认缓冲区大小 DefaultBufferSize = 32 * 1024 // RateUpdateInterval 速率更新间隔 RateUpdateInterval = 250 * time.Millisecond // FilePerm 文件权限 FilePerm = 0644 // DirPerm 目录权限 DirPerm = 0755 )
常量定义
Variables ¶
var ( // ErrAlreadyStopped 下载器已停止错误 ErrAlreadyStopped = errors.New("downloader has been stopped") // ErrInvalidURL URL无效错误 ErrInvalidURL = errors.New("invalid download URL") // ErrInvalidConcurrency 并发数无效错误 ErrInvalidConcurrency = errors.New("concurrency must be greater than 0") )
错误定义
Functions ¶
This section is empty.
Types ¶
type Downloader ¶
type Downloader struct {
// contains filtered or unexported fields
}
Downloader 文件下载器,支持多协程并发下载和断点续传
func NewDownloader ¶
func NewDownloader(url string, opts ...OptionFunc) *Downloader
NewDownloader 创建一个新的文件下载器实例
参数:
url - 要下载的文件URL地址 opts - 可选的配置函数,用于自定义下载行为
返回:
*Downloader - 配置好的下载器实例
示例:
dl := NewDownloader("https://example.com/file.zip",
WithConcurrency(8),
WithResume(true))
func (*Downloader) OnDownloadCanceled ¶
func (d *Downloader) OnDownloadCanceled(f func(filename string))
OnDownloadCanceled 设置下载被取消时的回调函数
参数:
f - 回调函数,接收被取消的文件名
func (*Downloader) OnDownloadFinished ¶
func (d *Downloader) OnDownloadFinished(f func(filename string))
OnDownloadFinished 设置下载成功完成后的回调函数
参数:
f - 回调函数,接收已完成的文件名
func (*Downloader) OnDownloadStart ¶
func (d *Downloader) OnDownloadStart(f func(total int64, filename string))
OnDownloadStart 设置下载开始时的回调函数
参数:
f - 回调函数,接收文件总大小和文件名
func (*Downloader) OnProgress ¶
func (d *Downloader) OnProgress(f func(loaded int64, total int64, rate string))
OnProgress 设置下载进度回调函数
参数:
f - 回调函数,接收已下载字节数、总字节数和当前速率
注意: 此回调会被频繁调用,应避免执行耗时操作
func (*Downloader) Pause ¶
func (d *Downloader) Pause() error
Pause 暂停下载(Stop的别名)
此方法与Stop()行为完全相同。如果启用了断点续传, 可以通过调用Resume()从上次停止的位置继续下载。
返回:
error - 如果下载器已经停止则返回ErrAlreadyStopped,否则返回nil
func (*Downloader) Resume ¶
func (d *Downloader) Resume() error
Resume 恢复之前暂停的下载(Start的别名)
如果启用了断点续传,将从上次停止的位置继续下载。
返回:
error - 下载过程中的错误,成功则返回nil
func (*Downloader) Start ¶
func (d *Downloader) Start() error
Start 开始执行下载任务
如果下载器之前被停止,会自动重新初始化
返回:
error - 下载过程中的错误,成功则返回nil
func (*Downloader) Stop ¶
func (d *Downloader) Stop() error
Stop 停止正在进行的下载任务
此方法会取消所有正在进行的下载协程,但不会删除已下载的分片文件。 如果启用了断点续传,可以通过调用Resume()继续下载。
返回:
error - 如果下载器已经停止则返回ErrAlreadyStopped,否则返回nil
type OptionFunc ¶
type OptionFunc func(*Options)
OptionFunc 配置函数
func WithFileName ¶
func WithFileName(path string) OptionFunc
WithFileName 设置下载的文件名或路径 如果 path 包含路径分隔符,将同时设置 FilePath 和 FileName
func WithHTTPClient ¶ added in v1.0.4
func WithHTTPClient(client *http.Client) OptionFunc
WithHTTPClient 设置自定义的HTTP客户端 可用于配置超时、重试策略、TLS配置等
func WithProxy ¶ added in v1.0.4
func WithProxy(proxyURL string) OptionFunc
WithProxy 设置代理服务器 proxyURL 代理服务器地址,例如:"http://127.0.0.1:7890" 或 "socks5://127.0.0.1:1080"
func WithSystemProxy ¶ added in v1.0.4
func WithSystemProxy() OptionFunc
WithSystemProxy 使用系统代理设置 会自动读取系统的 HTTP_PROXY、HTTPS_PROXY 和 NO_PROXY 环境变量
type Options ¶
type Options struct {
// FileName 指定下载后保存的文件名(不包含路径)
FileName string
// FilePath 指定下载后保存的完整路径(包含目录和文件名)
FilePath string
// BaseDir 多协程下载时分片文件的缓存目录
BaseDir string
// Concurrency 并发下载的协程数,0表示使用CPU核心数
Concurrency int
// Resume 是否启用断点续传功能
Resume bool
// HTTPClient 自定义HTTP客户端,可用于配置代理、超时等
HTTPClient *http.Client
}
Options 下载器配置选项