mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 23:03:43 +00:00
temp
This commit is contained in:
1 parent
bdce3c84a3
commit
74d0e8e0bc
6 files changed
+102
-73
No files matched your search
+38
-5
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/monkeyWie/gopeed-core/download/base"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TaskInfo struct {
|
||||
@@ -16,14 +17,16 @@ type TaskInfo struct {
|
||||
Files map[string]*base.FileInfo
|
||||
|
||||
fetcher base.Fetcher
|
||||
locker *sync.Mutex
|
||||
}
|
||||
|
||||
type FetcherBuilder func() (protocols []string, builder func() base.Fetcher)
|
||||
|
||||
type Downloader struct {
|
||||
*base.DefaultController
|
||||
fetchBuilders map[string]func() base.Fetcher
|
||||
tasks map[string]*TaskInfo
|
||||
*base.DefaultController
|
||||
listener func(taskInfo *TaskInfo, status base.Status)
|
||||
}
|
||||
|
||||
func NewDownloader(fbs ...FetcherBuilder) *Downloader {
|
||||
@@ -60,14 +63,14 @@ func (d *Downloader) Resolve(req *base.Request) (*base.Resource, error) {
|
||||
return fetcher.Resolve(req)
|
||||
}
|
||||
|
||||
func (d *Downloader) Create(res *base.Resource, opts *base.Options) error {
|
||||
func (d *Downloader) Create(res *base.Resource, opts *base.Options) (err error) {
|
||||
fetcher, err := d.buildFetcher(res.Req.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
err = fetcher.Create(res, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
id := uuid.New().String()
|
||||
d.tasks[id] = &TaskInfo{
|
||||
@@ -76,10 +79,40 @@ func (d *Downloader) Create(res *base.Resource, opts *base.Options) error {
|
||||
Opts: opts,
|
||||
Status: base.DownloadStatusReady,
|
||||
fetcher: fetcher,
|
||||
locker: new(sync.Mutex),
|
||||
}
|
||||
return fetcher.Start()
|
||||
go func() {
|
||||
err = fetcher.Start()
|
||||
if err != nil {
|
||||
d.emit(d.tasks[id], base.DownloadStatusError)
|
||||
}
|
||||
}()
|
||||
d.emit(d.tasks[id], base.DownloadStatusStart)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Downloader) Pause(id string) {
|
||||
task := d.tasks[id]
|
||||
task.locker.Lock()
|
||||
defer task.locker.Unlock()
|
||||
d.tasks[id].fetcher.Pause()
|
||||
d.emit(d.tasks[id], base.DownloadStatusPause)
|
||||
}
|
||||
|
||||
func (d *Downloader) Continue(id string) {
|
||||
task := d.tasks[id]
|
||||
task.locker.Lock()
|
||||
defer task.locker.Unlock()
|
||||
d.tasks[id].fetcher.Continue()
|
||||
d.emit(d.tasks[id], base.DownloadStatusStart)
|
||||
}
|
||||
|
||||
func (d *Downloader) Listener(fn func(taskInfo *TaskInfo, status base.Status)) {
|
||||
d.listener = fn
|
||||
}
|
||||
|
||||
func (d *Downloader) emit(taskInfo *TaskInfo, status base.Status) {
|
||||
if d.listener != nil {
|
||||
d.listener(taskInfo, status)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user