Skip to content

Commit

Permalink
Update ProxyFn signature
Browse files Browse the repository at this point in the history
ProxyFn now also accept the download aka download index or download chunk # and is no longer concatenated into the name by default;
this allows for the receiver to decided how to combine, if at all.

rename examples to _examples to avoid pulling in example dependencies with this library.
  • Loading branch information
deankarn committed May 14, 2017
1 parent c6c9e76 commit 51aad6b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package go-download
===================
![Project status](https://img.shields.io/badge/version-1.0.0-green.svg)
![Project status](https://img.shields.io/badge/version-2.0.0-green.svg)
[![Build Status](https://travis-ci.org/joeybloggs/go-download.svg?branch=master)](https://travis-ci.org/joeybloggs/go-download)
[![Coverage Status](https://coveralls.io/repos/github/joeybloggs/go-download/badge.svg?branch=master)](https://coveralls.io/github/joeybloggs/go-download?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/joeybloggs/go-download)](https://goreportcard.com/report/github.com/joeybloggs/go-download)
Expand All @@ -20,7 +20,7 @@ go get -u github.com/joeybloggs/go-download

## Examples

More examples [here](https://github.com/joeybloggs/go-download/tree/master/examples)
More examples [here](https://github.com/joeybloggs/go-download/tree/master/_examples)

```go
package main
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"
"log"

Expand All @@ -14,9 +15,9 @@ func main() {
defer progress.Stop()

options := &download.Options{
Proxy: func(name string, size int64, r io.Reader) io.Reader {
Proxy: func(name string, download int, size int64, r io.Reader) io.Reader {
bar := progress.AddBar(size).
PrependName(name, 0, 0).
PrependName(fmt.Sprintf("%s-%d", name, download), 0, 0).
PrependCounters("%3s / %3s", mpb.UnitBytes, 18, mpb.DwidthSync|mpb.DextraSpace).
AppendPercentage(5, 0)

Expand Down
24 changes: 13 additions & 11 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ type ConcurrencyFn func(size int64) int

// ProxyFn is the function used to pass the download io.Reader for proxying.
// eg. displaying a progress bar of the download.
type ProxyFn func(name string, size int64, r io.Reader) io.Reader
type ProxyFn func(name string, download int, size int64, r io.Reader) io.Reader

// File represents an open file descriptor to a downloaded file(s)
type File struct {
url string
dir string
size int64
modTime time.Time
options *Options
readers []io.ReadCloser
url string
dir string
baseName string
size int64
modTime time.Time
options *Options
readers []io.ReadCloser
io.Reader
}

Expand Down Expand Up @@ -78,8 +79,9 @@ func OpenContext(ctx context.Context, url string, options *Options) (*File, erro
}

f := &File{
url: url,
options: options,
url: url,
baseName: filepath.Base(url),
options: options,
}

resp, err := http.Head(f.url)
Expand Down Expand Up @@ -143,7 +145,7 @@ func (f *File) download(ctx context.Context) error {
var read io.Reader = resp.Body

if f.options != nil && f.options.Proxy != nil {
read = f.options.Proxy(filepath.Base(f.url), f.size, read)
read = f.options.Proxy(f.baseName, 0, f.size, read)
}

_, err = io.Copy(fh, read)
Expand Down Expand Up @@ -351,7 +353,7 @@ func (f *File) downloadPartial(ctx context.Context, resumeable bool, idx int, st
var read io.Reader = resp.Body

if f.options != nil && f.options.Proxy != nil {
read = f.options.Proxy(fmt.Sprintf("%s-%d", filepath.Base(f.url), idx), (end-start)+1, read)
read = f.options.Proxy(f.baseName, idx, (end-start)+1, read)
}

_, err = io.Copy(fh, read)
Expand Down
4 changes: 2 additions & 2 deletions download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestDownloadRangeBasic(t *testing.T) {
url := server.URL + "/testdata/data.txt"

options := &Options{
Proxy: func(name string, size int64, r io.Reader) io.Reader {
Proxy: func(name string, download int, size int64, r io.Reader) io.Reader {
return r // just mocking consumption
},
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestDownloadBasic(t *testing.T) {
url := server.URL + "/testdata/data.txt"

options := &Options{
Proxy: func(name string, size int64, r io.Reader) io.Reader {
Proxy: func(name string, download int, size int64, r io.Reader) io.Reader {
return r // just mocking consumption
},
}
Expand Down

0 comments on commit 51aad6b

Please sign in to comment.