Documentation ¶
Index ¶
- Constants
- func AddOptions(opt ...OptionFunc)
- func ReplaceHeader(h http.Header)
- func SetAllowOverwrite(d bool)
- func SetAutoFileRenaming(d bool)
- func SetBody(d io.Reader)
- func SetBreakpointExt(d string)
- func SetBreakpointResume(d bool)
- func SetClient(d *http.Client)
- func SetCreateDir(d bool)
- func SetDiskCache(d int)
- func SetHeader(k, v string)
- func SetMethod(d string)
- func SetOptions(opts []OptionFunc)
- func SetOutdir(d string)
- func SetPerm(d fs.FileMode)
- func SetProxy(p func(*http.Request) (*url.URL, error), h ...http.Header) error
- func SetRetryNumber(d int)
- func SetRetryTime(d time.Duration)
- func SetRoutineCount(d int)
- func SetRoutineSize(d int64)
- func SetSpeedLimit(d int)
- func SetTimeout(d time.Duration)
- type Bar
- type BarStatString
- type BarTemplate
- type Block
- type Breakpoint
- type Config
- type EventExtend
- type MultiReadable
- type OptionFunc
- func WithAllowOverwrite(d bool) OptionFunc
- func WithAutoFileRenaming(d bool) OptionFunc
- func WithAutoFilterFilename(d bool) OptionFunc
- func WithBar(bars ...*Bar) OptionFunc
- func WithBody(d io.Reader) OptionFunc
- func WithBreakpointExt(d string) OptionFunc
- func WithBreakpointResume(d bool) OptionFunc
- func WithClient(d *http.Client) OptionFunc
- func WithCreateDir(d bool) OptionFunc
- func WithDebug(d bool) OptionFunc
- func WithDiskCache(d int) OptionFunc
- func WithEvent(e ...ProgressEvent) OptionFunc
- func WithEventExtend(e ...ProgressEventExtend) OptionFunc
- func WithHeader(d func(h http.Header)) OptionFunc
- func WithMethod(d string) OptionFunc
- func WithOutdir(outdir string) OptionFunc
- func WithOutname(outname string) OptionFunc
- func WithOutpath(outpath string) OptionFunc
- func WithPerm(d fs.FileMode) OptionFunc
- func WithRetryNumber(d int) OptionFunc
- func WithRetryTime(d time.Duration) OptionFunc
- func WithRoutineCount(d int) OptionFunc
- func WithRoutineSize(d int64) OptionFunc
- func WithSpeedLimit(d int) OptionFunc
- func WithTimeout(d time.Duration) OptionFunc
- type ProgressEvent
- type ProgressEventExtend
- type Rain
- func (rain *Rain) AddOptions(opt ...OptionFunc)
- func (rain *Rain) New(uri string, opts ...OptionFunc) *RainControl
- func (rain *Rain) ReplaceHeader(h http.Header)
- func (rain *Rain) SetAllowOverwrite(d bool)
- func (rain *Rain) SetAutoFileRenaming(d bool)
- func (rain *Rain) SetAutoFilterFilename(d bool)
- func (rain *Rain) SetBody(d io.Reader)
- func (rain *Rain) SetBreakpointExt(d string)
- func (rain *Rain) SetBreakpointResume(d bool)
- func (rain *Rain) SetClient(d *http.Client)
- func (rain *Rain) SetCreateDir(d bool)
- func (rain *Rain) SetDiskCache(d int)
- func (rain *Rain) SetHeader(k, v string)
- func (rain *Rain) SetMethod(d string)
- func (rain *Rain) SetOptions(opt []OptionFunc)
- func (rain *Rain) SetOutdir(d string)
- func (rain *Rain) SetPerm(d fs.FileMode)
- func (rain *Rain) SetProxy(p func(*http.Request) (*url.URL, error), h ...http.Header) error
- func (rain *Rain) SetRetryNumber(d int)
- func (rain *Rain) SetRetryTime(d time.Duration)
- func (rain *Rain) SetRoutineCount(d int)
- func (rain *Rain) SetRoutineSize(d int64)
- func (rain *Rain) SetSpeedLimit(d int)
- func (rain *Rain) SetTimeout(d time.Duration)
- type RainControl
- func (rc *RainControl) Close()
- func (rc *RainControl) Error() error
- func (rc *RainControl) Outpath() string
- func (rc *RainControl) Run() (*RainControl, error)
- func (rc *RainControl) RunContext(ctx context.Context) (*RainControl, error)
- func (rc *RainControl) SetSpeedLimit(d int)
- func (rc *RainControl) Start() (*RainControl, error)
- func (rc *RainControl) StartContext(ctx context.Context) (*RainControl, error)
- func (rc *RainControl) Status() Status
- func (rc *RainControl) Wait() error
- func (rc *RainControl) WaitChan() <-chan error
- type Stat
- type Status
Constants ¶
const ( STATUS_NOTSTART = Status(iota - 1) // STATUS_BEGIN 准备中 STATUS_BEGIN // STATUS_RUNNING 运行中 STATUS_RUNNING // STATUS_CLOSE 关闭 STATUS_CLOSE // STATUS_ERROR 错误 STATUS_ERROR // STATUS_FINISH 完成 STATUS_FINISH )
const COPY_BUFFER_SIZE = 1024 * 32
COPY_BUFFER_SIZE 接收数据的 buffer 大小
Variables ¶
This section is empty.
Functions ¶
func SetAutoFileRenaming ¶
func SetAutoFileRenaming(d bool)
SetAutoFileRenaming 设置是否自动重命名文件,新文件名在名称之后扩展名之前加上一个点和一个数字(1..9999)
Types ¶
type Bar ¶
type Bar struct { // Template 进度条样式 Template *BarTemplate // FriendlyFormat 使用人类友好的单位 FriendlyFormat bool // FinishHide 完成后隐藏进度条,下载完成后清除掉进度条 FinishHide bool // Hide 是否隐藏进度条 Hide bool // Stdout 进度条输出, 默认为 os.Stdout Stdout io.Writer }
Bar 提供一个简单的进度条
type BarStatString ¶
type BarStatString struct { // TotalLength 文件总大小 TotalLength string // CompletedLength 已下载大小 CompletedLength string // DownloadSpeed 文件每秒下载速度 DownloadSpeed string // EstimatedTime 预计下载完成还需要的时间 EstimatedTime string // Progress 下载进度, 长度为 100 Progress string // Saucer 进度条 Saucer string }
BarStatString 注入到模版中的字符串结构 {{.CompletedLength}} / {{.TotalLength}} {{.Saucer}} {{.Progress}}% {{.DownloadSpeed}} {{.EstimatedTime}}
type BarTemplate ¶
type BarTemplate struct { // Template 模版 Template *template.Template // NoSizeTemplate 获取不到文件大小时的模板 NoSizeTemplate *template.Template // Saucer 进度字符, 默认为 = Saucer string // SaucerHead 进度条头, 使用场景 =====> , 其中的 > 就是进度条头, 默认为 > SaucerHead string // SaucerPadding 进度空白字符, 默认为 - SaucerPadding string // BarStart 进度前缀, 默认为 [ BarStart string // BarEnd 进度后缀, 默认为 ] BarEnd string // BarWidth 进度条宽度, 默认为 80 BarWidth int }
BarTemplate 进度条显示内容的参数
type Block ¶
type Block struct { Start int64 `json:"start"` End int64 `json:"end"` // contains filtered or unexported fields }
block 下载块
type Breakpoint ¶
type Breakpoint struct { // filesize 资源大小 Filesize int64 `json:"filesize"` // etag 资源唯一标识 Etag string `json:"etag"` // position 未分配任务的起始位置 Position int64 `json:"position"` // tasks 已分配的未完成任务 Tasks []*Block `json:"tasks"` }
breakpoint 断点
type Config ¶
type Config struct { // RoutineCount 多协程下载时最多同时下载一个文件的最大协程,默认为 1 RoutineCount int // RoutineSize 多协程下载时每个协程下载的大小,默认为 10M RoutineSize int64 // diskCache 磁盘缓冲区大小,默认为 1M DiskCache int // speedLimit 下载速度限制,默认为 0 无限制 SpeedLimit int // createDir 当需要创建目录时,是否创建目录,默认为 true CreateDir bool // allowOverwrite 是否允许覆盖文件,默认为 false AllowOverwrite bool // autoFileRenaming 文件自动重命名,新文件名在名称之后扩展名之前加上一个点和一个数字(1..9999)。默认:true AutoFileRenaming bool // AutoFilterFilename 自动过滤掉文件名称中的非法字符 AutoFilterFilename bool // breakpointResume 是否启用断点续传,默认为 true BreakpointResume bool // timeout 下载总超时时间,默认为 10 分钟 Timeout time.Duration // retryNumber 最多重试次数,默认为 5 RetryNumber int // retryTime 重试时的间隔时间,默认为 0 RetryTime time.Duration // BreakpointExt 断点文件扩展, 默认为 .temp.rain BreakpointExt string }
Config 配置
type EventExtend ¶ added in v0.2.0
type EventExtend struct { // Stat 信息 *Stat // DownloadSpeed 每秒下载字节数 DownloadSpeed int64 // EstimatedTime 预计下载完成还需要的时间 EstimatedTime time.Duration // contains filtered or unexported fields }
func (*EventExtend) AddEvent ¶ added in v0.2.0
func (se *EventExtend) AddEvent(e ...ProgressEventExtend)
AddEvent 新增事件
type MultiReadable ¶ added in v0.4.0
type MultiReadable struct {
// contains filtered or unexported fields
}
MultiReadable 重复读取 reader
func NewMultiReadable ¶ added in v0.4.0
func NewMultiReadable(reader io.Reader) *MultiReadable
func (*MultiReadable) Close ¶ added in v0.4.0
func (mr *MultiReadable) Close() error
func (*MultiReadable) Reset ¶ added in v0.4.0
func (mr *MultiReadable) Reset() error
type OptionFunc ¶
type OptionFunc func(ctl *control)
OptionFunc 参数
func WithAllowOverwrite ¶
func WithAllowOverwrite(d bool) OptionFunc
WithAllowOverwrite 设置是否允许覆盖下载文件
func WithAutoFileRenaming ¶
func WithAutoFileRenaming(d bool) OptionFunc
WithAutoFileRenaming 设置是否自动重命名文件,新文件名在名称之后扩展名之前加上一个点和一个数字(1..9999)
func WithAutoFilterFilename ¶
func WithAutoFilterFilename(d bool) OptionFunc
WithAutoFilterFilename 设置是否自动过滤掉文件名称中的非法字符
func WithBreakpointExt ¶
func WithBreakpointExt(d string) OptionFunc
WithBreakpointExt 断点文件扩展, 默认为 .temp.rain
func WithBreakpointResume ¶
func WithBreakpointResume(d bool) OptionFunc
WithBreakpointResume 设置是否启用断点续传
func WithEventExtend ¶ added in v0.2.0
func WithEventExtend(e ...ProgressEventExtend) OptionFunc
WithEventExtend 事件监听
type ProgressEvent ¶
type ProgressEvent interface {
Change(stat *Stat)
}
ProgressEvent 进度事件
func NewEventExtend ¶ added in v0.2.0
func NewEventExtend(e ...ProgressEventExtend) ProgressEvent
type ProgressEventExtend ¶ added in v0.2.0
type ProgressEventExtend interface { // Change 进度变化 Change(stat *EventExtend) // Close 中途暂停 Close(stat *EventExtend) // Error 出现错误 Error(stat *EventExtend) // Finish 下载成功 Finish(stat *EventExtend) }
ProgressEventExtend 进度事件扩展
type Rain ¶
type Rain struct {
// contains filtered or unexported fields
}
Rain 下载器
func (*Rain) AddOptions ¶ added in v0.3.0
func (rain *Rain) AddOptions(opt ...OptionFunc)
AddOptions 添加 New 时的 option
func (*Rain) ReplaceHeader ¶
ReplaceHeader 替换默认请求的 Header
func (*Rain) SetAllowOverwrite ¶
SetAllowOverwrite 设置是否允许覆盖下载文件
func (*Rain) SetAutoFileRenaming ¶
SetAutoFileRenaming 设置是否自动重命名文件,新文件名在名称之后扩展名之前加上一个点和一个数字(1..9999)
func (*Rain) SetAutoFilterFilename ¶
SetAutoFilterFilename 设置是否自动过滤掉文件名称中的非法字符
func (*Rain) SetBreakpointExt ¶
SetBreakpointExt 断点文件扩展, 默认为 .temp.rain
func (*Rain) SetBreakpointResume ¶
SetBreakpointResume 设置是否启用断点续传
func (*Rain) SetOptions ¶ added in v0.3.0
func (rain *Rain) SetOptions(opt []OptionFunc)
SetOptions 设置 New 时的 option
func (*Rain) SetRetryTime ¶
SetRetryNumber 设置请求重试的间隔时间
type RainControl ¶
type RainControl struct {
// contains filtered or unexported fields
}
RainControl 下载控制器
func (*RainControl) RunContext ¶
func (rc *RainControl) RunContext(ctx context.Context) (*RainControl, error)
RunContext 基于 Context 阻塞运行下载
func (*RainControl) SetSpeedLimit ¶ added in v0.3.0
func (rc *RainControl) SetSpeedLimit(d int)
SetSpeedLimit 设置下载限速,0 为不限速
func (*RainControl) StartContext ¶
func (rc *RainControl) StartContext(ctx context.Context) (*RainControl, error)
StartContext 基于 Context 非阻塞运行下载
func (*RainControl) WaitChan ¶ added in v0.4.0
func (rc *RainControl) WaitChan() <-chan error
WaitChan 阻塞返回 channel