method to download to file

This commit is contained in:
gutmet 2019-02-12 00:17:21 +01:00
parent c123400a6a
commit d97d95ed29

View File

@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
@ -312,7 +313,7 @@ func Notify(head string, body string) error {
return nil
}
func Download(url string) ([]byte, error) {
func DownloadAll(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, errors.New("Download: " + err.Error())
@ -324,3 +325,18 @@ func Download(url string) ([]byte, error) {
}
return data, err
}
func DownloadToFile(f string, url string) error {
resp, err := http.Get(url)
if err != nil {
return errors.New("DownloadToFile: " + err.Error())
}
defer resp.Body.Close()
file, err := os.Create(f)
if err != nil {
return errors.New("DownloadToFile: " + err.Error())
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
return err
}