diff options
author | Héctor Orón Martínez <zumbi@debian.org> | 2018-01-09 19:12:07 +0100 |
---|---|---|
committer | Héctor Orón Martínez <zumbi@debian.org> | 2018-01-09 19:12:07 +0100 |
commit | 4808cb7058c548bf76476ec2f9618d784d76bdda (patch) | |
tree | 1dc1e8cc24171783fc8d9da306b1e92798960a15 /net.go |
New upstream version 1.0.0+git20171222.87b0d5e
Diffstat (limited to 'net.go')
-rw-r--r-- | net.go | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ +package debos + +import ( + "fmt" + "io" + "log" + "net/http" + "os" +) + +// Function for downloading single file object with http(s) protocol +func DownloadHttpUrl(url, filename string) error { + log.Printf("Download started: '%s' -> '%s'\n", url, filename) + + // TODO: Proxy support? + + // Check if file object already exists. + fi, err := os.Stat(filename) + if !os.IsNotExist(err) && !fi.Mode().IsRegular() { + return fmt.Errorf("Failed to download '%s': '%s' exists and it is not a regular file\n", url, filename) + } + + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Url '%s' returned status code %d (%s)\n", url, resp.StatusCode, http.StatusText(resp.StatusCode)) + } + + // Output file + output, err := os.Create(filename) + if err != nil { + return err + } + defer output.Close() + + if _, err := io.Copy(output, resp.Body); err != nil { + return err + } + + return nil +} |