@@ -19,14 +19,21 @@ import (
19
19
type Downloader struct {
20
20
URL string
21
21
Done chan bool
22
- NoResume bool
23
22
resp * http.Response
24
23
out io.Writer
25
24
completed int64
26
25
size int64
27
26
err error
28
27
}
29
28
29
+ // DownloadOptions are optional flags that can be passed to Download function
30
+ type DownloadOptions int
31
+
32
+ const (
33
+ // NoResume will not try to resume a partial download
34
+ NoResume DownloadOptions = iota
35
+ )
36
+
30
37
// Close the download
31
38
func (d * Downloader ) Close () error {
32
39
return d .resp .Body .Close ()
@@ -98,16 +105,24 @@ func (d *Downloader) Completed() int64 {
98
105
// Download returns an asynchronous downloader that will donwload the specified url
99
106
// in the specified file. A download resume is tried if a file shorter than the requested
100
107
// url is already present.
101
- func Download (file string , url string ) (* Downloader , error ) {
108
+ func Download (file string , url string , options ... DownloadOptions ) (* Downloader , error ) {
109
+ noResume := false
110
+ for _ , opt := range options {
111
+ if opt == NoResume {
112
+ noResume = true
113
+ }
114
+ }
102
115
req , err := http .NewRequest ("GET" , url , nil )
103
116
if err != nil {
104
117
return nil , fmt .Errorf ("setting up HTTP request: %s" , err )
105
118
}
106
119
107
120
var completed int64
108
- if info , err := os .Stat (file ); err == nil {
109
- completed = info .Size ()
110
- req .Header .Set ("Range" , fmt .Sprintf ("bytes=%d-" , completed ))
121
+ if ! noResume {
122
+ if info , err := os .Stat (file ); err == nil {
123
+ completed = info .Size ()
124
+ req .Header .Set ("Range" , fmt .Sprintf ("bytes=%d-" , completed ))
125
+ }
111
126
}
112
127
113
128
client := & http.Client {}
0 commit comments