Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resume incompleted downloads #322

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pkg/appstore/appstore_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,27 @@ func (t *appstore) downloadFile(src, dst string, progress *progressbar.ProgressB
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

file, err := t.os.OpenFile(dst, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}

defer file.Close()
stat, _ := file.Stat()
req.Header.Add("range", fmt.Sprintf("bytes=%d-", stat.Size()))

res, err := t.httpClient.Do(req)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()

file, err := t.os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}

defer file.Close()

if progress != nil {
progress.ChangeMax64(res.ContentLength)
progress.ChangeMax64(res.ContentLength + stat.Size())
progress.Set64(stat.Size())
file.Seek(0, io.SeekEnd)
_, err = io.Copy(io.MultiWriter(file, progress), res.Body)
} else {
_, err = io.Copy(file, res.Body)
Expand Down