Skip to content

Commit

Permalink
临时提交
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Dec 30, 2020
1 parent 3215955 commit 871185e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
8 changes: 3 additions & 5 deletions download/common/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions download/common/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package common

type Event interface {
Status() Status
}
11 changes: 11 additions & 0 deletions download/http/envent.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions download/http/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
// 创建文件
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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}
}
}
14 changes: 4 additions & 10 deletions protocol/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"mime"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path"
"strconv"
"strings"
"sync"
Expand All @@ -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 == "" {
Expand Down

0 comments on commit 871185e

Please sign in to comment.