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" "errors"
"flag" "flag"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
@ -312,7 +313,7 @@ func Notify(head string, body string) error {
return nil return nil
} }
func Download(url string) ([]byte, error) { func DownloadAll(url string) ([]byte, error) {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
return nil, errors.New("Download: " + err.Error()) return nil, errors.New("Download: " + err.Error())
@ -324,3 +325,18 @@ func Download(url string) ([]byte, error) {
} }
return data, err 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
}