Skip to content

Commit b9a62a0

Browse files
committed
[rget] first commit (base code)
1 parent 73c537d commit b9a62a0

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

kadai3/imura81gt/rget/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
rget
2+
=========================================================
3+
4+
Command
5+
-----------------------------------------
6+
7+
```
8+
go run cmd/rget/main.go https://upload.wikimedia.org/wikipedia/commons/1/16/Notocactus_minimus.jpg
9+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/gopherdojo/dojo7/kadai3/imura81gt/rget"
9+
)
10+
11+
func main() {
12+
option := rget.Option{
13+
Concurrency: *flag.Int("c", 10, "concurrency"),
14+
}
15+
flag.Parse()
16+
urls := flag.Args()
17+
if len(urls) != 1 {
18+
fmt.Fprintf(os.Stderr, "%s <url>\n", os.Args[0])
19+
fmt.Fprintln(os.Stderr, "option:")
20+
flag.PrintDefaults()
21+
os.Exit(1)
22+
}
23+
24+
option.Url = urls[0]
25+
rget.Run(option)
26+
}

kadai3/imura81gt/rget/rget.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package rget
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
)
8+
9+
type Option struct {
10+
Concurrency int
11+
Url string
12+
}
13+
14+
type Unit struct {
15+
RangeStart int64
16+
RangeEnd int64
17+
}
18+
19+
type Units []Unit
20+
21+
func Run(option Option) {
22+
fmt.Printf("%+v\n", option)
23+
cl, err := contentLength(option.Url)
24+
if err != nil {
25+
fmt.Fprintln(os.Stderr, err)
26+
os.Exit(1)
27+
}
28+
29+
units := divide(cl, option.Concurrency)
30+
31+
//TODO: check errors
32+
download(units)
33+
34+
}
35+
36+
func contentLength(url string) (int64, error) {
37+
resp, err := http.Head(url)
38+
if err != nil {
39+
fmt.Fprintln(os.Stderr, err)
40+
return 0, err
41+
}
42+
43+
if resp.Header.Get("Accept-Ranges") == "" {
44+
err := fmt.Errorf("This URL cannot support Ranges Requests")
45+
// fmt.Fprintln(os.Stderr, err)
46+
return resp.ContentLength, err
47+
}
48+
if resp.Header["Accept-Ranges"][0] == "none" {
49+
err := fmt.Errorf("This URL cannot support Ranges Requests")
50+
// fmt.Fprintln(os.Stderr, err)
51+
return resp.ContentLength, err
52+
}
53+
if resp.ContentLength == 0 {
54+
err := fmt.Errorf("This URL size is %i", resp.Header["Content-Length"][0])
55+
// fmt.Fprintln(os.Stderr, err)
56+
return resp.ContentLength, err
57+
}
58+
59+
return resp.ContentLength, nil
60+
}
61+
62+
func divide(contentLength int64, concurrency int) Units {
63+
var units []Unit
64+
65+
sbyte := contentLength / int64(concurrency)
66+
for i := 0; i < concurrency; i++ {
67+
units = append(units, Unit{
68+
RangeStart: int64(i) * sbyte,
69+
RangeEnd: int64((i+1))*sbyte - 1,
70+
})
71+
}
72+
73+
return units
74+
}
75+
76+
func download(units Units) {
77+
fmt.Println(units)
78+
}

0 commit comments

Comments
 (0)