Commit a50fe6ab by Li Feifei

debug

parent 59dc54a3
Pipeline #15713 failed with stages
in 7 seconds
...@@ -9,6 +9,8 @@ import ( ...@@ -9,6 +9,8 @@ import (
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/net/ghttp"
"github.com/dustin/go-humanize"
"github.com/gogf/gf/util/gconv"
) )
func Get(url string) ([]byte, error) { func Get(url string) ([]byte, error) {
...@@ -25,6 +27,7 @@ func handle(response *ghttp.ClientResponse, err error) ([]byte, error) { ...@@ -25,6 +27,7 @@ func handle(response *ghttp.ClientResponse, err error) ([]byte, error) {
type WriteCounter struct { type WriteCounter struct {
Total uint64 Total uint64
Length uint64
} }
func (wc *WriteCounter) Write(p []byte) (int, error) { func (wc *WriteCounter) Write(p []byte) (int, error) {
...@@ -41,25 +44,11 @@ func (wc WriteCounter) PrintProgress() { ...@@ -41,25 +44,11 @@ func (wc WriteCounter) PrintProgress() {
// Return again and print current status of download // Return again and print current status of download
// We use the humanize package to print the bytes in a meaningful way (e.g. 10 MB) // We use the humanize package to print the bytes in a meaningful way (e.g. 10 MB)
fmt.Printf("\rDownloading... %s complete", wc.formatFileSize(wc.Total)) fmt.Printf("\rDownloading... %s %s complete", fmt.Sprintf("%.2f", gconv.Float64(wc.Total)/gconv.Float64(wc.Length)) + "%",
humanize.Bytes(wc.Total))
} }
func (wc WriteCounter) formatFileSize(fileSize uint64) string {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}
// 下载文件 // 下载文件
func Download(file string, path string) error { func Download(file string, path string) error {
...@@ -78,10 +67,11 @@ func Download(file string, path string) error { ...@@ -78,10 +67,11 @@ func Download(file string, path string) error {
} }
defer out.Close() defer out.Close()
// 然后将响应流和文件流对接起来 // 然后将响应流和文件流对接起来
counter := &WriteCounter{} counter := &WriteCounter{Length: gconv.Uint64(resp.Header.Get("Content-Length"))}
_, err = io.Copy(out, io.TeeReader(resp.Body, counter)) _, err = io.Copy(out, io.TeeReader(resp.Body, counter))
if err != nil && err == io.EOF { if err != nil && err == io.EOF {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment