Commit 7f6e96f4 authored by Cristian Maglie's avatar Cristian Maglie Committed by Cristian Maglie

Updated go.bug.st/downloader

Fix #87
parent a32d52fa
...@@ -397,11 +397,11 @@ ...@@ -397,11 +397,11 @@
[[projects]] [[projects]]
branch = "master" branch = "master"
digest = "1:5cd3c0f530ab660c7b08f563020dc00b8e761c2f7650457a3f669b6601f7f28d" digest = "1:975e88bfd9ed90a0e939a9e67ecab0682a28c0b9ed021bd30f469a0b5b4e3739"
name = "go.bug.st/downloader" name = "go.bug.st/downloader"
packages = ["."] packages = ["."]
pruneopts = "UT" pruneopts = "UT"
revision = "60d736642c5c2252f6d19016d7e68178b19478f0" revision = "9b8976a44d87b5d86444bccc3ed875b2530f45e0"
[[projects]] [[projects]]
branch = "master" branch = "master"
......
...@@ -11,19 +11,20 @@ import ( ...@@ -11,19 +11,20 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"sync/atomic" "sync"
"time" "time"
) )
// Downloader is an asynchronous downloader // Downloader is an asynchronous downloader
type Downloader struct { type Downloader struct {
URL string URL string
Done chan bool Done chan bool
resp *http.Response resp *http.Response
out *os.File out *os.File
completed int64 completed int64
size int64 completedLock sync.Mutex
err error size int64
err error
} }
// DownloadOptions are optional flags that can be passed to Download function // DownloadOptions are optional flags that can be passed to Download function
...@@ -79,7 +80,9 @@ func (d *Downloader) AsyncRun() { ...@@ -79,7 +80,9 @@ func (d *Downloader) AsyncRun() {
n, err := in.Read(buff[:]) n, err := in.Read(buff[:])
if n > 0 { if n > 0 {
d.out.Write(buff[:n]) d.out.Write(buff[:n])
atomic.AddInt64(&d.completed, int64(n)) d.completedLock.Lock()
d.completed += int64(n)
d.completedLock.Unlock()
} }
if err == io.EOF { if err == io.EOF {
break break
...@@ -107,7 +110,10 @@ func (d *Downloader) Error() error { ...@@ -107,7 +110,10 @@ func (d *Downloader) Error() error {
// Completed returns the bytes read so far // Completed returns the bytes read so far
func (d *Downloader) Completed() int64 { func (d *Downloader) Completed() int64 {
return atomic.LoadInt64(&d.completed) d.completedLock.Lock()
res := d.completed
d.completedLock.Unlock()
return res
} }
// Download returns an asynchronous downloader that will donwload the specified url // Download returns an asynchronous downloader that will donwload the specified url
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment