summaryrefslogtreecommitdiff
path: root/net.go
diff options
context:
space:
mode:
authorHéctor Orón Martínez <zumbi@debian.org>2018-01-09 19:12:07 +0100
committerHéctor Orón Martínez <zumbi@debian.org>2018-01-09 19:12:07 +0100
commit4808cb7058c548bf76476ec2f9618d784d76bdda (patch)
tree1dc1e8cc24171783fc8d9da306b1e92798960a15 /net.go
New upstream version 1.0.0+git20171222.87b0d5e
Diffstat (limited to 'net.go')
-rw-r--r--net.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/net.go b/net.go
new file mode 100644
index 0000000..989c90d
--- /dev/null
+++ b/net.go
@@ -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
+}