From eef73867db718d4a6b706d591508d3e22c9949df Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 04 八月 2020 15:26:43 +0800
Subject: [PATCH] update esutil

---
 extend/util/requtil.go |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/extend/util/requtil.go b/extend/util/requtil.go
index 203d21f..0bb8280 100644
--- a/extend/util/requtil.go
+++ b/extend/util/requtil.go
@@ -3,10 +3,15 @@
 import (
 	"bytes"
 	"encoding/json"
+	"fmt"
 	"github.com/ajg/form"
+	"io"
 	"io/ioutil"
 	"net/http"
+	"os"
 	"strings"
+	"github.com/dustin/go-humanize"
+	"time"
 )
 
 const (
@@ -59,7 +64,9 @@
 			request.Header.Add(key, val)
 		}
 	}
-	client := &http.Client{}
+	client := &http.Client{
+		Timeout: time.Second * 5,
+	}
 	resp, err := client.Do(request)
 	if err != nil {
 		return resultBytes, err
@@ -70,4 +77,47 @@
 		return resultBytes, err
 	}
 	return resultBytes, nil
-}
\ No newline at end of file
+}
+
+
+type WriteCounter struct {
+	Total  uint64
+}
+
+func (wc WriteCounter) PrintProgress() {
+	fmt.Printf("\r%s", strings.Repeat(" ", 35))
+	fmt.Printf("/rDownloading... %s complete", humanize.Bytes(wc.Total))
+}
+
+func (wc *WriteCounter) Write(p []byte) (int, error) {
+	n := len(p)
+	wc.Total = uint64(n)
+	wc.PrintProgress()
+	return n, nil
+}
+
+//涓嬭浇鏂囦欢
+func DownloadFile(fPath string, url string) error {
+	f, err := os.Create(fPath)
+	if err != nil {
+		return err
+	}
+	resp, err := http.Get(url)
+	if err != nil {
+		f.Close()
+		return err
+	}
+	defer resp.Body.Close()
+	counter := &WriteCounter{}
+	if _, err = io.Copy(f, io.TeeReader(resp.Body, counter)); err != nil {
+		f.Close()
+		return err
+	}
+	fmt.Printf("\n")
+	f.Close()
+	if err = os.Rename(fPath, fPath);err != nil {
+		return err
+	}
+	return nil
+}
+

--
Gitblit v1.8.0