53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package fetcher
|
|
|
|
import (
|
|
"gitea.bvbej.com/bvbej/base-golang/pkg/downloader/base"
|
|
"gitea.bvbej.com/bvbej/base-golang/pkg/downloader/controller"
|
|
)
|
|
|
|
// Fetcher 对应协议的下载支持
|
|
type Fetcher interface {
|
|
// Setup 设置文件相关信息
|
|
Setup(ctl controller.Controller)
|
|
// Resolve 解析请求
|
|
Resolve(req *base.Request) (res *base.Resource, err error)
|
|
// Create 创建任务
|
|
Create(res *base.Resource, opts *base.Options) (err error)
|
|
// Start 开始
|
|
Start() (err error)
|
|
// Pause 暂停
|
|
Pause() (err error)
|
|
// Continue 继续
|
|
Continue() (err error)
|
|
// Progress 获取任务各个文件下载进度
|
|
Progress() Progress
|
|
// Wait 该方法会一直阻塞,直到任务下载结束
|
|
Wait() (err error)
|
|
}
|
|
|
|
type DefaultFetcher struct {
|
|
Ctl controller.Controller
|
|
DoneCh chan error
|
|
}
|
|
|
|
func (f *DefaultFetcher) Setup(ctl controller.Controller) {
|
|
f.Ctl = ctl
|
|
f.DoneCh = make(chan error, 1)
|
|
}
|
|
|
|
func (f *DefaultFetcher) Wait() (err error) {
|
|
return <-f.DoneCh
|
|
}
|
|
|
|
// Progress 获取任务中各个文件的已下载字节数
|
|
type Progress []int64
|
|
|
|
// TotalDownloaded 获取任务总下载字节数
|
|
func (p Progress) TotalDownloaded() int64 {
|
|
total := int64(0)
|
|
for _, d := range p {
|
|
total += d
|
|
}
|
|
return total
|
|
}
|