mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-17 22:03:41 +00:00
临时提交
This commit is contained in:
1 parent
3215955056
commit
871185e7ea
5 files changed
+36
-15
No files matched your search
@@ -57,14 +57,12 @@ func NewController() *Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Touch(name string, size int64) (file *os.File, err error) {
|
func (c *Controller) Touch(name string, size int64) (file *os.File, err error) {
|
||||||
|
file, err = os.Create(name)
|
||||||
if size > 0 {
|
if size > 0 {
|
||||||
err := os.Truncate(name, size)
|
err = os.Truncate(name, size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
file, err = os.OpenFile(name, os.O_RDWR, 0666)
|
|
||||||
} else {
|
|
||||||
file, err = os.Create(name)
|
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.Files[name] = file
|
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) {
|
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 {
|
if err == nil {
|
||||||
c.Files[name] = file
|
c.Files[name] = file
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
type Event interface {
|
||||||
|
Status() Status
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ type Process struct {
|
|||||||
chunks []*model.Chunk
|
chunks []*model.Chunk
|
||||||
|
|
||||||
pauseCond *sync.Cond
|
pauseCond *sync.Cond
|
||||||
|
eventCh chan common.Event
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProcess(fetcher *Fetcher, res *common.Resource, opts *common.Options) *Process {
|
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 {
|
func (p *Process) Start() error {
|
||||||
ctl := p.fetcher.GetCtl()
|
ctl := p.fetcher.GetCtl()
|
||||||
// 创建文件
|
// 创建文件
|
||||||
@@ -68,6 +74,7 @@ func (p *Process) Start() error {
|
|||||||
p.clients = make([]*http.Response, 1)
|
p.clients = make([]*http.Response, 1)
|
||||||
p.chunks[0] = model.NewChunk(0, 0)
|
p.chunks[0] = model.NewChunk(0, 0)
|
||||||
}
|
}
|
||||||
|
p.dispatch(common.DownloadStatusStart)
|
||||||
return p.fetch()
|
return p.fetch()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,3 +250,9 @@ func (p *Process) fetchChunk(index int, name string, chunk *model.Chunk) (err er
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Process) dispatch(status common.Status) {
|
||||||
|
if p.eventCh != nil {
|
||||||
|
p.eventCh <- &Event{status: status}
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-10
@@ -6,8 +6,8 @@ import (
|
|||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -32,21 +32,15 @@ func Resolve(request *Request) (*Response, error) {
|
|||||||
}
|
}
|
||||||
ret := &Response{}
|
ret := &Response{}
|
||||||
// Get file name by "Content-Disposition"
|
// Get file name by "Content-Disposition"
|
||||||
contentDisposition := response.Header.Get("Content-Disposition")
|
if contentDisposition := response.Header.Get("Content-Disposition"); contentDisposition != "" {
|
||||||
if contentDisposition != "" {
|
|
||||||
_, params, _ := mime.ParseMediaType(contentDisposition)
|
_, params, _ := mime.ParseMediaType(contentDisposition)
|
||||||
filename := params["filename"]
|
if filename := params["filename"]; filename != "" {
|
||||||
if filename != "" {
|
|
||||||
ret.Name = filename
|
ret.Name = filename
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Get file name by URL
|
// Get file name by URL
|
||||||
if ret.Name == "" {
|
if ret.Name == "" {
|
||||||
parse, err := url.Parse(httpRequest.URL.String())
|
ret.Name = path.Base(httpRequest.URL.Path)
|
||||||
if err == nil {
|
|
||||||
// e.g. /files/test.txt => test.txt
|
|
||||||
ret.Name = subLastSlash(parse.Path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Unknown file name
|
// Unknown file name
|
||||||
if ret.Name == "" {
|
if ret.Name == "" {
|
||||||
|
|||||||
Reference in new issue
Block a user