mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-06-02 08:09:51 -04:00
Fix high memory usage due to downloads
This commit is contained in:
parent
28b2ef38d9
commit
0fe563ced1
1 changed files with 17 additions and 29 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/cavaliergopher/grab/v3"
|
"github.com/cavaliergopher/grab/v3"
|
||||||
"github.com/gotk3/gotk3/glib"
|
"github.com/gotk3/gotk3/glib"
|
||||||
|
@ -83,51 +84,38 @@ func CreateProgressWindow(parent *gtk.Window) (ProgressWindow, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadFile(progressWindow *ProgressWindow, client *grab.Client, url string, outputPath string) error {
|
func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadURL string, dstPath string) error {
|
||||||
if progressWindow.cancelled {
|
req, err := grab.NewRequest(dstPath, downloadURL)
|
||||||
return fmt.Errorf("cancelled")
|
|
||||||
}
|
|
||||||
done := false
|
|
||||||
req, err := grab.NewRequest(outputPath, url)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filePath := path.Base(dstPath)
|
||||||
|
|
||||||
resp := client.Do(req)
|
resp := client.Do(req)
|
||||||
|
t := time.NewTicker(500 * time.Millisecond)
|
||||||
|
defer t.Stop()
|
||||||
|
|
||||||
filePath := path.Base(outputPath)
|
Loop:
|
||||||
|
for {
|
||||||
go func(err *error) {
|
select {
|
||||||
for !resp.IsComplete() {
|
case <-t.C:
|
||||||
glib.IdleAdd(func() {
|
glib.IdleAdd(func() {
|
||||||
progressWindow.label.SetText(filePath)
|
progressWindow.label.SetText(filePath)
|
||||||
progressWindow.bar.SetFraction(resp.Progress())
|
progressWindow.bar.SetFraction(resp.Progress())
|
||||||
progressWindow.percentLabel.SetText(fmt.Sprintf("%.0f%%", 100*resp.Progress()))
|
progressWindow.percentLabel.SetText(fmt.Sprintf("%.0f%%", 100*resp.Progress()))
|
||||||
})
|
})
|
||||||
|
|
||||||
if progressWindow.cancelled {
|
if progressWindow.cancelled {
|
||||||
resp.Cancel()
|
resp.Cancel()
|
||||||
break
|
break Loop
|
||||||
}
|
}
|
||||||
}
|
case <-resp.Done:
|
||||||
|
if err := resp.Err(); err != nil {
|
||||||
*err = resp.Err()
|
return fmt.Errorf("download error: %+v", err)
|
||||||
done = true
|
}
|
||||||
}(&err)
|
break Loop
|
||||||
|
|
||||||
for {
|
|
||||||
for gtk.EventsPending() {
|
|
||||||
gtk.MainIteration()
|
|
||||||
}
|
|
||||||
if done {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue