|
| 1 | +package pkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "path" |
| 13 | + "strconv" |
| 14 | + |
| 15 | + "github.com/gosuri/uiprogress" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // ContentType is for the http header of content type |
| 20 | + ContentType = "Content-Type" |
| 21 | + // ApplicationForm is for the form submit |
| 22 | + ApplicationForm = "application/x-www-form-urlencoded" |
| 23 | +) |
| 24 | + |
| 25 | +// HTTPDownloader is the downloader for http request |
| 26 | +type HTTPDownloader struct { |
| 27 | + TargetFilePath string |
| 28 | + URL string |
| 29 | + ShowProgress bool |
| 30 | + InsecureSkipVerify bool |
| 31 | + |
| 32 | + UserName string |
| 33 | + Password string |
| 34 | + |
| 35 | + Proxy string |
| 36 | + ProxyAuth string |
| 37 | + |
| 38 | + Debug bool |
| 39 | + RoundTripper http.RoundTripper |
| 40 | +} |
| 41 | + |
| 42 | +// SetProxy set the proxy for a http |
| 43 | +func SetProxy(proxy, proxyAuth string, tr *http.Transport) (err error) { |
| 44 | + if proxy == "" { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + var proxyURL *url.URL |
| 49 | + if proxyURL, err = url.Parse(proxy); err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + tr.Proxy = http.ProxyURL(proxyURL) |
| 54 | + |
| 55 | + if proxyAuth != "" { |
| 56 | + basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(proxyAuth)) |
| 57 | + tr.ProxyConnectHeader = http.Header{} |
| 58 | + tr.ProxyConnectHeader.Add("Proxy-Authorization", basicAuth) |
| 59 | + } |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +func (h *HTTPDownloader) fetchProxyFromEnv(scheme string) { |
| 64 | + allProxy := os.Getenv("all_proxy") |
| 65 | + httpProxy := os.Getenv("http_proxy") |
| 66 | + httpsProxy := os.Getenv("https_proxy") |
| 67 | + |
| 68 | + if allProxy != "" { |
| 69 | + h.Proxy = allProxy |
| 70 | + } else { |
| 71 | + switch scheme { |
| 72 | + case "http": |
| 73 | + if httpProxy != "" { |
| 74 | + h.Proxy = httpProxy |
| 75 | + } |
| 76 | + case "https": |
| 77 | + if httpsProxy != "" { |
| 78 | + h.Proxy = httpsProxy |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// DownloadFile download a file with the progress |
| 85 | +func (h *HTTPDownloader) DownloadFile() error { |
| 86 | + filepath, downloadURL, showProgress := h.TargetFilePath, h.URL, h.ShowProgress |
| 87 | + // Get the data |
| 88 | + req, err := http.NewRequest(http.MethodGet, downloadURL, nil) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + if h.UserName != "" && h.Password != "" { |
| 94 | + req.SetBasicAuth(h.UserName, h.Password) |
| 95 | + } |
| 96 | + var tr http.RoundTripper |
| 97 | + if h.RoundTripper != nil { |
| 98 | + tr = h.RoundTripper |
| 99 | + } else { |
| 100 | + trp := &http.Transport{ |
| 101 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: h.InsecureSkipVerify}, |
| 102 | + } |
| 103 | + h.fetchProxyFromEnv(req.URL.Scheme) |
| 104 | + if err = SetProxy(h.Proxy, h.ProxyAuth, trp); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if h.Proxy != "" { |
| 109 | + basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(h.ProxyAuth)) |
| 110 | + req.Header.Add("Proxy-Authorization", basicAuth) |
| 111 | + } |
| 112 | + tr = trp |
| 113 | + } |
| 114 | + client := &http.Client{Transport: tr} |
| 115 | + var resp *http.Response |
| 116 | + |
| 117 | + if resp, err = client.Do(req); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + if resp.StatusCode != 200 { |
| 122 | + if h.Debug { |
| 123 | + if data, err := ioutil.ReadAll(resp.Body); err == nil { |
| 124 | + ioutil.WriteFile("debug-download.html", data, 0664) |
| 125 | + } |
| 126 | + } |
| 127 | + return fmt.Errorf("invalidate status code: %d", resp.StatusCode) |
| 128 | + } |
| 129 | + |
| 130 | + writer := &ProgressIndicator{ |
| 131 | + Title: "Downloading", |
| 132 | + } |
| 133 | + if showProgress { |
| 134 | + if total, ok := resp.Header["Content-Length"]; ok && len(total) > 0 { |
| 135 | + fileLength, err := strconv.ParseInt(total[0], 10, 64) |
| 136 | + if err == nil { |
| 137 | + writer.Total = float64(fileLength) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if err := os.MkdirAll(path.Dir(filepath), os.FileMode(0755)); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + // Create the file |
| 147 | + out, err := os.Create(filepath) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + defer out.Close() |
| 152 | + |
| 153 | + writer.Writer = out |
| 154 | + |
| 155 | + if showProgress { |
| 156 | + writer.Init() |
| 157 | + } |
| 158 | + |
| 159 | + // Write the body to file |
| 160 | + _, err = io.Copy(writer, resp.Body) |
| 161 | + return err |
| 162 | +} |
| 163 | + |
| 164 | +// ProgressIndicator hold the progress of io operation |
| 165 | +type ProgressIndicator struct { |
| 166 | + Writer io.Writer |
| 167 | + Reader io.Reader |
| 168 | + Title string |
| 169 | + |
| 170 | + // bytes.Buffer |
| 171 | + Total float64 |
| 172 | + count float64 |
| 173 | + bar *uiprogress.Bar |
| 174 | +} |
| 175 | + |
| 176 | +// Init set the default value for progress indicator |
| 177 | +func (i *ProgressIndicator) Init() { |
| 178 | + uiprogress.Start() // start rendering |
| 179 | + i.bar = uiprogress.AddBar(100) // Add a new bar |
| 180 | + |
| 181 | + // optionally, append and prepend completion and elapsed time |
| 182 | + i.bar.AppendCompleted() |
| 183 | + // i.bar.PrependElapsed() |
| 184 | + |
| 185 | + if i.Title != "" { |
| 186 | + i.bar.PrependFunc(func(_ *uiprogress.Bar) string { |
| 187 | + return fmt.Sprintf("%s: ", i.Title) |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// Write writes the progress |
| 193 | +func (i *ProgressIndicator) Write(p []byte) (n int, err error) { |
| 194 | + n, err = i.Writer.Write(p) |
| 195 | + i.setBar(n) |
| 196 | + return |
| 197 | +} |
| 198 | + |
| 199 | +// Read reads the progress |
| 200 | +func (i *ProgressIndicator) Read(p []byte) (n int, err error) { |
| 201 | + n, err = i.Reader.Read(p) |
| 202 | + i.setBar(n) |
| 203 | + return |
| 204 | +} |
| 205 | + |
| 206 | +func (i *ProgressIndicator) setBar(n int) { |
| 207 | + i.count += float64(n) |
| 208 | + |
| 209 | + if i.bar != nil { |
| 210 | + i.bar.Set((int)(i.count * 100 / i.Total)) |
| 211 | + } |
| 212 | +} |
0 commit comments