-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_downloader.go
325 lines (269 loc) · 6.98 KB
/
file_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
package idg
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"sync"
"gopkg.in/cheggaaa/pb.v1"
)
type PartReport struct {
FilePart *FilePart
DownloadedPath string
Err error
}
// FileDownloader a worker for downloading a file
type FileDownloader struct {
Client *http.Client
File *File
Bar *pb.ProgressBar
MaxConnections int
Dir string
dispatcher chan *FilePart
partReport chan *PartReport
quitSignal chan bool
progressMonitor chan int
}
func NewFileDownloader(file *File, dir string, maxConnections int) *FileDownloader {
return &FileDownloader{
Client: &http.Client{},
File: file,
Dir: dir,
MaxConnections: maxConnections,
Bar: pb.New(0),
}
}
// Download download a file
func (fileDownloader *FileDownloader) Download() (string, error) {
if err := fileDownloader.parseFile(); err != nil {
return "", err
}
if !fileDownloader.File.AcceptRange {
fileDownloader.MaxConnections = 1
}
fileDownloader.startDispatcher()
doneSignal := fileDownloader.startDownloaders()
downloadedParts := []*FilePart{}
LOOP:
for {
select {
case <-doneSignal:
close(fileDownloader.quitSignal)
break LOOP
case partReport := <-fileDownloader.partReport:
if partReport.Err != nil {
close(fileDownloader.quitSignal)
return "", partReport.Err
}
downloadedParts = append(downloadedParts, partReport.FilePart)
}
}
filePath, err := fileDownloader.join(downloadedParts)
if err != nil {
return "", err
}
fileDownloader.File.DiskPath = filePath
return filePath, nil
}
func (fileDownloader *FileDownloader) parseFile() error {
if _, err := url.Parse(fileDownloader.File.URL); err != nil {
return err
}
req, _ := http.NewRequest(http.MethodGet, fileDownloader.File.URL, nil)
for key, value := range fileDownloader.File.Header {
req.Header.Add(key, value)
}
if len(fileDownloader.File.Cookies) > 0 {
fileDownloader.Client.Jar.SetCookies(req.URL, fileDownloader.File.Cookies)
}
res, err := fileDownloader.Client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return errors.New(res.Status)
}
fileDownloader.File.Name = getFileName(res)
fileDownloader.File.AcceptRange = isAcceptByteRange(res)
fileDownloader.File.Size = res.ContentLength
// Update URL. It might be changed if the request has been redirected
fileDownloader.File.URL = res.Request.URL.String()
if fileDownloader.File.Size == 0 {
return errors.New("Failed to get the file's size")
}
return nil
}
func (fileDownloader *FileDownloader) downloadPart(part *FilePart) *PartReport {
partName := fmt.Sprintf("%s_%d", fileDownloader.File.Name, part.PartNo)
partFilePath := filepath.Join(fileDownloader.Dir, partName)
partReport := &PartReport{FilePart: part}
req, _ := http.NewRequest(http.MethodGet, fileDownloader.File.URL, nil)
for key, value := range fileDownloader.File.Header {
req.Header.Add(key, value)
}
if fileDownloader.File.AcceptRange {
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", part.Begin, part.End))
}
res, err := fileDownloader.Client.Do(req)
if err != nil {
partReport.Err = err
return partReport
}
if res.StatusCode != 200 {
if err != nil {
partReport.Err = err
return partReport
}
}
diskfile, err := os.Create(partFilePath)
if err != nil {
partReport.Err = err
return partReport
}
if err := fileDownloader.copyBuffer(diskfile, res.Body); err != nil {
partReport.Err = err
return partReport
}
partReport.FilePart.DiskPath = partFilePath
return partReport
}
func (fileDownloader *FileDownloader) startDownloaders() chan bool {
wg := &sync.WaitGroup{}
wg.Add(fileDownloader.MaxConnections)
for i := 0; i < fileDownloader.MaxConnections; i++ {
go func() {
defer wg.Done()
for {
select {
case part, ok := <-fileDownloader.dispatcher:
if !ok {
// all parts are done
return
}
fileDownloader.partReport <- fileDownloader.downloadPart(part)
case <-fileDownloader.quitSignal:
return
}
}
}()
}
return wait(wg)
}
func (fileDownloader *FileDownloader) startDispatcher() {
fileDownloader.monitor()
fileDownloader.dispatcher = make(chan *FilePart, fileDownloader.MaxConnections)
fileDownloader.quitSignal = make(chan bool)
fileDownloader.partReport = make(chan *PartReport, fileDownloader.MaxConnections)
go func() {
defer close(fileDownloader.dispatcher)
rangeBytes := fileDownloader.File.Size / int64(fileDownloader.MaxConnections)
for i := 0; i < fileDownloader.MaxConnections; i++ {
begin := rangeBytes * int64(i)
end := begin + rangeBytes
if begin > 0 {
begin = begin + 1
}
if i == fileDownloader.MaxConnections-1 {
end = fileDownloader.File.Size
}
filePart := &FilePart{
PartNo: int64(i),
Begin: begin,
End: end,
}
select {
case fileDownloader.dispatcher <- filePart:
case <-fileDownloader.quitSignal:
return
}
}
}()
}
func (fileDownloader *FileDownloader) join(parts []*FilePart) (string, error) {
filepath := filepath.Join(fileDownloader.Dir, fileDownloader.File.Name)
file, err := os.Create(filepath)
if err != nil {
return "", err
}
defer file.Close()
sort.Slice(parts, func(i, j int) bool {
return parts[i].PartNo < parts[j].PartNo
})
for _, part := range parts {
i, err := os.Open(part.DiskPath)
if err != nil {
return "", err
}
if _, err := io.Copy(file, i); err != nil {
return "", err
}
i.Close()
if err := os.RemoveAll(part.DiskPath); err != nil {
return "", err
}
}
return filepath, nil
}
func (fileDownloader *FileDownloader) monitor() {
fileDownloader.Bar.SetTotal64(fileDownloader.File.Size)
fileDownloader.Bar.SetUnits(pb.U_BYTES)
fileDownloader.Bar.Start()
fileDownloader.Bar.ShowSpeed = true
fileDownloader.Bar.ShowElapsedTime = true
fileDownloader.progressMonitor = make(chan int, 1024)
go func() {
for {
select {
case bytes := <-fileDownloader.progressMonitor:
fileDownloader.Bar.Add(bytes)
case <-fileDownloader.quitSignal:
fileDownloader.Bar.Finish()
return
}
}
}()
}
func (fileDownloader *FileDownloader) copyBuffer(dst io.Writer, src io.Reader) (err error) {
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
if wt, ok := src.(io.WriterTo); ok {
wt.WriteTo(dst)
return
}
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
if rt, ok := dst.(io.ReaderFrom); ok {
rt.ReadFrom(src)
return
}
buf := make([]byte, 32*1024)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
fileDownloader.progressMonitor <- nw
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return err
}
func DownloadSingleFile(file *File, dir string, maxConnections int) (string, error) {
return NewFileDownloader(file, dir, maxConnections).Download()
}