2019-04-17 20:26:44 +01:00
|
|
|
package fileio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadFile - Download file and store it
|
2021-06-09 17:07:07 +01:00
|
|
|
func DownloadFile(outputFilename string, url string) error {
|
2019-04-17 20:26:44 +01:00
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
2021-06-09 17:07:07 +01:00
|
|
|
return err
|
2019-04-17 20:26:44 +01:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(outputFilename)
|
|
|
|
if err != nil {
|
2021-06-09 17:07:07 +01:00
|
|
|
return err
|
2019-04-17 20:26:44 +01:00
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-06-09 17:07:07 +01:00
|
|
|
return err
|
2019-04-17 20:26:44 +01:00
|
|
|
}
|
2021-06-09 17:07:07 +01:00
|
|
|
|
|
|
|
return nil
|
2019-04-17 20:26:44 +01:00
|
|
|
}
|