-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_downloader.go
376 lines (304 loc) · 8.01 KB
/
fast_downloader.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package fastdownloader
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strconv"
"sync"
"time"
)
// Options for creating new the new instance downloader.
type Options struct {
// The number of bytes to download in each request.
// If not set, the default is 5MB or 1024*1024*5 Bytes.
ChunkSizeInBytes int
// The number of concurrent requests to make.
// If not set, the default is 5.
Concurrency int
// The number of retries to make for each request.
// If not set, the default is 3.
Retries int
// Url to download from.
Url string
// Request timeout in seconds.
// If not set, the default is 3 minutes.
RequestTimeout time.Duration
// Output file name.
OutputFileName string
// Output file directory.
// default output directory is the current user's home/downloads directory.
OutputFileDirectory string
}
type ConcurrentDownloader interface {
// Start the download process.
StartDownload() error
// Get the output file path.
OutputFilePath() string
// Get the total download time.
DownloadTime() time.Duration
// IsDownloadComplete() bool
}
type downloader struct {
firstChunkID int
lastChunkID int
client *http.Client
options Options
chunkRanges []chunkRange
wg *sync.WaitGroup
inputChan chan chunkRange
outputChan chan dataChunk
stopChan chan struct{}
outputFilePath string
downloadTime time.Duration
}
type chunkRange struct {
chunkId int
header string
}
type dataChunk struct {
chunkId int
data []byte
downloadTime int64 //ms
retries int
}
type info struct {
Offset int64
Length int
}
func (d *downloader) worker(inputChan <-chan chunkRange, outputChan chan<- dataChunk, stopChan <-chan struct{}, URL string) {
defer d.wg.Done()
for {
select {
case <-stopChan:
fmt.Println("Stopping worker")
return
case in := <-inputChan:
fmt.Println("Downloading chunk", in.chunkId)
for i := 0; i < d.options.Retries; i++ {
data, err := d.downloadChunk(in)
if err != nil {
fmt.Println("Error downloading chunk", in.chunkId, "retrying...")
continue
}
data.retries = i
outputChan <- data
fmt.Println("Downloading chunk complete ", in.chunkId)
break
}
}
}
}
func newOffSet(f *os.File) int64 {
fs, _ := f.Stat()
s := fs.Size()
fmt.Println("File size", s)
return s
}
func (d *downloader) merger() error {
defer d.wg.Done()
f, err := os.CreateTemp(d.options.OutputFileDirectory, "temp.tmp")
if err != nil {
log.Println(err)
}
defer os.Remove(f.Name())
m := make(map[int]info)
downloadedChunksMapping := make(map[int]bool)
for {
select {
case data := <-d.outputChan:
if _, ok := downloadedChunksMapping[data.chunkId]; !ok {
downloadedChunksMapping[data.chunkId] = true
fmt.Printf("Downloaded chunk %d in %d ms with %d retries", data.chunkId, data.downloadTime, data.retries)
}
offset := newOffSet(f)
m[data.chunkId] = info{Offset: offset, Length: len(data.data)}
_, err := f.Write(data.data)
if err != nil {
println(err.Error())
}
if len(downloadedChunksMapping) == len(d.chunkRanges) {
of, e := os.OpenFile(filepath.Join(d.options.OutputFileDirectory, d.options.OutputFileName), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if e != nil {
println(e.Error())
}
defer of.Close()
f.Close()
sortOutputFiles(m, d.firstChunkID, d.lastChunkID, f.Name(), of)
d.outputFilePath = of.Name()
for i := 0; i < d.options.Concurrency; i++ {
d.stopChan <- struct{}{}
}
return nil
}
}
}
// return nil
}
func sortOutputFiles(m map[int]info, firstID, lastID int, inputTmpFileName string, output *os.File) {
input, e := os.Open(inputTmpFileName)
if e != nil {
fmt.Println("Error opening input file", e)
return
}
for i := firstID; i <= lastID; i++ {
info, ok := m[i]
if !ok {
fmt.Println("Missing chunk", i)
continue
}
data := make([]byte, info.Length)
readB, e := input.ReadAt(data, info.Offset)
if e != nil {
fmt.Println("Error reading chunk from", i, e)
continue
}
writeB, _ := output.Write(data)
fmt.Println("Writing chunk", i, "expected length", info.Length, "actual read", readB, "actual write", writeB)
}
}
func (d *downloader) downloadChunk(info chunkRange) (dataChunk, error) {
var data dataChunk
r, err := http.NewRequest("GET", d.options.Url, nil)
if err != nil {
return data, err
}
r.Header.Add("Range", info.header)
startTime := time.Now()
res, err := d.client.Do(r)
data.downloadTime = time.Since(startTime).Milliseconds()
if err != nil {
return data, err
}
data.data, err = ioutil.ReadAll(res.Body)
defer res.Body.Close()
data.chunkId = info.chunkId
if err != nil {
return data, err
}
return data, nil
}
func NewConcurrentDownloader(options Options) (ConcurrentDownloader, error) {
if err := validateOpts(&options); err != nil {
return nil, err
}
return &downloader{options: options, client: &http.Client{
Timeout: options.RequestTimeout,
}}, nil
}
func validateOpts(options *Options) error {
if options.ChunkSizeInBytes == 0 {
options.ChunkSizeInBytes = 1024 * 1024 * 1
}
if options.Concurrency == 0 {
options.Concurrency = 5
}
if options.Retries == 0 {
options.Retries = 3
}
if options.Url == "" {
return errors.New("Url is required")
}
if options.OutputFileDirectory == "" {
usr, err := user.Current()
if err != nil {
return err
}
options.OutputFileDirectory = filepath.Join(usr.HomeDir, "Downloads")
}
if options.OutputFileName == "" {
return errors.New("Output file name is required")
}
if options.RequestTimeout == 0 {
println("Setting request timeout to 3 minutes")
options.RequestTimeout = 3 * time.Minute
}
return nil
}
func (d *downloader) OutputFilePath() string {
return d.outputFilePath
}
func (d *downloader) DownloadTime() time.Duration {
return d.downloadTime
}
func (d *downloader) StartDownload() error {
startTime := time.Now()
err := d.calculateChunkRanges()
if err != nil {
return err
}
d.inputChan = make(chan chunkRange, len(d.chunkRanges))
d.outputChan = make(chan dataChunk, len(d.chunkRanges))
d.stopChan = make(chan struct{})
d.wg = &sync.WaitGroup{}
d.wg.Add(d.options.Concurrency + 1)
for i := 0; i < d.options.Concurrency; i++ {
go d.worker(d.inputChan, d.outputChan, d.stopChan, d.options.Url)
}
go d.merger()
for _, chunk := range d.chunkRanges {
d.inputChan <- chunk
}
d.wg.Wait()
close(d.inputChan)
close(d.outputChan)
close(d.stopChan)
d.downloadTime = time.Since(startTime)
return nil
}
func (d *downloader) requestHEAD() (int, error) {
fmt.Println("Requesting HEAD")
r, err := http.NewRequest("HEAD", d.options.Url, nil)
if err != nil {
return 0, err
}
res, err := d.client.Do(r)
if err != nil {
return 0, err
}
fmt.Printf("Response: %+v", res)
if res.StatusCode < 200 || res.StatusCode > 206 {
return 0, errors.New(fmt.Sprintf("Status Code (%d): Invalid header received", res.StatusCode))
}
cl := res.Header.Get("content-length")
length, err := strconv.Atoi(cl)
if err != nil {
return length, err
}
name := res.Header.Get("Content-Disposition")
println("Content-Disposition", name)
//it will be 'none' if ranges not supported
if res.Header.Get("Accept-Ranges") == "none" {
fmt.Println("Ranges not supported by server")
return length, errors.New("Ranges not supported by server")
}
fmt.Println("File length: ", length, " bytes")
return length, nil
}
func (d *downloader) calculateChunkRanges() error {
length, err := d.requestHEAD()
if err != nil {
return err
}
chunk_id := 1
for lowerIndex := 0; lowerIndex < length; {
uperIndex := lowerIndex + d.options.ChunkSizeInBytes - 1
if uperIndex >= length {
uperIndex = length - 1
}
d.chunkRanges = append(d.chunkRanges, chunkRange{
chunkId: chunk_id,
header: fmt.Sprintf("bytes=%d-%d", lowerIndex, uperIndex),
})
fmt.Println(fmt.Sprintf("Chunk %d: [%d-%d]", chunk_id, lowerIndex, uperIndex))
chunk_id++
lowerIndex = lowerIndex + d.options.ChunkSizeInBytes
}
d.firstChunkID = 1
d.lastChunkID = chunk_id - 1
return nil
}