Skip to content

Commit

Permalink
Fix high memory usage due to downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpl0itU committed Jul 19, 2023
1 parent 28b2ef3 commit 0fe563c
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"github.com/cavaliergopher/grab/v3"
"github.com/gotk3/gotk3/glib"
Expand Down Expand Up @@ -83,51 +84,38 @@ func CreateProgressWindow(parent *gtk.Window) (ProgressWindow, error) {
}, nil
}

func downloadFile(progressWindow *ProgressWindow, client *grab.Client, url string, outputPath string) error {
if progressWindow.cancelled {
return fmt.Errorf("cancelled")
}
done := false
req, err := grab.NewRequest(outputPath, url)
func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadURL string, dstPath string) error {
req, err := grab.NewRequest(dstPath, downloadURL)
if err != nil {
return err
}

resp := client.Do(req)
filePath := path.Base(dstPath)

filePath := path.Base(outputPath)
resp := client.Do(req)
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()

go func(err *error) {
for !resp.IsComplete() {
Loop:
for {
select {
case <-t.C:
glib.IdleAdd(func() {
progressWindow.label.SetText(filePath)
progressWindow.bar.SetFraction(resp.Progress())
progressWindow.percentLabel.SetText(fmt.Sprintf("%.0f%%", 100*resp.Progress()))
})

if progressWindow.cancelled {
resp.Cancel()
break
break Loop
}
case <-resp.Done:
if err := resp.Err(); err != nil {
return fmt.Errorf("download error: %+v", err)
}
break Loop
}

*err = resp.Err()
done = true
}(&err)

for {
for gtk.EventsPending() {
gtk.MainIteration()
}
if done {
break
}
}

if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 0fe563c

Please sign in to comment.