-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (62 loc) · 1.59 KB
/
main.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
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"sync"
"github.com/cheggaaa/pb"
)
var wg sync.WaitGroup
const (
AcceptRanges = "Accept-Ranges"
ContentLength = "Content-Length"
Range = "Range"
partLength = 2 * 1000 * 1000
numberGoroutines = 8
)
func main() {
wg.Add(numberGoroutines)
url := os.Args[1]
fileName := GetFileName(url)
// send HEAD request
response, err := http.Head(url)
checkError(err)
if response.Header[AcceptRanges][0] != "bytes" {
fmt.Printf("%s does NOT support Multipart Download\n", url)
os.Exit(1)
}
contentLength, err := strconv.ParseInt(response.Header.Get(ContentLength), 10, 64)
checkError(err)
fmt.Printf("Total: %d\n", contentLength)
parts := CalculateNumberOfParts(contentLength, partLength)
tasks := make(chan Task, parts)
client := &http.Client{}
progressBars := make([]*pb.ProgressBar, 0)
for i := 0; i < numberGoroutines; i++ {
go Download(tasks, client, url, fileName)
}
files := make([]string, 0)
for part := 0; part < parts; part++ {
partFileName := fmt.Sprintf("%s-%d", fileName, part)
total := int64(partLength)
if part == (parts - 1) {
total = contentLength - int64((partLength * part))
}
progressBar := pb.New64(total).Prefix(partFileName)
task := Task{
Part: part,
ProgressBar: progressBar,
}
tasks <- task
progressBars = append(progressBars, progressBar)
files = append(files, partFileName)
}
progressBarPool, err := pb.StartPool(progressBars...)
checkError(err)
close(tasks)
wg.Wait()
progressBarPool.Stop()
err = Join(files, fileName)
checkError(err)
}