临时提交

This commit is contained in:
liwei committed 2020-12-30 16:54:36 +08:00
1 parent 3215955056
commit 871185e7ea
5 files changed
+36 -15

No files matched your search

+3 -5
View File
@@ -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
}
+5
View File
@@ -0,0 +1,5 @@
package common
type Event interface {
Status() Status
}
+11
View File
@@ -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
View File
@@ -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}
}
}
+4 -10
View File
@@ -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 == "" {