-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.go
More file actions
47 lines (36 loc) · 896 Bytes
/
Copy pathtransfer.go
File metadata and controls
47 lines (36 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package octavius
import (
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"time"
)
func Download(addr, dstFile string, w io.Writer) error {
url := fmt.Sprintf("http://%v/%v", addr, dstFile)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
start := time.Now()
written, err := io.Copy(w, resp.Body)
elapsed := time.Since(start)
log.Infof("Response Code: ", resp.Status)
log.Infof("Writing file took %v", elapsed)
size := written
speed := int(float64(written) / elapsed.Seconds())
log.Infof("Size: %v\tTime to download: %v\tSpeed: %v/s", size, elapsed, ByteCountDecimal(speed))
if err != nil {
return err
}
return err
}
func DownloadReader(addr, dstFile string) (io.Reader, error) {
url := fmt.Sprintf("http://%v/%v", addr, dstFile)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
return resp.Body, nil
}