/
get.go
42 lines (40 loc) · 819 Bytes
/
get.go
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
package mailrucloud
import (
"fmt"
"io"
// "io/ioutil"
"net/http"
"os"
)
// Get downloads file from the cloud.
// src is the full file path in the cloud, dst is the local path to store.
func (c *MailRuCloud) Get(src, dst string, ch chan<- int) (err error) {
if err = c.GetShardInfo(); err != nil {
return
}
r, err := c.Client.Get(c.Shard.Get + src)
if err != nil {
Logger.Println(err)
return
}
defer r.Body.Close()
var f *os.File
if r.StatusCode != http.StatusOK {
err = fmt.Errorf("Get failed. Status: %d", r.StatusCode)
Logger.Println(err)
return
} else {
f, err = os.Create(dst)
if err != nil {
Logger.Println(err)
return
}
defer f.Close()
}
_, err = io.Copy(f, NewIoProgress(r.Body, int(r.ContentLength), ch))
if err != nil {
Logger.Println(err)
return
}
return
}