@@ -19,14 +19,21 @@ import (
1919type Downloader struct {
2020 URL string
2121 Done chan bool
22- NoResume bool
2322 resp * http.Response
2423 out io.Writer
2524 completed int64
2625 size int64
2726 err error
2827}
2928
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+
3037// Close the download
3138func (d * Downloader ) Close () error {
3239 return d .resp .Body .Close ()
@@ -98,16 +105,24 @@ func (d *Downloader) Completed() int64 {
98105// Download returns an asynchronous downloader that will donwload the specified url
99106// in the specified file. A download resume is tried if a file shorter than the requested
100107// 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+ }
102115 req , err := http .NewRequest ("GET" , url , nil )
103116 if err != nil {
104117 return nil , fmt .Errorf ("setting up HTTP request: %s" , err )
105118 }
106119
107120 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+ }
111126 }
112127
113128 client := & http.Client {}
0 commit comments