Skip to content

Commit 7aade22

Browse files
authored
Merge #17
v0.2
2 parents fd202da + 43c714a commit 7aade22

13 files changed

+1132
-872
lines changed

.github/workflows/test.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
on: [push, pull_request]
2+
name: Test
3+
jobs:
4+
test:
5+
strategy:
6+
matrix:
7+
go-version: [1.14.x, 1.15.x]
8+
os: [ubuntu-latest, macos-latest, windows-latest]
9+
runs-on: ${{ matrix.os }}
10+
steps:
11+
- name: Install Go
12+
uses: actions/setup-go@v2
13+
with:
14+
go-version: ${{ matrix.go-version }}
15+
- name: Checkout code
16+
uses: actions/checkout@v2
17+
- name: Test
18+
run: go test -race ./...
19+

README.md

+48-47
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,43 @@
11
<div align="center">
22
<h1>Got.</h1>
3-
<h4 align="center">
4-
Simple and fast concurrent downloader.
3+
<h4 align="center">
4+
Simple and fast concurrent downloader.
55
</h4>
66
</div>
77

88
<p align="center">
9-
<a href="#installation">Installation</a> ❘
10-
<a href="#command-line-tool-usage">CLI Usage</a> ❘
11-
<a href="#module-usage">Module Usage</a> ❘
12-
<a href="#license">License</a>
9+
<a href="#installation">Installation</a> ❘
10+
<a href="#command-line-tool-usage">CLI Usage</a> ❘
11+
<a href="#module-usage">Module Usage</a> ❘
12+
<a href="#license">License</a>
1313
</p>
1414

1515
## Comparison
1616

17-
Comparison in my machine:
17+
Comparison in cloud server:
1818

1919
```bash
20-
// cURL
21-
$ time curl http://speedtest.ftp.otenet.gr/files/test10Mb.db --output test
22-
23-
real 0m38.225s
24-
user 0m0.044s
25-
sys 0m0.199s
26-
2720

28-
// Got
29-
$ time got --out test http://speedtest.ftp.otenet.gr/files/test10Mb.db
30-
31-
real 0m7.136s
32-
user 0m0.793s
33-
sys 0m0.507s
34-
```
35-
---
36-
Comparison in cloud server:
21+
[root@centos-nyc-12 ~]# time got -o /tmp/test -c 20 http://www.ovh.net/files/1Gio.dat
22+
URL: http://www.ovh.net/files/1Gio.dat done!
3723

38-
```bash
39-
// Got
40-
$ time got --out /tmp/test http://www.ovh.net/files/1Gio.dat
41-
Total Size: 1.1 GB | Chunk Size: 54 MB | Concurrency: 10 | Progress: 1.1 GB | Done!
24+
real 0m8.832s
25+
user 0m0.203s
26+
sys 0m3.176s
4227

43-
real 0m10.273s
44-
user 0m0.205s
45-
sys 0m3.296s
4628

47-
// cURL
48-
$ time curl http://www.ovh.net/files/1Gio.dat --output /tmp/test1
29+
[root@centos-nyc-12 ~]# time curl http://www.ovh.net/files/1Gio.dat --output /tmp/test1
4930
% Total % Received % Xferd Average Speed Time Time Time Current
50-
Dload Upload Total Spent Left Speed
51-
100 1024M 100 1024M 0 0 30.8M 0 0:00:33 0:00:33 --:--:-- 36.4M
31+
Dload Upload Total Spent Left Speed
32+
100 1024M 100 1024M 0 0 35.6M 0 0:00:28 0:00:28 --:--:-- 34.4M
33+
34+
real 0m28.781s
35+
user 0m0.379s
36+
sys 0m1.970s
5237

53-
real 0m33.318s
54-
user 0m0.420s
55-
sys 0m2.056s
5638
```
5739

40+
5841
## Installation
5942

6043
#### Download and install the latest [release](https://github.com/melbahja/got/releases):
@@ -85,14 +68,29 @@ go get github.com/melbahja/got/cmd/got
8568
got https://example.com/file.mp4
8669
```
8770

88-
#### Or you can specify destination path:
71+
#### You can specify destination path:
72+
```bash
73+
got -o /path/to/save https://example.com/file.mp4
74+
```
75+
76+
#### You can download multiple URLs and save them to directory:
77+
```bash
78+
got --dir /path/to/dir https://example.com/file.mp4 https://example.com/file2.mp4
79+
```
80+
81+
#### You can download multiple URLs from a file:
8982
```bash
90-
got --out /path/to/save https://example.com/file.mp4
83+
got --dir /path/to/dir -f urls.txt
9184
```
9285

93-
#### To see all available flags type:
86+
### You can pipe multiple URLs:
9487
```bash
95-
got --help
88+
cat urls.txt | got --dir /path/to/dir
89+
```
90+
91+
#### Docs for available flags:
92+
```bash
93+
got help
9694
```
9795

9896

@@ -107,20 +105,23 @@ import "github.com/melbahja/got"
107105

108106
func main() {
109107

110-
dl, err := got.New("https://example.com/file.mp4", "/path/to/save")
108+
g := got.New()
111109

112-
if err != nil {
113-
// handle the error!
114-
}
110+
err := g.Download("http://localhost/file.ext", "/path/to/save")
115111

116-
// Start the download
117-
err = dl.Start()
112+
if err != nil {
113+
// ..
114+
}
118115
}
119116

120117
```
121118
122119
For more see [GoDocs](https://pkg.go.dev/github.com/melbahja/got).
123120
121+
## How It Works?
122+
123+
Got takes advantage of the HTTP range requests support in servers [RFC 7233](https://tools.ietf.org/html/rfc7233), if the requested URL server supports partial content Got split the file into chunks, then starts downloading and merging them into the destinaton file concurrently.
124+
124125
125126
## License
126127

chunk.go

+9-37
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,28 @@
11
package got
22

33
import (
4-
"fmt"
5-
"io"
6-
"net/http"
7-
"os"
4+
"sync"
85
)
96

10-
// Chunk is a part of file.
7+
// Chunk is a partial content range.
118
type Chunk struct {
129

13-
// Progress to report written bytes.
14-
*Progress
15-
1610
// Chunk start pos.
1711
Start uint64
1812

19-
// Chunk end.
13+
// Chunk end pos.
2014
End uint64
2115

2216
// Path name where this chunk downloaded.
2317
Path string
2418

19+
// Done to check is this chunk downloaded.
2520
Done chan struct{}
2621
}
2722

28-
// Download a chunk, and report to Progress, it returns error if any!
29-
func (c *Chunk) Download(URL string, client *http.Client, dest *os.File) (err error) {
30-
31-
req, err := NewRequest("GET", URL)
32-
33-
if err != nil {
34-
return err
35-
}
36-
37-
contentRange := fmt.Sprintf("bytes=%d-%d", c.Start, c.End)
38-
39-
if c.End == 0 {
40-
contentRange = fmt.Sprintf("bytes=%d-", c.Start)
41-
}
42-
43-
req.Header.Set("Range", contentRange)
44-
45-
res, err := client.Do(req)
46-
47-
if err != nil {
48-
return err
49-
}
50-
51-
defer res.Body.Close()
52-
53-
_, err = io.Copy(dest, io.TeeReader(res.Body, c.Progress))
54-
55-
return err
23+
// ChunkPool helps in multi *Download files.
24+
var ChunkPool = &sync.Pool{
25+
New: func() interface{} {
26+
return new(Chunk)
27+
},
5628
}

0 commit comments

Comments
 (0)