From 4ba2eaf40705b372e6370130c14ade6279e1adc8 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Mon, 26 Nov 2018 17:03:58 +0900 Subject: [PATCH 1/7] add: downloader package --- .../yoheimiyamoto/downloader/downloader.go | 72 +++++++++++++++++++ .../downloader/downloader_test.go | 32 +++++++++ 2 files changed, 104 insertions(+) create mode 100644 kadai3-2/yoheimiyamoto/downloader/downloader.go create mode 100644 kadai3-2/yoheimiyamoto/downloader/downloader_test.go diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader.go b/kadai3-2/yoheimiyamoto/downloader/downloader.go new file mode 100644 index 0000000..e62e86f --- /dev/null +++ b/kadai3-2/yoheimiyamoto/downloader/downloader.go @@ -0,0 +1,72 @@ +package downloader + +import ( + "fmt" + "io" + "net/http" + "os" + "strconv" +) + +// Client ... +type Client struct { + *http.Client + url string +} + +// NewClient ... +func NewClient(url string) *Client { + return &Client{new(http.Client), url} +} + +// contentLengthを取得 +func (c *Client) contentLength() (int, error) { + res, err := c.Head(c.url) + if err != nil { + return 0, err + } + return strconv.Atoi(res.Header.Get("Content-Length")) +} + +// dst -> ダウンロード先のファイル名 +func (c *Client) download(dst string, start, end int) error { + req, _ := http.NewRequest("GET", c.url, nil) + req.Header.Set("Authorization", "Bearer access-token") + req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end)) + + res, err := c.Do(req) + if err != nil { + return err + } + defer res.Body.Close() + f, err := os.Create(dst) + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(f, res.Body) + if err != nil { + return err + } + return nil +} + +func (c *Client) mergeFiles(src []string, dst string) error { + f, err := os.Create(dst) + if err != nil { + return err + } + defer f.Close() + for _, s := range src { + _f, err := os.Open(s) + if err != nil { + return err + } + _, err = io.Copy(f, _f) + if err != nil { + return err + } + _f.Close() + } + return nil +} diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go new file mode 100644 index 0000000..a39d3f9 --- /dev/null +++ b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go @@ -0,0 +1,32 @@ +package downloader + +import "testing" + +const URL = "https://images.pexels.com/photos/248304/pexels-photo-248304.jpeg" + +func TestContentLength(t *testing.T) { + c := NewClient(URL) + l, err := c.contentLength() + if err != nil { + t.Error(err) + } + expect := 6480509 + if l != expect { + t.Errorf("expect: %d, actual: %d", expect, l) + } +} + +func TestDownload(t *testing.T) { + c := NewClient(URL) + c.download("1.jpg", 0, 5000) + c.download("2.jpg", 5001, 6480509) +} + +func TestMergeFiles(t *testing.T) { + c := NewClient(URL) + src := []string{"1.jpg", "2.jpg"} + err := c.mergeFiles(src, "image.jpg") + if err != nil { + t.Error(err) + } +} From 6df3f9faf5ee5b2342f5f75dec99e6523e997e4c Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Mon, 26 Nov 2018 17:07:00 +0900 Subject: [PATCH 2/7] Update downloader.go --- .../yoheimiyamoto/downloader/downloader.go | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader.go b/kadai3-2/yoheimiyamoto/downloader/downloader.go index e62e86f..501f4fa 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader.go +++ b/kadai3-2/yoheimiyamoto/downloader/downloader.go @@ -19,15 +19,9 @@ func NewClient(url string) *Client { return &Client{new(http.Client), url} } -// contentLengthを取得 -func (c *Client) contentLength() (int, error) { - res, err := c.Head(c.url) - if err != nil { - return 0, err - } - return strconv.Atoi(res.Header.Get("Content-Length")) -} - +// ファイルの分割ダウンロード +// start -> 開始のbyte +// end -> 終了のbyte // dst -> ダウンロード先のファイル名 func (c *Client) download(dst string, start, end int) error { req, _ := http.NewRequest("GET", c.url, nil) @@ -51,6 +45,9 @@ func (c *Client) download(dst string, start, end int) error { return nil } +// ダウンロードした分割ファイルのマージ +// src -> 元の分割ファイル名 +// dst -> マージ先のファイル名 func (c *Client) mergeFiles(src []string, dst string) error { f, err := os.Create(dst) if err != nil { @@ -70,3 +67,12 @@ func (c *Client) mergeFiles(src []string, dst string) error { } return nil } + +// contentLengthを取得 +func (c *Client) contentLength() (int, error) { + res, err := c.Head(c.url) + if err != nil { + return 0, err + } + return strconv.Atoi(res.Header.Get("Content-Length")) +} From acb416bcba15b6b7932706291ad0c0a092b52b46 Mon Sep 17 00:00:00 2001 From: yoheimiyamoto Date: Mon, 26 Nov 2018 18:00:22 +0900 Subject: [PATCH 3/7] draft --- .../yoheimiyamoto/downloader/downloader.go | 52 +++++++++++++++++-- .../downloader/downloader_test.go | 13 +++-- kadai3-2/yoheimiyamoto/main.go | 5 ++ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 kadai3-2/yoheimiyamoto/main.go diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader.go b/kadai3-2/yoheimiyamoto/downloader/downloader.go index 501f4fa..aa8059d 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader.go +++ b/kadai3-2/yoheimiyamoto/downloader/downloader.go @@ -14,26 +14,72 @@ type Client struct { url string } +type rangeProperty struct { + dst string // 一時保存先のファイル名 + start int // 開始のバイト数 + end int // 終了のバイト数 +} + // NewClient ... func NewClient(url string) *Client { return &Client{new(http.Client), url} } +// +func newRangeProperties(contentLength int) []*rangeProperty { + // const range = 10000 + var out []*rangeProperty + f := func(i int) (int, int) { + return 0, 0 + } + start := 1 + end := start + 10000 + for { + r := &rangeProperty{ + dst: fmt.Sprintf("file%d.jpg", i), + start: start, + end: end, + } + out = append(out, r) + start += 10000 + end := start + 10000 + if end > contentLength { + end = contentLength + } + } + return out +} + +func Download() { + +} + +// 一時保存ファイルの全削除 +func removeFiles(ps []*rangeProperty) error { + for _, p := range ps { + err := os.Remove(p.dst) + if err != nil { + return err + } + } + return nil +} + // ファイルの分割ダウンロード // start -> 開始のbyte // end -> 終了のbyte // dst -> ダウンロード先のファイル名 -func (c *Client) download(dst string, start, end int) error { +func (c *Client) rangeDownload(r *rangeProperty) error { req, _ := http.NewRequest("GET", c.url, nil) req.Header.Set("Authorization", "Bearer access-token") - req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end)) + req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", r.start, r.end)) res, err := c.Do(req) if err != nil { return err } defer res.Body.Close() - f, err := os.Create(dst) + f, err := os.Create(r.dst) if err != nil { return err } diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go index a39d3f9..5ca9dff 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go +++ b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go @@ -16,10 +16,15 @@ func TestContentLength(t *testing.T) { } } -func TestDownload(t *testing.T) { - c := NewClient(URL) - c.download("1.jpg", 0, 5000) - c.download("2.jpg", 5001, 6480509) +func TestNewRangeProperties(t *testing.T) { + ps := newRangeProperties(40000) + t.Logf("%v", ps) +} + +func TestRangeDownload(t *testing.T) { + // c := NewClient(URL) + // c.rangeDownload("1.jpg", 0, 5000) + // c.rangeDownload("2.jpg", 5001, 6480509) } func TestMergeFiles(t *testing.T) { diff --git a/kadai3-2/yoheimiyamoto/main.go b/kadai3-2/yoheimiyamoto/main.go new file mode 100644 index 0000000..d3103f9 --- /dev/null +++ b/kadai3-2/yoheimiyamoto/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + +} \ No newline at end of file From d024a1dd074ad554789e2d4a633ea4bab9b3ffd7 Mon Sep 17 00:00:00 2001 From: YoheiMiyamoto Date: Fri, 30 Nov 2018 12:23:26 +0900 Subject: [PATCH 4/7] update --- .../yoheimiyamoto/downloader/downloader.go | 110 ++++++++++++------ .../downloader/downloader_test.go | 20 ++-- kadai3-2/yoheimiyamoto/downloader/image.jpg | 0 kadai3-2/yoheimiyamoto/main.go | 15 ++- 4 files changed, 98 insertions(+), 47 deletions(-) create mode 100644 kadai3-2/yoheimiyamoto/downloader/image.jpg diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader.go b/kadai3-2/yoheimiyamoto/downloader/downloader.go index aa8059d..c6d5816 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader.go +++ b/kadai3-2/yoheimiyamoto/downloader/downloader.go @@ -3,6 +3,7 @@ package downloader import ( "fmt" "io" + "math" "net/http" "os" "strconv" @@ -15,7 +16,7 @@ type Client struct { } type rangeProperty struct { - dst string // 一時保存先のファイル名 + path string // 一時保存先のファイル名 start int // 開始のバイト数 end int // 終了のバイト数 } @@ -25,44 +26,77 @@ func NewClient(url string) *Client { return &Client{new(http.Client), url} } -// -func newRangeProperties(contentLength int) []*rangeProperty { - // const range = 10000 - var out []*rangeProperty - f := func(i int) (int, int) { - return 0, 0 +// Download ... +func (c *Client) Download() error { + l, err := c.contentLength() + if err != nil { + return err } - start := 1 - end := start + 10000 - for { - r := &rangeProperty{ - dst: fmt.Sprintf("file%d.jpg", i), - start: start, - end: end, + ps := newRangeProperties(l) + for _, p := range ps { + err = c.rangeDownload(p) + if err != nil { + return err + } + } + for _, p := range ps { + fmt.Printf("%v", p) + } + defer removeFiles(ps) + src := make([]string, len(ps)) + for i, p := range ps { + src[i] = p.path + } + err = mergeFiles(src, "output.jpg") + if err != nil { + return nil + } + return nil +} + +// contentLengthを取得 +func (c *Client) contentLength() (int, error) { + res, err := c.Head(c.url) + if err != nil { + return 0, err + } + return strconv.Atoi(res.Header.Get("Content-Length")) +} + +// rangeDownloadの引数として必要なrangePropertyを生成 +func newRangeProperties(contentLength int) []*rangeProperty { + num := 1000000 + + maxIndex := int(math.Ceil(float64(contentLength) / float64(num))) + + f := func(i int) *rangeProperty { + start := 0 + if i != 0 { + start = i*num + 1 } - out = append(out, r) - start += 10000 - end := start + 10000 + end := (i + 1) * num if end > contentLength { end = contentLength } + return &rangeProperty{ + path: fmt.Sprintf("file%d.jpg", i), + start: start, + end: end, + } } - return out -} - -func Download() { -} + var out []*rangeProperty -// 一時保存ファイルの全削除 -func removeFiles(ps []*rangeProperty) error { - for _, p := range ps { - err := os.Remove(p.dst) - if err != nil { - return err + for i := 0; i < maxIndex; i++ { + start := i + num + end := start + num + if end > contentLength { + end = contentLength } + out = append(out, f(i)) } - return nil + + return out } // ファイルの分割ダウンロード @@ -79,7 +113,7 @@ func (c *Client) rangeDownload(r *rangeProperty) error { return err } defer res.Body.Close() - f, err := os.Create(r.dst) + f, err := os.Create(r.path) if err != nil { return err } @@ -94,7 +128,7 @@ func (c *Client) rangeDownload(r *rangeProperty) error { // ダウンロードした分割ファイルのマージ // src -> 元の分割ファイル名 // dst -> マージ先のファイル名 -func (c *Client) mergeFiles(src []string, dst string) error { +func mergeFiles(src []string, dst string) error { f, err := os.Create(dst) if err != nil { return err @@ -114,11 +148,13 @@ func (c *Client) mergeFiles(src []string, dst string) error { return nil } -// contentLengthを取得 -func (c *Client) contentLength() (int, error) { - res, err := c.Head(c.url) - if err != nil { - return 0, err +// 一時保存ファイルの全削除 +func removeFiles(ps []*rangeProperty) error { + for _, p := range ps { + err := os.Remove(p.path) + if err != nil { + return err + } } - return strconv.Atoi(res.Header.Get("Content-Length")) + return nil } diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go index 5ca9dff..ff30e39 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go +++ b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go @@ -17,8 +17,12 @@ func TestContentLength(t *testing.T) { } func TestNewRangeProperties(t *testing.T) { - ps := newRangeProperties(40000) - t.Logf("%v", ps) + ps := newRangeProperties(40001) + // t.Logf("%v", ps[0]) + // t.Logf("%v", ps[1]) + // t.Logf("%v", ps[2]) + // t.Logf("%v", ps[3]) + // t.Logf("%v", ps[4]) } func TestRangeDownload(t *testing.T) { @@ -28,10 +32,10 @@ func TestRangeDownload(t *testing.T) { } func TestMergeFiles(t *testing.T) { - c := NewClient(URL) - src := []string{"1.jpg", "2.jpg"} - err := c.mergeFiles(src, "image.jpg") - if err != nil { - t.Error(err) - } + // c := NewClient(URL) + // src := []string{"1.jpg", "2.jpg"} + // err := c.mergeFiles(src, "image.jpg") + // if err != nil { + // t.Error(err) + // } } diff --git a/kadai3-2/yoheimiyamoto/downloader/image.jpg b/kadai3-2/yoheimiyamoto/downloader/image.jpg new file mode 100644 index 0000000..e69de29 diff --git a/kadai3-2/yoheimiyamoto/main.go b/kadai3-2/yoheimiyamoto/main.go index d3103f9..ba495d0 100644 --- a/kadai3-2/yoheimiyamoto/main.go +++ b/kadai3-2/yoheimiyamoto/main.go @@ -1,5 +1,16 @@ package main +import ( + "fmt" + "os" + + "github.com/YoheiMiyamoto/dojo4/kadai3-2/yoheimiyamoto/downloader" +) + func main() { - -} \ No newline at end of file + c := downloader.NewClient("https://images.pexels.com/photos/248304/pexels-photo-248304.jpeg") + err := c.Download() + if err != nil { + fmt.Fprintln(os.Stdout, err.Error()) + } +} From cb891c71c4bd173f69e8cb22ecae35f955315fb0 Mon Sep 17 00:00:00 2001 From: YoheiMiyamoto Date: Fri, 30 Nov 2018 12:23:40 +0900 Subject: [PATCH 5/7] update --- kadai3-2/yoheimiyamoto/downloader/downloader_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go index ff30e39..8a03437 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go +++ b/kadai3-2/yoheimiyamoto/downloader/downloader_test.go @@ -17,7 +17,7 @@ func TestContentLength(t *testing.T) { } func TestNewRangeProperties(t *testing.T) { - ps := newRangeProperties(40001) + // ps := newRangeProperties(40001) // t.Logf("%v", ps[0]) // t.Logf("%v", ps[1]) // t.Logf("%v", ps[2]) From bcb4b4a07db4bb6b3a93dfc63684fa3d9f3979ab Mon Sep 17 00:00:00 2001 From: YoheiMiyamoto Date: Sun, 2 Dec 2018 09:18:39 +0900 Subject: [PATCH 6/7] update --- .../downloader/{downloader.go => client.go} | 120 ++++++++---------- .../{downloader_test.go => client_test.go} | 0 .../downloader/range_property.go | 48 +++++++ kadai3-2/yoheimiyamoto/main.go | 3 +- 4 files changed, 105 insertions(+), 66 deletions(-) rename kadai3-2/yoheimiyamoto/downloader/{downloader.go => client.go} (53%) rename kadai3-2/yoheimiyamoto/downloader/{downloader_test.go => client_test.go} (100%) create mode 100644 kadai3-2/yoheimiyamoto/downloader/range_property.go diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader.go b/kadai3-2/yoheimiyamoto/downloader/client.go similarity index 53% rename from kadai3-2/yoheimiyamoto/downloader/downloader.go rename to kadai3-2/yoheimiyamoto/downloader/client.go index c6d5816..8639bb9 100644 --- a/kadai3-2/yoheimiyamoto/downloader/downloader.go +++ b/kadai3-2/yoheimiyamoto/downloader/client.go @@ -1,12 +1,14 @@ package downloader import ( + "context" "fmt" "io" - "math" "net/http" "os" "strconv" + + "golang.org/x/sync/errgroup" ) // Client ... @@ -15,34 +17,38 @@ type Client struct { url string } -type rangeProperty struct { - path string // 一時保存先のファイル名 - start int // 開始のバイト数 - end int // 終了のバイト数 -} - // NewClient ... func NewClient(url string) *Client { return &Client{new(http.Client), url} } // Download ... -func (c *Client) Download() error { +func (c *Client) Download(ctx context.Context) error { l, err := c.contentLength() if err != nil { return err } + + fmt.Fprintf(os.Stdout, "contentLength: %d\n", l) + + eg, ctx := errgroup.WithContext(ctx) + ps := newRangeProperties(l) + fmt.Fprintf(os.Stdout, "ps: %d\n", len(ps)) + for _, p := range ps { - err = c.rangeDownload(p) - if err != nil { - return err - } + p := p + eg.Go(func() error { + return c.rangeDownload(ctx, p) + }) } - for _, p := range ps { - fmt.Printf("%v", p) + + if err := eg.Wait(); err != nil { + return err } + defer removeFiles(ps) + src := make([]string, len(ps)) for i, p := range ps { src[i] = p.path @@ -63,65 +69,49 @@ func (c *Client) contentLength() (int, error) { return strconv.Atoi(res.Header.Get("Content-Length")) } -// rangeDownloadの引数として必要なrangePropertyを生成 -func newRangeProperties(contentLength int) []*rangeProperty { - num := 1000000 +// ファイルの分割ダウンロード +// start -> 開始のbyte +// end -> 終了のbyte +// dst -> ダウンロード先のファイル名 +func (c *Client) rangeDownload(ctx context.Context, r *rangeProperty) error { + errCh := make(chan error, 1) - maxIndex := int(math.Ceil(float64(contentLength) / float64(num))) + go func() { + req, _ := http.NewRequest("GET", c.url, nil) + req.Header.Set("Authorization", "Bearer access-token") + req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", r.start, r.end)) - f := func(i int) *rangeProperty { - start := 0 - if i != 0 { - start = i*num + 1 + res, err := c.Do(req) + if err != nil { + errCh <- err + return } - end := (i + 1) * num - if end > contentLength { - end = contentLength + defer res.Body.Close() + + f, err := os.Create(r.path) + if err != nil { + errCh <- err + return } - return &rangeProperty{ - path: fmt.Sprintf("file%d.jpg", i), - start: start, - end: end, + defer f.Close() + _, err = io.Copy(f, res.Body) + if err != nil { + errCh <- err + return } - } - - var out []*rangeProperty - - for i := 0; i < maxIndex; i++ { - start := i + num - end := start + num - if end > contentLength { - end = contentLength + errCh <- nil + }() + select { + case err := <-errCh: + if err != nil { + fmt.Println(err.Error()) + return err } - out = append(out, f(i)) + case <-ctx.Done(): + fmt.Println("強制終了") + return ctx.Err() } - return out -} - -// ファイルの分割ダウンロード -// start -> 開始のbyte -// end -> 終了のbyte -// dst -> ダウンロード先のファイル名 -func (c *Client) rangeDownload(r *rangeProperty) error { - req, _ := http.NewRequest("GET", c.url, nil) - req.Header.Set("Authorization", "Bearer access-token") - req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", r.start, r.end)) - - res, err := c.Do(req) - if err != nil { - return err - } - defer res.Body.Close() - f, err := os.Create(r.path) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, res.Body) - if err != nil { - return err - } return nil } diff --git a/kadai3-2/yoheimiyamoto/downloader/downloader_test.go b/kadai3-2/yoheimiyamoto/downloader/client_test.go similarity index 100% rename from kadai3-2/yoheimiyamoto/downloader/downloader_test.go rename to kadai3-2/yoheimiyamoto/downloader/client_test.go diff --git a/kadai3-2/yoheimiyamoto/downloader/range_property.go b/kadai3-2/yoheimiyamoto/downloader/range_property.go new file mode 100644 index 0000000..276eb18 --- /dev/null +++ b/kadai3-2/yoheimiyamoto/downloader/range_property.go @@ -0,0 +1,48 @@ +package downloader + +import ( + "fmt" + "math" +) + +type rangeProperty struct { + path string // 一時保存先のファイル名 + start int // 開始のバイト数 + end int // 終了のバイト数 +} + +// rangeDownloadの引数として必要なrangePropertyを生成 +func newRangeProperties(contentLength int) []*rangeProperty { + num := 1000000 + + maxIndex := int(math.Ceil(float64(contentLength) / float64(num))) + + f := func(i int) *rangeProperty { + start := 0 + if i != 0 { + start = i*num + 1 + } + end := (i + 1) * num + if end > contentLength { + end = contentLength + } + return &rangeProperty{ + path: fmt.Sprintf("file%d.jpg", i), + start: start, + end: end, + } + } + + var out []*rangeProperty + + for i := 0; i < maxIndex; i++ { + start := i + num + end := start + num + if end > contentLength { + end = contentLength + } + out = append(out, f(i)) + } + + return out +} diff --git a/kadai3-2/yoheimiyamoto/main.go b/kadai3-2/yoheimiyamoto/main.go index ba495d0..051925d 100644 --- a/kadai3-2/yoheimiyamoto/main.go +++ b/kadai3-2/yoheimiyamoto/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "os" @@ -9,7 +10,7 @@ import ( func main() { c := downloader.NewClient("https://images.pexels.com/photos/248304/pexels-photo-248304.jpeg") - err := c.Download() + err := c.Download(context.Background()) if err != nil { fmt.Fprintln(os.Stdout, err.Error()) } From cb371943622207f9752e4bc3801d407aa2fe2013 Mon Sep 17 00:00:00 2001 From: YoheiMiyamoto Date: Sun, 2 Dec 2018 09:24:09 +0900 Subject: [PATCH 7/7] Update client.go --- kadai3-2/yoheimiyamoto/downloader/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kadai3-2/yoheimiyamoto/downloader/client.go b/kadai3-2/yoheimiyamoto/downloader/client.go index 8639bb9..e274b25 100644 --- a/kadai3-2/yoheimiyamoto/downloader/client.go +++ b/kadai3-2/yoheimiyamoto/downloader/client.go @@ -94,6 +94,7 @@ func (c *Client) rangeDownload(ctx context.Context, r *rangeProperty) error { return } defer f.Close() + _, err = io.Copy(f, res.Body) if err != nil { errCh <- err @@ -101,14 +102,13 @@ func (c *Client) rangeDownload(ctx context.Context, r *rangeProperty) error { } errCh <- nil }() + select { case err := <-errCh: if err != nil { - fmt.Println(err.Error()) return err } case <-ctx.Done(): - fmt.Println("強制終了") return ctx.Err() }