Commit 59dc54a3 by Li Feifei

下载带进度条

parent 227f8a33
Pipeline #15712 passed with stages
in 34 seconds
package http package http
import ( import (
"fmt"
"io" "io"
"os" "os"
"strings"
"time" "time"
"github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/g"
...@@ -21,8 +23,47 @@ func handle(response *ghttp.ClientResponse, err error) ([]byte, error) { ...@@ -21,8 +23,47 @@ func handle(response *ghttp.ClientResponse, err error) ([]byte, error) {
return response.ReadAll(), nil return response.ReadAll(), nil
} }
type WriteCounter struct {
Total uint64
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
func (wc WriteCounter) PrintProgress() {
// Clear the line by using a character return to go back to the start and remove
// the remaining characters by filling it with spaces
fmt.Printf("\r%s", strings.Repeat(" ", 35))
// 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)
fmt.Printf("\rDownloading... %s complete", wc.formatFileSize(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 {
g.Log().Async().Infof("Download File: %s", path)
resp, err := g.Client().Get(file) resp, err := g.Client().Get(file)
if err != nil { if err != nil {
...@@ -35,10 +76,13 @@ func Download(file string, path string) error { ...@@ -35,10 +76,13 @@ func Download(file string, path string) error {
if err != nil { if err != nil {
return err return err
} }
defer out.Close() defer out.Close()
// 然后将响应流和文件流对接起来 // 然后将响应流和文件流对接起来
_, err = io.Copy(out, resp.Body) counter := &WriteCounter{}
_, err = io.Copy(out, io.TeeReader(resp.Body, counter))
if err != nil && err == io.EOF { if err != nil && err == io.EOF {
return nil return nil
...@@ -46,5 +90,6 @@ func Download(file string, path string) error { ...@@ -46,5 +90,6 @@ func Download(file string, path string) error {
if err != nil { if err != nil {
return err return err
} }
fmt.Print("\n")
return nil return nil
} }
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