utility

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CONTROLFILESIZE 控制文件最小长度
	CONTROLFILESIZE = 34
	// DOWNLOADBLOCKSIZE 一个下载块的长度
	DOWNLOADBLOCKSIZE = 24
	// CONTROLFILEHEAD 控制文件头
	CONTROLFILEHEAD = "rain"
)

Variables

View Source
var HttpClient = &http.Client{
	Transport: &http.Transport{

		Proxy: http.ProxyFromEnvironment,

		DisableCompression: true,

		ResponseHeaderTimeout: 5 * time.Second,

		TLSHandshakeTimeout: 10 * time.Second,

		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},

		MaxIdleConnsPerHost: 10,
	},

	Timeout: time.Minute * 5,
}

HttpClient 全局使用的 HTTP 客户端

Functions

func FileExist

func FileExist(path string) bool

FileExist 判断文件是否存在

func FilterFileNameFormWindows

func FilterFileNameFormWindows(name string) string

func FormatFileSize

func FormatFileSize(fileSize int64) string

formatFileSize 字节的单位转换 保留两位小数

func GetFileName

func GetFileName(uri, contentDisposition, contentType string, headinfo []byte) string

GetFileName 自动获取资源文件名称 名称获取的顺序:响应头 content-disposition 的 filename 字段、uri.Path 中的 \ 最后的字符、随机生成 文件后缀的获取顺序:文件魔数、响应头 content-type 匹配系统中的库

func GetStringLength

func GetStringLength(str string) int

GetStringLength 获取字符串的长度

func SetHttpMaxIdleConnsPerHost

func SetHttpMaxIdleConnsPerHost(d int)

func SetHttpProxy

func SetHttpProxy(d func(*http.Request) (*url.URL, error))

func SetHttpResponseHeaderTimeout

func SetHttpResponseHeaderTimeout(d time.Duration)

func SetHttpTLSHandshakeTimeout

func SetHttpTLSHandshakeTimeout(d time.Duration)

func SetHttpTimeout

func SetHttpTimeout(d time.Duration)

Types

type ControlFile

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

ControlFile 控制文件,记录了断点下载所需要的信息

func NewControlfile

func NewControlfile(path string, size int64, perm fs.FileMode) (*ControlFile, error)

NewControlfile 创建控制文件

func (*ControlFile) AddCompleted

func (cf *ControlFile) AddCompleted(key int, completed int64)

AddCompleted 添加数据块已完成的数据量

func (*ControlFile) AddDownloadPiece

func (cf *ControlFile) AddDownloadPiece(completed, start, end int64) int

AddDownloadPiece 添加下载块

func (*ControlFile) Close

func (cf *ControlFile) Close()

Close 释放控制文件

func (*ControlFile) Encoding

func (cf *ControlFile) Encoding() *bytes.Buffer

Encoding 编码输出二进制

func (*ControlFile) GetBreakpointResume

func (cf *ControlFile) GetBreakpointResume() bool

GetBreakpointResume 获取是否需要单点续传

func (*ControlFile) GetCompletedLength

func (cf *ControlFile) GetCompletedLength() int64

GetCompletedLength 获取已下载长度

func (*ControlFile) GetDownloadPiece

func (cf *ControlFile) GetDownloadPiece() []*DownloadPiece

GetDownloadPiece 获取下载块

func (*ControlFile) ParseControlfile

func (cf *ControlFile) ParseControlfile(data []byte) *ControlFile

ParseControlfile 解析控制文件

func (*ControlFile) Remove

func (cf *ControlFile) Remove() error

Remove 删除控制文件

func (*ControlFile) Save

func (cf *ControlFile) Save()

save 保存控制文件

func (*ControlFile) SetSaveEvent

func (cf *ControlFile) SetSaveEvent(d func())

SetSaveEvent 设置控制文件保存事件

type DownloadPiece

type DownloadPiece struct {
	// completed 8 字节 已下载大小
	Completed int64
	// start 8 字节 开始字节
	Start int64
	// end 8 字节 结束字节
	End int64
}

DownloadPiece 下载块信息

type FileController

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

FileController 文件控制器

func NewFileController

func NewFileController(filepath string, cl *int64, diskCache, speedLimit int, perm fs.FileMode) (*FileController, error)

NewFileController 创建文件控制器

func (*FileController) Close

func (fileController *FileController) Close() error

func (*FileController) Copy

func (fileController *FileController) Copy(ctx context.Context, src io.Reader, id int, start int64, dataSize int64) error

Copy 数据拷贝

func (*FileController) Rateread

func (fileController *FileController) Rateread(ctx context.Context, src io.Reader, buf []byte) (n int, err error)

Rateread 限速读取

func (*FileController) SetWriteChangeEvent

func (fileController *FileController) SetWriteChangeEvent(d func(id int, n int))

func (*FileController) Sync

func (fileController *FileController) Sync() error

func (*FileController) Truncate

func (fileController *FileController) Truncate(size int64) error

type FileWriteAt

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

FileWriteAt 指定位置写入

func NewFileWriteAt

func NewFileWriteAt(id int, file *os.File, start int64, w func(id int, n int)) *FileWriteAt

NewFileWriteAt 创建指定文件位置写入器

func (*FileWriteAt) Write

func (fileat *FileWriteAt) Write(p []byte) (n int, err error)

Write 基于指针位置写入数据

type IoProxyReader

type IoProxyReader struct {
	Reader io.Reader
	Send   func(n int)
}

IoProxyReader 代理 io 读

func (*IoProxyReader) Close

func (r *IoProxyReader) Close() (err error)

Close the wrapped reader when it implements io.Closer

func (*IoProxyReader) Read

func (r *IoProxyReader) Read(p []byte) (n int, err error)

Read 读

type WaitGroupPool

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

WaitGroupPool sync.WaitGroup 池

func NewWaitGroupPool

func NewWaitGroupPool(size int) *WaitGroupPool

NewWaitGroupPool 创建一个 size 大小的 sync.WaitGroup 池

func (*WaitGroupPool) Add

func (p *WaitGroupPool) Add()

Add 添加一个 sync.WaitGroup 线程

func (*WaitGroupPool) AllDone

func (p *WaitGroupPool) AllDone() <-chan error

SyneDone 非阻塞等待时全部完成

func (*WaitGroupPool) Count

func (p *WaitGroupPool) Count() int

Count 未完成线程的个数

func (*WaitGroupPool) Done

func (p *WaitGroupPool) Done()

Done 完成一个 sync.WaitGroup 线程

func (*WaitGroupPool) Error

func (p *WaitGroupPool) Error(err error)

Error 出现错误

func (*WaitGroupPool) Syne

func (p *WaitGroupPool) Syne()

Syne 非阻塞等待

func (*WaitGroupPool) Wait

func (p *WaitGroupPool) Wait()

Wait 阻塞等待 sync.WaitGroup 清零

Directories

Path Synopsis
Package rate provides a rate limiter.
Package rate provides a rate limiter.

Jump to

Keyboard shortcuts

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