-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (170 loc) Β· 4.29 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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"sync"
"time"
"github.com/muesli/termenv"
"github.com/pkg/errors"
"github.com/royalbhati/foxy/metadata"
"github.com/royalbhati/foxy/progress"
)
type Download struct {
URL string
TargetPath string
TotalSections int
DirPath string
}
func main() {
p := termenv.ColorProfile()
startTime := time.Now()
var bar progress.Bar
bar.NewOption(0, 10)
var url string
fmt.Printf("%s\n", termenv.String(" ποΈ Paste the url here").Bold().Foreground(p.Color("#FDD835")))
fmt.Scanf("%s\n", &url)
if url == "" {
log.Fatal("URL can't be empty")
}
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
d := Download{
URL: url,
TargetPath: "",
TotalSections: 10,
DirPath: path,
}
if err := d.Do(p, &bar); err != nil {
fmt.Printf("\n%s\n", termenv.String("An error occured while downloading the file").Foreground(p.Color("0")).Background(p.Color("#E88388")))
// fmt.Printf("\n%s", err)
log.Fatal(err)
}
fmt.Printf("\n\n β
Download completed in %v seconds\n", time.Now().Sub(startTime).Seconds())
}
func (d Download) Do(p termenv.Profile, bar *progress.Bar) error {
fmt.Printf("\n%s\n", termenv.String(" π CHECKING URL ").Bold().Foreground(p.Color("#FDD835")))
r, err := d.getNewRequest("HEAD")
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(r)
if err != nil {
return err
}
if resp.StatusCode > 299 {
return errors.New(fmt.Sprintf("Can't process, response is %v", resp.StatusCode))
}
fmt.Printf("\n%s\n", termenv.String(" π URL OK ").Bold().Foreground(p.Color("#8BC34A")))
size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
a := metadata.GetFileName(resp)
d.TargetPath = a
if err != nil {
return err
}
var yesOrNo string
fmt.Printf("\n%s %f %s\n", termenv.String(" π SIZE IS ").Bold().Foreground(p.Color("#FFB300")), (float64(size)*float64(0.001))/1000, "MB")
fmt.Printf("\n%s\n", termenv.String(" π PROCEED DOWNLOADING (y/n)").Bold().Foreground(p.Color("#FFB300")))
fmt.Scanf("%s", &yesOrNo)
if yesOrNo == "n" {
fmt.Printf("%s\n", termenv.String("EXITING").Bold().Foreground(p.Color("#FF7043")))
os.Exit(2)
return nil
}
var sections = make([][2]int, d.TotalSections)
eachSize := size / d.TotalSections
for i := range sections {
if i == 0 {
sections[i][0] = 0
} else {
sections[i][0] = sections[i-1][1] + 1
}
if i < d.TotalSections-1 {
sections[i][1] = sections[i][0] + eachSize
} else {
sections[i][1] = size - 1
}
}
var wg sync.WaitGroup
sum2 := 0
for i, s := range sections {
wg.Add(1)
go func(i int, s [2]int, bar *progress.Bar) {
defer wg.Done()
err = d.downloadSection(i, s, &sum2, p, bar)
if err != nil {
panic(err)
}
}(i, s, bar)
}
wg.Wait()
return d.mergeFiles(sections)
}
func (d Download) downloadSection(i int, c [2]int, sum *int, p termenv.Profile, bar *progress.Bar) error {
r, err := d.getNewRequest("GET")
if err != nil {
return err
}
r.Header.Set("Range", fmt.Sprintf("bytes=%v-%v", c[0], c[1]))
resp, err := http.DefaultClient.Do(r)
if err != nil {
return err
}
if resp.StatusCode > 299 {
return errors.New(fmt.Sprintf("Can't process, response is %v", resp.StatusCode))
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(d.DirPath, fmt.Sprintf("section-%v.tmp", i)), b, os.ModePerm)
if err != nil {
return err
}
fmt.Printf("section-%v.tmp", i)
*sum = *sum + 1
bar.Play(int64(*sum))
return nil
}
// Get a new http request
func (d Download) getNewRequest(method string) (*http.Request, error) {
r, err := http.NewRequest(
method,
d.URL,
nil,
)
if err != nil {
return nil, err
}
r.Header.Set("User-Agent", "TDM")
return r, nil
}
func (d Download) mergeFiles(sections [][2]int) error {
f, err := os.OpenFile(path.Join(d.DirPath, d.TargetPath), os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}
defer f.Close()
for i := range sections {
tmpFileName := fmt.Sprintf("section-%v.tmp", i)
b, err := ioutil.ReadFile(path.Join(d.DirPath, tmpFileName))
if err != nil {
return err
}
_, err = f.Write(b)
if err != nil {
return err
}
err = os.Remove(tmpFileName)
if err != nil {
return err
}
}
return nil
}