From 871185e7ea0cc3329b39f99f08d8e1fa75c9e447 Mon Sep 17 00:00:00 2001 From: liwei Date: Wed, 30 Dec 2020 16:54:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- download/common/controller.go | 8 +++----- download/common/event.go | 5 +++++ download/http/envent.go | 11 +++++++++++ download/http/process.go | 13 +++++++++++++ protocol/http/client.go | 14 ++++---------- 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 download/common/event.go create mode 100644 download/http/envent.go diff --git a/download/common/controller.go b/download/common/controller.go index b02b29f9..a77d15ba 100644 --- a/download/common/controller.go +++ b/download/common/controller.go @@ -57,14 +57,12 @@ func NewController() *Controller { } func (c *Controller) Touch(name string, size int64) (file *os.File, err error) { + file, err = os.Create(name) if size > 0 { - err := os.Truncate(name, size) + err = os.Truncate(name, size) if err != nil { return nil, err } - file, err = os.OpenFile(name, os.O_RDWR, 0666) - } else { - file, err = os.Create(name) } if err == nil { c.Files[name] = file @@ -73,7 +71,7 @@ func (c *Controller) Touch(name string, size int64) (file *os.File, err error) { } func (c *Controller) Open(name string) (file *os.File, err error) { - file, err = os.OpenFile(name, os.O_RDWR, 0666) + file, err = os.OpenFile(name, os.O_RDWR, os.ModePerm) if err == nil { c.Files[name] = file } diff --git a/download/common/event.go b/download/common/event.go new file mode 100644 index 00000000..57899950 --- /dev/null +++ b/download/common/event.go @@ -0,0 +1,5 @@ +package common + +type Event interface { + Status() Status +} diff --git a/download/http/envent.go b/download/http/envent.go new file mode 100644 index 00000000..02ed52b8 --- /dev/null +++ b/download/http/envent.go @@ -0,0 +1,11 @@ +package http + +import "github.com/monkeyWie/gopeed-core/download/common" + +type Event struct { + status common.Status +} + +func (e *Event) Status() common.Status { + return e.status +} diff --git a/download/http/process.go b/download/http/process.go index b6120c2b..bfc6f1e1 100644 --- a/download/http/process.go +++ b/download/http/process.go @@ -20,6 +20,7 @@ type Process struct { chunks []*model.Chunk pauseCond *sync.Cond + eventCh chan common.Event } func NewProcess(fetcher *Fetcher, res *common.Resource, opts *common.Options) *Process { @@ -33,6 +34,11 @@ func NewProcess(fetcher *Fetcher, res *common.Resource, opts *common.Options) *P } } +func (p *Process) Listen() <-chan common.Event { + p.eventCh = make(chan common.Event) + return p.eventCh +} + func (p *Process) Start() error { ctl := p.fetcher.GetCtl() // 创建文件 @@ -68,6 +74,7 @@ func (p *Process) Start() error { p.clients = make([]*http.Response, 1) p.chunks[0] = model.NewChunk(0, 0) } + p.dispatch(common.DownloadStatusStart) return p.fetch() } @@ -243,3 +250,9 @@ func (p *Process) fetchChunk(index int, name string, chunk *model.Chunk) (err er } return } + +func (p *Process) dispatch(status common.Status) { + if p.eventCh != nil { + p.eventCh <- &Event{status: status} + } +} diff --git a/protocol/http/client.go b/protocol/http/client.go index f1c1aa4e..68e17bf1 100644 --- a/protocol/http/client.go +++ b/protocol/http/client.go @@ -6,8 +6,8 @@ import ( "mime" "net/http" "net/http/cookiejar" - "net/url" "os" + "path" "strconv" "strings" "sync" @@ -32,21 +32,15 @@ func Resolve(request *Request) (*Response, error) { } ret := &Response{} // Get file name by "Content-Disposition" - contentDisposition := response.Header.Get("Content-Disposition") - if contentDisposition != "" { + if contentDisposition := response.Header.Get("Content-Disposition"); contentDisposition != "" { _, params, _ := mime.ParseMediaType(contentDisposition) - filename := params["filename"] - if filename != "" { + if filename := params["filename"]; filename != "" { ret.Name = filename } } // Get file name by URL if ret.Name == "" { - parse, err := url.Parse(httpRequest.URL.String()) - if err == nil { - // e.g. /files/test.txt => test.txt - ret.Name = subLastSlash(parse.Path) - } + ret.Name = path.Base(httpRequest.URL.Path) } // Unknown file name if ret.Name == "" {