|
1 | 1 | package rget
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "fmt"
|
| 6 | + "io" |
5 | 7 | "net/http"
|
6 | 8 | "os"
|
| 9 | + "path" |
| 10 | + |
| 11 | + "golang.org/x/sync/errgroup" |
7 | 12 | )
|
8 | 13 |
|
9 | 14 | type Option struct {
|
10 |
| - Concurrency int |
11 |
| - Url string |
| 15 | + Concurrency int |
| 16 | + URL string |
| 17 | + OutputDir string |
| 18 | + ContentLength int64 |
| 19 | + Units Units |
12 | 20 | }
|
13 | 21 |
|
14 | 22 | type Unit struct {
|
15 |
| - RangeStart int64 |
16 |
| - RangeEnd int64 |
| 23 | + RangeStart int64 |
| 24 | + RangeEnd int64 |
| 25 | + TempFileName string |
17 | 26 | }
|
18 | 27 |
|
19 | 28 | type Units []Unit
|
20 | 29 |
|
21 | 30 | func Run(option Option) {
|
22 | 31 | fmt.Printf("%+v\n", option)
|
23 |
| - cl, err := contentLength(option.Url) |
| 32 | + err := option.contentLength() |
24 | 33 | if err != nil {
|
25 |
| - fmt.Fprintln(os.Stderr, err) |
26 |
| - os.Exit(1) |
| 34 | + fmt.Errorf("%s", err) |
27 | 35 | }
|
28 | 36 |
|
29 |
| - units := divide(cl, option.Concurrency) |
| 37 | + option.divide() |
30 | 38 |
|
31 |
| - //TODO: check errors |
32 |
| - download(units) |
| 39 | + err = option.parallelDownload() |
| 40 | + if err != nil { |
| 41 | + fmt.Errorf("%s", err) |
| 42 | + } |
33 | 43 |
|
34 | 44 | }
|
35 | 45 |
|
36 |
| -func contentLength(url string) (int64, error) { |
37 |
| - resp, err := http.Head(url) |
| 46 | +func (o *Option) contentLength() error { |
| 47 | + //resp, err := http.Head(url) |
| 48 | + resp, err := http.Head(o.URL) |
38 | 49 | if err != nil {
|
39 | 50 | fmt.Fprintln(os.Stderr, err)
|
40 |
| - return 0, err |
| 51 | + //return 0, err |
| 52 | + return err |
41 | 53 | }
|
42 | 54 |
|
43 | 55 | if resp.Header.Get("Accept-Ranges") == "" {
|
44 |
| - err := fmt.Errorf("This URL cannot support Ranges Requests") |
| 56 | + err := fmt.Errorf("%s URL cannot support Ranges Requests", o.URL) |
45 | 57 | // fmt.Fprintln(os.Stderr, err)
|
46 |
| - return resp.ContentLength, err |
| 58 | + //return resp.ContentLength, err |
| 59 | + return err |
47 | 60 | }
|
48 | 61 | if resp.Header["Accept-Ranges"][0] == "none" {
|
49 |
| - err := fmt.Errorf("This URL cannot support Ranges Requests") |
| 62 | + err := fmt.Errorf("%s cannot support Ranges Requests", o.URL) |
50 | 63 | // fmt.Fprintln(os.Stderr, err)
|
51 |
| - return resp.ContentLength, err |
| 64 | + //return resp.ContentLength, err |
| 65 | + return err |
52 | 66 | }
|
53 | 67 | if resp.ContentLength == 0 {
|
54 |
| - err := fmt.Errorf("This URL size is %i", resp.Header["Content-Length"][0]) |
| 68 | + err := fmt.Errorf("%s size is %s", o.URL, resp.Header["Content-Length"][0]) |
55 | 69 | // fmt.Fprintln(os.Stderr, err)
|
56 |
| - return resp.ContentLength, err |
| 70 | + //return resp.ContentLength, err |
| 71 | + return err |
57 | 72 | }
|
58 | 73 |
|
59 |
| - return resp.ContentLength, nil |
| 74 | + o.ContentLength = resp.ContentLength |
| 75 | + //return resp.ContentLength, nil |
| 76 | + return err |
60 | 77 | }
|
61 | 78 |
|
62 |
| -func divide(contentLength int64, concurrency int) Units { |
| 79 | +//func divide(contentLength int64, concurrency int) Units { |
| 80 | +func (o *Option) divide() { |
63 | 81 | var units []Unit
|
64 | 82 |
|
65 |
| - sbyte := contentLength / int64(concurrency) |
66 |
| - for i := 0; i < concurrency; i++ { |
| 83 | + //sbyte := contentLength / int64(concurrency) |
| 84 | + sbyte := o.ContentLength / int64(o.Concurrency) |
| 85 | + |
| 86 | + // for i := 0; i < concurrency; i++ { |
| 87 | + for i := 0; i < o.Concurrency; i++ { |
67 | 88 | units = append(units, Unit{
|
68 |
| - RangeStart: int64(i) * sbyte, |
69 |
| - RangeEnd: int64((i+1))*sbyte - 1, |
| 89 | + RangeStart: int64(i) * sbyte, |
| 90 | + RangeEnd: int64((i+1))*sbyte - 1, |
| 91 | + TempFileName: fmt.Sprintf("%d_%s", i, path.Base(o.URL)), |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + o.Units = units |
| 96 | + //return units |
| 97 | +} |
| 98 | + |
| 99 | +// func download(units Units) { |
| 100 | +// filepath.Split() |
| 101 | +// fmt.Println(units) |
| 102 | +// } |
| 103 | + |
| 104 | +func (o *Option) parallelDownload() error { |
| 105 | + fmt.Println("parallelDownload", o.Units) |
| 106 | + |
| 107 | + eg, ctx := errgroup.WithContext(context.Background()) |
| 108 | + for i := range o.Units { |
| 109 | + // https://godoc.org/golang.org/x/sync/errgroup#example-Group--Parallel |
| 110 | + // https://golang.org/doc/faq#closures_and_goroutines |
| 111 | + i := i |
| 112 | + eg.Go(func() error { |
| 113 | + return o.downloadWithContext(ctx, i) |
70 | 114 | })
|
71 | 115 | }
|
72 | 116 |
|
73 |
| - return units |
| 117 | + if err := eg.Wait(); err != nil { |
| 118 | + o.clearCache() |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (o *Option) downloadWithContext(ctx context.Context, i int) error { |
| 126 | + ctx, cancel := context.WithCancel(ctx) |
| 127 | + defer cancel() |
| 128 | + |
| 129 | + fmt.Printf("Downloading: %v %+v\n", i, o.Units[i]) |
| 130 | + |
| 131 | + //v1.13 |
| 132 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, o.URL, nil) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("Error: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + // add range header |
| 138 | + fmt.Printf(fmt.Sprintf("bytes=%d-%d", o.Units[i].RangeStart, o.Units[i].RangeEnd)) |
| 139 | + req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", o.Units[i].RangeStart, o.Units[i].RangeEnd)) |
| 140 | + |
| 141 | + client := &http.Client{} |
| 142 | + resp, err := client.Do(req) |
| 143 | + defer resp.Body.Close() |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("Error: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + select { |
| 149 | + case <-ctx.Done(): |
| 150 | + fmt.Printf("Done: %v %+v\n", i, o.Units[i]) |
| 151 | + return nil |
| 152 | + default: |
| 153 | + fmt.Println("default:", i, o.Units[i]) |
| 154 | + //return fmt.Errorf("Error: %v %+v", i, o.Units[i]) |
| 155 | + } |
| 156 | + |
| 157 | + w, err := os.Create(o.Units[i].TempFileName) |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("Error: %v", err) |
| 160 | + } |
| 161 | + defer func() error { |
| 162 | + if err := w.Close(); err != nil { |
| 163 | + return fmt.Errorf("Error: %v", err) |
| 164 | + } |
| 165 | + return nil |
| 166 | + }() |
| 167 | + |
| 168 | + _, err = io.Copy(w, resp.Body) |
| 169 | + if err != nil { |
| 170 | + return fmt.Errorf("Error: %v", err) |
| 171 | + } |
| 172 | + |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func (o *Option) conbine() error { |
| 177 | + return nil |
74 | 178 | }
|
75 | 179 |
|
76 |
| -func download(units Units) { |
77 |
| - fmt.Println(units) |
| 180 | +func (o *Option) clearCache() error { |
| 181 | + //TODO: remove temporary files |
| 182 | + return nil |
78 | 183 | }
|
0 commit comments