Refactor downloadFile function to simplify progress reporting

This commit is contained in:
Xpl0itU 2024-01-17 21:02:36 +01:00
parent 34bdbbd8ce
commit cb05374b18

View file

@ -44,7 +44,6 @@ func calculateDownloadSpeed(downloaded int64, startTime, endTime time.Time) int6
func downloadFile(ctx context.Context, progressReporter ProgressReporter, client *http.Client, downloadURL, dstPath string, doRetries bool, buffer []byte) error {
filePath := filepath.Base(dstPath)
var speed int64
var startTime time.Time
for attempt := 1; attempt <= maxRetries; attempt++ {
@ -100,9 +99,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
}
downloaded += int64(n)
endTime := time.Now()
speed = calculateDownloadSpeed(downloaded, startTime, endTime)
progressReporter.UpdateDownloadProgress(downloaded, speed, filePath)
progressReporter.UpdateDownloadProgress(downloaded, calculateDownloadSpeed(downloaded, startTime, time.Now()), filePath)
}
break
}