This commit is contained in:
liwei committed 2019-04-24 22:59:54 +08:00
1 parent dace77c07c
commit 00468e4e4a
6 files changed
+124 -27

No files matched your search

+51 -18
View File
@@ -4,38 +4,39 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"mime" "mime"
"net/http" "net/http"
"net/http/cookiejar"
"net/url"
"os" "os"
"strconv" "strconv"
"strings" "strings"
) )
// Resolve 解析下载请求 // Resolve return the file response to be downloaded
func Resolve(request *Request) (*Response, error) { func Resolve(request *Request) (*Response, error) {
// Build request
httpRequest, err := http.NewRequest(strings.ToUpper(request.Method), request.URL, nil) httpRequest, err := http.NewRequest(strings.ToUpper(request.Method), request.URL, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
/* for k, v := range request.Header { for k, v := range request.Header {
httpRequest.Header.Add(k, v) httpRequest.Header.Add(k, v)
} */ }
httpClient := &http.Client{} // Use "Range" header to resolve
httpRequest.Header.Add("Range", "bytes=0-0")
// Cookie handle
jar, _ := cookiejar.New(nil)
httpClient := &http.Client{Jar: jar}
response, err := httpClient.Do(httpRequest) response, err := httpClient.Do(httpRequest)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if response.StatusCode != 200 && response.StatusCode != 206 { if response.StatusCode != 200 && response.StatusCode != 206 {
fmt.Println(response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
return nil, fmt.Errorf("Response status error:%d", response.StatusCode) return nil, fmt.Errorf("Response status error:%d", response.StatusCode)
} }
ret := &Response{} ret := &Response{}
// Get file name by "Content-Disposition"
//解析文件名
contentDisposition := response.Header.Get("Content-Disposition") contentDisposition := response.Header.Get("Content-Disposition")
if contentDisposition != "" { if contentDisposition != "" {
_, params, _ := mime.ParseMediaType(contentDisposition) _, params, _ := mime.ParseMediaType(contentDisposition)
@@ -44,19 +45,51 @@ func Resolve(request *Request) (*Response, error) {
ret.Name = filename ret.Name = filename
} }
} }
// Get file name by URL
if ret.Name == "" { if ret.Name == "" {
parse, err := url.Parse(request.URL)
if err == nil {
// e.g. /files/test.txt => test.txt
ret.Name = subLastSlash(parse.Path)
}
} }
//解析文件大小 // Unknow file name
contentLength := response.Header.Get("Content-Length") if ret.Name == "" {
if contentLength != "" { ret.Name = "unknow"
ret.size, _ = strconv.ParseInt(contentLength, 10, 64) }
// Is support range
ret.Range = response.StatusCode == 206
// Get file size
if ret.Range {
contentRange := response.Header.Get("Content-Range")
if contentRange != "" {
// e.g. bytes 0-1000/1001 => 1001
total := subLastSlash(contentRange)
if total != "" && total != "*" {
parse, err := strconv.ParseInt(total, 10, 64)
if err != nil {
return nil, err
}
ret.Size = parse
}
}
} else {
contentLength := response.Header.Get("Content-Length")
if contentLength != "" {
ret.Size, _ = strconv.ParseInt(contentLength, 10, 64)
}
} }
//判断是否支持分段下载
ret.Partial = ret.size > 0 && response.StatusCode == 206
return ret, nil return ret, nil
} }
func subLastSlash(str string) string {
index := strings.LastIndex(str, "/")
if index != -1 {
return str[index+1:]
}
return ""
}
// Down 下载 // Down 下载
func Down(request *Request) { func Down(request *Request) {
httpRequest, err := http.NewRequest(request.Method, request.URL, bytes.NewReader(request.content)) httpRequest, err := http.NewRequest(request.Method, request.URL, bytes.NewReader(request.content))
+18 -3
View File
@@ -1,7 +1,10 @@
package down package down
import ( import (
"fmt"
"net/url"
"reflect" "reflect"
"strings"
"testing" "testing"
) )
@@ -42,6 +45,7 @@ func TestDown(t *testing.T) {
} }
func TestResolve(t *testing.T) { func TestResolve(t *testing.T) {
// os.Setenv("HTTP_PROXY", "http://127.0.0.1:8888")
type args struct { type args struct {
request *Request request *Request
} }
@@ -56,19 +60,19 @@ func TestResolve(t *testing.T) {
args{ args{
&Request{ &Request{
"get", "get",
"https://github.com/proxyee-down-org/proxyee-down/releases/download/3.4/proxyee-down-main.jar", "http://github.com/proxyee-down-org/proxyee-down/releases/download/3.4/proxyee-down-main.jar",
map[string]string{ map[string]string{
"Host": "github.com", "Host": "github.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Referer": "https://github.com/proxyee-down-org/proxyee-down/releases", "Referer": "http://github.com/proxyee-down-org/proxyee-down/releases",
"Accept-Encoding": "gzip, deflate, br", "Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
}, },
nil, nil,
}, },
}, },
&Response{"proxyee-down-main.jar", 30159703, false}, &Response{"proxyee-down-main.jar", 30159703, true},
false, false,
}, },
} }
@@ -85,3 +89,14 @@ func TestResolve(t *testing.T) {
}) })
} }
} }
func TestTemp(t *testing.T) {
parse, _ := url.Parse("http://www.baidu.com/asda/text.txt?a=11&b=333")
arr := strings.Split(parse.Path, "/")
for i := range arr {
fmt.Println(arr[i])
}
// var begin, end, total int64
//fmt.Sscanf("bytes 0-1000/1001", "%s %d-%d/%d", _, &begin, &end, &total)
//fmt.Printf("%d-%d/%d", begin, end, total)
}
+3 -3
View File
@@ -1,7 +1,7 @@
package down package down
type Response struct { type Response struct {
Name string Name string
size int64 Size int64
Partial bool Range bool
} }
+10 -1
View File
@@ -3,6 +3,15 @@ module pdown-core
go 1.12 go 1.12
require ( require (
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02 // indirect github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 // indirect
github.com/getlantern/golog v0.0.0-20170508214112-cca714f7feb5 // indirect
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 // indirect
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 // indirect
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f // indirect
github.com/getlantern/systray v0.0.0-20190131073753-26d5b920200d
github.com/go-stack/stack v1.8.0 // indirect
github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb
) )
+25 -2
View File
@@ -1,4 +1,27 @@
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02 h1:nI4Q7WQDcng8eRmi8bRP1SXlJIYLkgdgR7ZhJuzPbhs= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02/go.mod h1:iYGcTYIPUvEWhFo6aKUuLchs+AV4ssYdyuBbQJZGcBk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 h1:NRUJuo3v3WGC/g5YiyF790gut6oQr5f3FBI88Wv0dx4=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 h1:6uJ+sZ/e03gkbqZ0kUG6mfKoqDb4XMAzMIwlajq19So=
github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7/go.mod h1:l+xpFBrCtDLpK9qNjxs+cHU6+BAdlBaxHqikB6Lku3A=
github.com/getlantern/golog v0.0.0-20170508214112-cca714f7feb5 h1:Okd7vkn9CfIgDBj1ST/vtBTCfD/kxIhYD412K+FRKPc=
github.com/getlantern/golog v0.0.0-20170508214112-cca714f7feb5/go.mod h1:Vwx1Cg64gCdIalad44uvQsKZw6LsVczIKZrUBStEjVw=
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 h1:micT5vkcr9tOVk1FiH8SWKID8ultN44Z+yzd2y/Vyb0=
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7/go.mod h1:dD3CgOrwlzca8ed61CsZouQS5h5jIzkK9ZWrTcf0s+o=
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 h1:XYzSdCbkzOC0FDNrgJqGRo8PCMFOBFL9py72DRs7bmc=
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55/go.mod h1:6mmzY2kW1TOOrVy+r41Za2MxXM+hhqTtY3oBKd2AgFA=
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f h1:wrYrQttPS8FHIRSlsrcuKazukx/xqO/PpLZzZXsF+EA=
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f/go.mod h1:D5ao98qkA6pxftxoqzibIBBrLSUli+kYnJqrgBf9cIA=
github.com/getlantern/systray v0.0.0-20190131073753-26d5b920200d h1:4P2eDMAoQcQoWIIKCNIkuVbQb+paRmpMxVXVfbs7B4U=
github.com/getlantern/systray v0.0.0-20190131073753-26d5b920200d/go.mod h1:7Splj4WBQSps8jODnMgrIV6goKL0N1HR+mhCAEVWlA0=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc h1:uhnyuvDwdKbjemAXHKsiEZOPagHim2nRjMcazH1g26A=
github.com/oxtoacart/bpool v0.0.0-20190227141107-8c4636f812cc/go.mod h1:L3UMQOThbttwfYRNFOWLLVXMhk5Lkio4GGOtw5UrxS0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb h1:zVjnyZIM7UtkG3dNckiudIm+TUHkZqi5xlVQPd3J6/c= github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb h1:zVjnyZIM7UtkG3dNckiudIm+TUHkZqi5xlVQPd3J6/c=
github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb/go.mod h1:a1CV8KR4Dd1eP2g+mEijGOp+HKczwdKHWyx0aPHKvo4= github.com/zserge/webview v0.0.0-20190123072648-16c93bcaeaeb/go.mod h1:a1CV8KR4Dd1eP2g+mEijGOp+HKczwdKHWyx0aPHKvo4=
+17
View File
@@ -1,9 +1,26 @@
package main package main
import "github.com/zserge/webview" import "github.com/zserge/webview"
import "github.com/getlantern/systray"
func main() { func main() {
systray.Run(onReady,onExit)
// Open wikipedia in a 800x600 resizable window // Open wikipedia in a 800x600 resizable window
webview.Open("Minimal webview example", webview.Open("Minimal webview example",
"https://www.baidu.com", 800, 600, true) "https://www.baidu.com", 800, 600, true)
}
func onReady() {
systray.SetIcon(icon.Data)
systray.SetTitle("Awesome App")
systray.SetTooltip("Pretty awesome超级棒")
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
// Sets the icon of a menu item. Only available on Mac.
mQuit.SetIcon(icon.Data)
}
func onExit() {
// clean up here
} }