mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-01 04:40:38 +00:00
Bind progress into Downloader.
This commit is contained in:
+22
-7
@@ -19,6 +19,7 @@ type Downloader interface {
|
|||||||
Pause()
|
Pause()
|
||||||
Resume()
|
Resume()
|
||||||
Shutdown()
|
Shutdown()
|
||||||
|
GetProgress() *Progress
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check interface
|
// Check interface
|
||||||
@@ -33,6 +34,7 @@ type downloaderImpl struct {
|
|||||||
stopped chan bool
|
stopped chan bool
|
||||||
pause chan bool
|
pause chan bool
|
||||||
unpause chan bool
|
unpause chan bool
|
||||||
|
progress *Progress
|
||||||
threads int
|
threads int
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,8 +57,11 @@ func NewDownloader(threads int) Downloader {
|
|||||||
pause: make(chan bool),
|
pause: make(chan bool),
|
||||||
unpause: make(chan bool),
|
unpause: make(chan bool),
|
||||||
threads: threads,
|
threads: threads,
|
||||||
|
progress: NewProgress(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
downloader.progress.Start()
|
||||||
|
|
||||||
for i := 0; i < downloader.threads; i++ {
|
for i := 0; i < downloader.threads; i++ {
|
||||||
go downloader.process()
|
go downloader.process()
|
||||||
}
|
}
|
||||||
@@ -74,6 +79,8 @@ func (downloader *downloaderImpl) Shutdown() {
|
|||||||
for i := 0; i < downloader.threads; i++ {
|
for i := 0; i < downloader.threads; i++ {
|
||||||
<-downloader.stopped
|
<-downloader.stopped
|
||||||
}
|
}
|
||||||
|
|
||||||
|
downloader.progress.Shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pause pauses task processing
|
// Pause pauses task processing
|
||||||
@@ -90,6 +97,11 @@ func (downloader *downloaderImpl) Resume() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resume resumes task processing
|
||||||
|
func (downloader *downloaderImpl) GetProgress() *Progress {
|
||||||
|
return downloader.progress
|
||||||
|
}
|
||||||
|
|
||||||
// Download starts new download task
|
// Download starts new download task
|
||||||
func (downloader *downloaderImpl) Download(url string, destination string, result chan<- error) {
|
func (downloader *downloaderImpl) Download(url string, destination string, result chan<- error) {
|
||||||
downloader.DownloadWithChecksum(url, destination, result, ChecksumInfo{Size: -1}, false)
|
downloader.DownloadWithChecksum(url, destination, result, ChecksumInfo{Size: -1}, false)
|
||||||
@@ -103,7 +115,7 @@ func (downloader *downloaderImpl) DownloadWithChecksum(url string, destination s
|
|||||||
|
|
||||||
// handleTask processes single download task
|
// handleTask processes single download task
|
||||||
func (downloader *downloaderImpl) handleTask(task *downloadTask) {
|
func (downloader *downloaderImpl) handleTask(task *downloadTask) {
|
||||||
fmt.Printf("Downloading %s...\n", task.url)
|
downloader.progress.Printf("Downloading %s...\n", task.url)
|
||||||
|
|
||||||
resp, err := http.Get(task.url)
|
resp, err := http.Get(task.url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -132,16 +144,19 @@ func (downloader *downloaderImpl) handleTask(task *downloadTask) {
|
|||||||
}
|
}
|
||||||
defer outfile.Close()
|
defer outfile.Close()
|
||||||
|
|
||||||
var w io.Writer
|
|
||||||
|
|
||||||
checksummer := NewChecksumWriter()
|
checksummer := NewChecksumWriter()
|
||||||
|
writers := []io.Writer{outfile}
|
||||||
|
|
||||||
if task.expected.Size != -1 {
|
if task.expected.Size != -1 {
|
||||||
w = io.MultiWriter(outfile, checksummer)
|
writers = append(writers, checksummer)
|
||||||
} else {
|
|
||||||
w = outfile
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if downloader.progress != nil {
|
||||||
|
writers = append(writers, downloader.progress)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := io.MultiWriter(writers...)
|
||||||
|
|
||||||
_, err = io.Copy(w, resp.Body)
|
_, err = io.Copy(w, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Remove(temppath)
|
os.Remove(temppath)
|
||||||
@@ -164,7 +179,7 @@ func (downloader *downloaderImpl) handleTask(task *downloadTask) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if task.ignoreMismatch {
|
if task.ignoreMismatch {
|
||||||
fmt.Printf("WARNING: %s\n", err.Error())
|
downloader.progress.Printf("WARNING: %s\n", err.Error())
|
||||||
} else {
|
} else {
|
||||||
os.Remove(temppath)
|
os.Remove(temppath)
|
||||||
task.result <- err
|
task.result <- err
|
||||||
|
|||||||
@@ -117,3 +117,8 @@ func (f *FakeDownloader) Pause() {
|
|||||||
// Resume does nothing
|
// Resume does nothing
|
||||||
func (f *FakeDownloader) Resume() {
|
func (f *FakeDownloader) Resume() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetProgress does nothing
|
||||||
|
func (f *FakeDownloader) GetProgress() *Progress {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user