-
Notifications
You must be signed in to change notification settings - Fork 2
/
get.go
78 lines (75 loc) · 1.62 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package mailrucloud
import (
"fmt"
"io"
// "io/ioutil"
"net/http"
"os"
)
// Get downloads file parts or the full file from the cloud.
// src is the full file path in the cloud
// dst is the local path to store
// ch is progress channel
func (c *MailRuCloud) Get(src, dst string, ch chan<- int) (err error) {
mpart := false
_, err = c.Stat(src)
if err != nil {
_, err = c.Stat(fmt.Sprintf("%s.Multifile-Part%02d", src, 0))
mpart = true
}
if err != nil {
Logger.Println(err)
return
}
f, err := os.Create(dst)
if err != nil {
Logger.Println(err)
return
}
defer f.Close()
if mpart {
for i := 0; ; i++ {
if _, err = c.Stat(fmt.Sprintf("%s.Multifile-Part%02d", src, i)); err == nil {
err = c.GetFilePart(f, fmt.Sprintf("%s.Multifile-Part%02d", src, i), ch)
if err != nil {
Logger.Println(err)
return
}
} else {
if i > 0 {
err = nil
}
break
}
}
} else {
err = c.GetFilePart(f, src, ch)
}
return
}
// GetFilePart downloads file parts or the full file from the cloud.
// f is file descriptor
// src is the full file path in the cloud
// ch is progress channel
func (c *MailRuCloud) GetFilePart(f *os.File, src 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()
if r.StatusCode != http.StatusOK {
err = fmt.Errorf("Get failed. Status: %d", r.StatusCode)
Logger.Println(err)
return
}
_, err = io.Copy(f, NewIoProgress(r.Body, int(r.ContentLength), ch))
if err != nil {
Logger.Println(err)
return
}
return
}