downloader

package
v0.0.0-...-a2fe341 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DownloadingFileSuffix 断点续传临时文件后缀
	DownloadingFileSuffix = ".baidupcs_go_downloading"
)
View Source
var (

	// FileNameRE 正则表达式: 匹配文件名
	FileNameRE = regexp.MustCompile("filename=\"(.*?)\"")
)

Functions

func DoDownload

func DoDownload(url string, fileName string, sizeofFile int64)

DoDownload 简单网络下载器, 使用默认下载线程, 通过调用 SetMaxThread 来修改默认下载线程

func HTTPGet

func HTTPGet(urlStr string) (body []byte, err error)

HTTPGet 简单实现 http 访问 GET 请求

func SetCacheSize

func SetCacheSize(size int64)

SetCacheSize 设置缓冲大小

func SetMaxParallel

func SetMaxParallel(t int)

SetMaxParallel 设置最大线程

Types

type Block

type Block struct {
	Begin int64 `json:"begin"`
	End   int64 `json:"end"`

	Final bool `json:"isfinal"` // 最后线程, 因为最后的下载线程, 需要另外做处理
	// contains filtered or unexported fields
}

Block 下载区块

type FileDl

type FileDl struct {
	URL  string   // 下载地址
	Size int64    // 文件大小
	File *os.File // 要写入的文件

	BlockList blockList // 用于记录未下载的文件块起始位置

	*HTTPClient // http client
	// contains filtered or unexported fields
}

FileDl 下载详情

func NewFileDl

func NewFileDl(h *HTTPClient, url, savePath string, size int64) (*FileDl, error)

NewFileDl 创建新的文件下载

如果 size <= 0 则自动获取文件大小

func (FileDl) GetStatus

func (f FileDl) GetStatus() status

GetStatus 获取下载统计信息

func (*FileDl) OnError

func (f *FileDl) OnError(fn func(int, error))

OnError 任务出错时触发的事件

errCode为错误码,errStr为错误描述

func (*FileDl) OnFinish

func (f *FileDl) OnFinish(fn func())

OnFinish 任务完成时触发的事件

func (*FileDl) OnStart

func (f *FileDl) OnStart(fn func())

OnStart 任务开始时触发的事件

func (*FileDl) Start

func (f *FileDl) Start()

Start 开始下载

type HTTPClient

type HTTPClient struct {
	Client http.Client
}

HTTPClient http client

func NewHTTPClient

func NewHTTPClient() *HTTPClient

NewHTTPClient 返回 HTTPClient 的指针, 预设了一些配置

func (*HTTPClient) ClearCookiejar

func (h *HTTPClient) ClearCookiejar()

ClearCookiejar 清空 cookie

func (*HTTPClient) Fetch

func (h *HTTPClient) Fetch(method string, urlStr string, post interface{}, header map[string]string) (body []byte, err error)

Fetch 实现 http/https 访问 和 GET/POST 请求, 根据给定的 method (GET, POST, HEAD, PUT 等等), urlStr (网址), post (post 数据), header (header 请求头数据), 进行网站访问。 返回值分别为 网站主体, 错误

func (*HTTPClient) SetCookiejar

func (h *HTTPClient) SetCookiejar(c *cookiejar.Jar)

SetCookiejar 设置 cookie

func (*HTTPClient) SetGzip

func (h *HTTPClient) SetGzip(b bool)

SetGzip 是否启用Gzip

func (*HTTPClient) SetHTTPSecure

func (h *HTTPClient) SetHTTPSecure(b bool)

SetHTTPSecure 是否启用 https 安全检查

func (*HTTPClient) SetKeepAlive

func (h *HTTPClient) SetKeepAlive(b bool)

SetKeepAlive 设置 Keep-Alive

func (*HTTPClient) SetResponseHeaderTimeout

func (h *HTTPClient) SetResponseHeaderTimeout(t time.Duration)

SetResponseHeaderTimeout 设置目标服务器响应超时时间

func (*HTTPClient) SetTimeout

func (h *HTTPClient) SetTimeout(t time.Duration)

SetTimeout 设置 http 请求超时时间 默认30s

Jump to

Keyboard shortcuts

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