| 32 | } |
| 33 | |
| 34 | func (p *Reader) Read(buf []byte) (int, error) { |
| 35 | read, err := p.in.Read(buf) |
| 36 | p.current += int64(read) |
| 37 | updateEvery := int64(1024 * 512) // 512kB |
| 38 | if p.size > 0 { |
| 39 | // Update progress for every 1% read if 1% < 512kB |
| 40 | if increment := int64(0.01 * float64(p.size)); increment < updateEvery { |
| 41 | updateEvery = increment |
| 42 | } |
| 43 | } |
| 44 | if p.current-p.lastUpdate > updateEvery || err != nil { |
| 45 | p.updateProgress(err != nil && read == 0) |
| 46 | p.lastUpdate = p.current |
| 47 | } |
| 48 | |
| 49 | return read, err |
| 50 | } |
| 51 | |
| 52 | // Close closes the progress reader and its underlying reader. |
| 53 | func (p *Reader) Close() error { |