mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-06 22:18:28 +00:00
Bind progress into Downloader.
This commit is contained in:
+34
-19
@@ -19,6 +19,7 @@ type Downloader interface {
|
|||||||
Pause()
|
Pause()
|
||||||
Resume()
|
Resume()
|
||||||
Shutdown()
|
Shutdown()
|
||||||
|
GetProgress() *Progress
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check interface
|
// Check interface
|
||||||
@@ -28,12 +29,13 @@ var (
|
|||||||
|
|
||||||
// downloaderImpl is implementation of Downloader interface
|
// downloaderImpl is implementation of Downloader interface
|
||||||
type downloaderImpl struct {
|
type downloaderImpl struct {
|
||||||
queue chan *downloadTask
|
queue chan *downloadTask
|
||||||
stop chan bool
|
stop chan bool
|
||||||
stopped chan bool
|
stopped chan bool
|
||||||
pause chan bool
|
pause chan bool
|
||||||
unpause chan bool
|
unpause chan bool
|
||||||
threads int
|
progress *Progress
|
||||||
|
threads int
|
||||||
}
|
}
|
||||||
|
|
||||||
// downloadTask represents single item in queue
|
// downloadTask represents single item in queue
|
||||||
@@ -49,14 +51,17 @@ type downloadTask struct {
|
|||||||
// of threads
|
// of threads
|
||||||
func NewDownloader(threads int) Downloader {
|
func NewDownloader(threads int) Downloader {
|
||||||
downloader := &downloaderImpl{
|
downloader := &downloaderImpl{
|
||||||
queue: make(chan *downloadTask, 1000),
|
queue: make(chan *downloadTask, 1000),
|
||||||
stop: make(chan bool),
|
stop: make(chan bool),
|
||||||
stopped: make(chan bool),
|
stopped: make(chan bool),
|
||||||
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