Skip to content

Commit 4618cbe

Browse files
committed
[rget] download temp file to temp dir, add combine function
1 parent 54921b1 commit 4618cbe

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

kadai3/imura81gt/rget/rget.go

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"io/ioutil"
78
"net/http"
89
"os"
910
"path"
11+
"path/filepath"
1012

1113
"golang.org/x/sync/errgroup"
1214
)
@@ -36,7 +38,19 @@ func Run(option Option) {
3638

3739
option.divide()
3840

39-
err = option.parallelDownload()
41+
tmpDir, err := ioutil.TempDir("", "rget")
42+
if err != nil {
43+
fmt.Errorf("%s", err)
44+
}
45+
defer os.RemoveAll(tmpDir)
46+
fmt.Println(tmpDir)
47+
48+
err = option.parallelDownload(tmpDir)
49+
if err != nil {
50+
fmt.Errorf("%s", err)
51+
}
52+
53+
err = option.combine(tmpDir)
4054
if err != nil {
4155
fmt.Errorf("%s", err)
4256
}
@@ -47,27 +61,19 @@ func (o *Option) contentLength() error {
4761
//resp, err := http.Head(url)
4862
resp, err := http.Head(o.URL)
4963
if err != nil {
50-
fmt.Fprintln(os.Stderr, err)
51-
//return 0, err
5264
return err
5365
}
5466

5567
if resp.Header.Get("Accept-Ranges") == "" {
5668
err := fmt.Errorf("%s URL cannot support Ranges Requests", o.URL)
57-
// fmt.Fprintln(os.Stderr, err)
58-
//return resp.ContentLength, err
5969
return err
6070
}
6171
if resp.Header["Accept-Ranges"][0] == "none" {
6272
err := fmt.Errorf("%s cannot support Ranges Requests", o.URL)
63-
// fmt.Fprintln(os.Stderr, err)
64-
//return resp.ContentLength, err
6573
return err
6674
}
6775
if resp.ContentLength == 0 {
6876
err := fmt.Errorf("%s size is %s", o.URL, resp.Header["Content-Length"][0])
69-
// fmt.Fprintln(os.Stderr, err)
70-
//return resp.ContentLength, err
7177
return err
7278
}
7379

@@ -80,10 +86,9 @@ func (o *Option) contentLength() error {
8086
func (o *Option) divide() {
8187
var units []Unit
8288

83-
//sbyte := contentLength / int64(concurrency)
89+
//TODO: if o.ContentLength < int64(o.Concurrency)
8490
sbyte := o.ContentLength / int64(o.Concurrency)
8591

86-
// for i := 0; i < concurrency; i++ {
8792
for i := 0; i < o.Concurrency; i++ {
8893
units = append(units, Unit{
8994
RangeStart: int64(i) * sbyte,
@@ -92,16 +97,13 @@ func (o *Option) divide() {
9297
})
9398
}
9499

100+
// TODO: should distribute the remainder to each unit
101+
units[len(units)-1].RangeEnd = o.ContentLength
102+
95103
o.Units = units
96-
//return units
97104
}
98105

99-
// func download(units Units) {
100-
// filepath.Split()
101-
// fmt.Println(units)
102-
// }
103-
104-
func (o *Option) parallelDownload() error {
106+
func (o *Option) parallelDownload(tmpDir string) error {
105107
fmt.Println("parallelDownload", o.Units)
106108

107109
eg, ctx := errgroup.WithContext(context.Background())
@@ -110,19 +112,22 @@ func (o *Option) parallelDownload() error {
110112
// https://golang.org/doc/faq#closures_and_goroutines
111113
i := i
112114
eg.Go(func() error {
113-
return o.downloadWithContext(ctx, i)
115+
return o.downloadWithContext(ctx, i, tmpDir)
114116
})
115117
}
116118

117119
if err := eg.Wait(); err != nil {
118-
o.clearCache()
119120
return err
120121
}
121122

122123
return nil
123124
}
124125

125-
func (o *Option) downloadWithContext(ctx context.Context, i int) error {
126+
func (o *Option) downloadWithContext(
127+
ctx context.Context,
128+
i int,
129+
dir string,
130+
) error {
126131
ctx, cancel := context.WithCancel(ctx)
127132
defer cancel()
128133

@@ -135,26 +140,26 @@ func (o *Option) downloadWithContext(ctx context.Context, i int) error {
135140
}
136141

137142
// add range header
138-
fmt.Printf(fmt.Sprintf("bytes=%d-%d", o.Units[i].RangeStart, o.Units[i].RangeEnd))
143+
fmt.Printf(fmt.Sprintf("bytes=%d-%d\n", o.Units[i].RangeStart, o.Units[i].RangeEnd))
139144
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", o.Units[i].RangeStart, o.Units[i].RangeEnd))
140145

141-
client := &http.Client{}
146+
client := http.DefaultClient
142147
resp, err := client.Do(req)
143-
defer resp.Body.Close()
144148
if err != nil {
149+
fmt.Printf("client err: %s", err)
145150
return fmt.Errorf("Error: %v", err)
146151
}
152+
defer resp.Body.Close()
147153

148154
select {
149155
case <-ctx.Done():
150156
fmt.Printf("Done: %v %+v\n", i, o.Units[i])
151157
return nil
152158
default:
153159
fmt.Println("default:", i, o.Units[i])
154-
//return fmt.Errorf("Error: %v %+v", i, o.Units[i])
155160
}
156161

157-
w, err := os.Create(o.Units[i].TempFileName)
162+
w, err := os.Create(filepath.Join(dir, o.Units[i].TempFileName))
158163
if err != nil {
159164
return fmt.Errorf("Error: %v", err)
160165
}
@@ -173,11 +178,29 @@ func (o *Option) downloadWithContext(ctx context.Context, i int) error {
173178
return nil
174179
}
175180

176-
func (o *Option) conbine() error {
177-
return nil
178-
}
181+
func (o *Option) combine(dir string) error {
182+
w, err := os.Create(path.Base(o.URL))
183+
if err != nil {
184+
return fmt.Errorf("Error: %v", err)
185+
}
186+
defer func() error {
187+
if err := w.Close(); err != nil {
188+
return fmt.Errorf("Error: %v", err)
189+
}
190+
return nil
191+
}()
192+
193+
for _, unit := range o.Units {
194+
r, err := os.Open(filepath.Join(dir, unit.TempFileName))
195+
if err != nil {
196+
return fmt.Errorf("Error: %v", err)
197+
}
198+
199+
_, err = io.Copy(w, r)
200+
if err != nil {
201+
return fmt.Errorf("Error: %v", err)
202+
}
203+
}
179204

180-
func (o *Option) clearCache() error {
181-
//TODO: remove temporary files
182205
return nil
183206
}

0 commit comments

Comments
 (0)