|
@ -5,6 +5,7 @@ import ( |
|
|
"io" |
|
|
"io" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"net/http" |
|
|
"net/http" |
|
|
|
|
|
"net/url" |
|
|
"os" |
|
|
"os" |
|
|
"strings" |
|
|
"strings" |
|
|
) |
|
|
) |
|
@ -69,21 +70,35 @@ func CopyFile(source string, target string) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func DownloadFile(url string, file string) { |
|
|
func DownloadFile(fileUrl string, filePath string, proxyUrl string) { |
|
|
log.Debugf("File download `%s` => `%s`", url, file) |
|
|
log.Debugf("File download `%s` => `%s`", fileUrl, filePath) |
|
|
resp, err := http.Get(url) |
|
|
client := http.Client{} |
|
|
defer resp.Body.Close() |
|
|
if proxyUrl != "" { // use proxy for download
|
|
|
|
|
|
log.Infof("File download via proxy -> %s", proxyUrl) |
|
|
|
|
|
rawUrl, _ := url.Parse(proxyUrl) |
|
|
|
|
|
client = http.Client{ |
|
|
|
|
|
Transport: &http.Transport{ |
|
|
|
|
|
Proxy: http.ProxyURL(rawUrl), |
|
|
|
|
|
}, |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
resp, err := client.Get(fileUrl) |
|
|
|
|
|
defer func() { |
|
|
|
|
|
if resp != nil { |
|
|
|
|
|
resp.Body.Close() |
|
|
|
|
|
} |
|
|
|
|
|
}() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Errorf("Download `%s` error -> %v", url, err) |
|
|
log.Errorf("Download `%s` error -> %v", fileUrl, err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
output, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
|
|
output, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) |
|
|
defer output.Close() |
|
|
defer output.Close() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Panicf("Open `%s` error -> %v", file, err) |
|
|
log.Panicf("Open `%s` error -> %v", filePath, err) |
|
|
} |
|
|
} |
|
|
if _, err = io.Copy(output, resp.Body); err != nil { |
|
|
if _, err = io.Copy(output, resp.Body); err != nil { |
|
|
log.Panicf("File `%s` save error -> %v", file, err) |
|
|
log.Panicf("File `%s` save error -> %v", filePath, err) |
|
|
} |
|
|
} |
|
|
log.Infof("Download success `%s` => `%s`", url, file) |
|
|
log.Infof("Download success `%s` => `%s`", fileUrl, filePath) |
|
|
} |
|
|
} |
|
|