-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (166 loc) · 5.19 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
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"sync"
"io"
)
type DownloadChunk struct {
Start uint64
End uint64
};
/*
Attempts to get the file size of the download.
Arguments:
- url (string): The url of the resource to download.
Returns:
- int: The size of the file to download.
- error: The error if any occured.
Example:
size, err := GetFileSize("https://google.com");
if err != nil {
return err;
}
*/
func GetFileSize(url string) (uint64, error) {
resp, err := http.Head(url);
if err != nil {
return 0, fmt.Errorf("Error: Failed to make request to %s. %w", url, err);
}
defer resp.Body.Close();
contentSize := resp.Header["Content-Length"];
if contentSize == nil {
return 0, fmt.Errorf("Error: Failed to determine download size.");
}
size, err := strconv.ParseInt(contentSize[0], 10, 64);
if err != nil {
return 0, fmt.Errorf("Error: Failed to determine download size. %w", err);
}
return uint64(size), nil;
}
/*
Returns a list of chunks that need to be downloaded.
Arguments:
- size (uint64): The total size of the download.
- threads (uint64): The number of threads to run.
Returns:
[]DownloadChunk: The list of chunks that need to be downloaded.
Example:
chunks := GetDownloadChunks(1024, 4);
*/
func GetDownloadChunks(size uint64, threads uint64) []DownloadChunk {
chunkSize := size / threads;
results := make([]DownloadChunk, threads);
var i uint64;
for i = 0; i < threads; i++ {
if i == threads - 1 {
results[i] = DownloadChunk{
Start: i * chunkSize,
End: (i * chunkSize) + (size - (i * chunkSize)),
};
} else {
results[i] = DownloadChunk{
Start: i * chunkSize,
End: (i * chunkSize) + chunkSize,
};
}
}
return results;
}
/*
Downloads the requested chunks from the url.
Arguments:
- url (string): The url of what to download.
- chunks ([]DownloadChunks): The chunks to download.
- output (string): The path to where to save the downloaded file.
Returns:
- error: The error if any occured.
Example:
err := DownloadChunks("https://google.com", chunks, "./google.html")
if err != nil {
return err;
}
*/
func DownloadChunks(url string, chunks []DownloadChunk, output string) error {
f, err := os.Create(output);
if err != nil {
return fmt.Errorf("Error: Failed to create file %s. %v", output, err);
}
defer f.Close();
var mutex sync.Mutex;
errChan := make(chan error);
for i := 0; i < len(chunks); i++ {
go func(start uint64, end uint64) {
fmt.Printf("Log: Downloading chuck %d-%d.\n", start, end);
req, err := http.NewRequest("GET", url, nil);
if err != nil {
errChan<-fmt.Errorf("Error: Failed to create request for %s. %v", url, err);
return;
}
chunkHeader := fmt.Sprintf("bytes=%d-%d", start, end);
req.Header.Set("Range", chunkHeader);
resp, err := http.DefaultClient.Do(req);
if err != nil {
errChan<-fmt.Errorf("Error: Failed to make request to %s. %v", url, err);
return;
}
defer resp.Body.Close();
if resp.StatusCode != http.StatusPartialContent {
errChan<-fmt.Errorf("Error: Unsuccessful status code from %s (%d).", url, resp.StatusCode);
return;
}
mutex.Lock();
fmt.Printf("Log: Writing chuck %d-%d.\n", start, end);
_, err = f.Seek(int64(start), 0);
if err != nil {
errChan<-fmt.Errorf("Error: Unsuccessful writing to file %s. %v", output, err);
mutex.Unlock();
return;
}
_, err = io.Copy(f, resp.Body);
if err != nil {
errChan<-fmt.Errorf("Error: Unsuccessful writing to file %s. %v", output, err);
mutex.Unlock();
return;
}
mutex.Unlock();
errChan<-nil;
}(chunks[i].Start, chunks[i].End)
}
for i := 0; i < len(chunks); i++ {
err := <-errChan;
if err != nil {
return err;
}
}
return nil;
}
func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: gofast <url> <threads> <output file path>\n");
return;
}
downloadSize, err := GetFileSize(os.Args[1]);
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to get download size. %v\n", err);
return;
}
fmt.Printf("Log: Download size: %d bytes.\n", downloadSize);
t, err := strconv.ParseInt(os.Args[2], 10, 64);
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Invalid thread number. %v\n", err);
return;
}
if t <= 0 {
fmt.Fprintf(os.Stderr, "Error: Invalid thread number.\n");
return;
}
threads := uint64(t);
chunks := GetDownloadChunks(downloadSize, threads);
err = DownloadChunks(os.Args[1], chunks, os.Args[3]);
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err);
}
}