-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
50 lines (41 loc) · 974 Bytes
/
web.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package web
import (
"net/http"
"github.com/omeid/gonzo"
"github.com/omeid/gonzo/context"
)
// Gets the list of urls and passes the results to output channel.
// It reports the progress to the Context using a ReadProgress proxy.
func Get(ctx context.Context, urls ...string) gonzo.Pipe {
ctx, cancel := context.WithCancel(ctx)
out := make(chan gonzo.File)
client := &http.Client{}
go func() {
defer close(out)
for _, url := range urls {
if url == "" {
ctx.Error("Empty URL.")
cancel()
return
}
select {
case <-ctx.Done():
ctx.Warn(context.Canceled)
return
default:
ctx.Infof("Downloading %s", url)
file, err := get(ctx, client, url)
if err != nil {
ctx.Error(err)
cancel()
break
}
//TODO: Add progress meter.
//s, _ := file.Stat()
//file.Reader = c.ReadProgress(file.Reader, "Downloading "+file.Path, s.Size())
out <- file
}
}
}()
return gonzo.NewPipe(ctx, out)
}