private async Task <byte[]> DownloadWithProgress(HttpResponseMessage response, IProgressBar trackProgress, string title) { // thanks stackoverflow using (Stream fileStream = await response.Content.ReadAsStreamAsync()) { // ReSharper disable once PossibleInvalidOperationException long total = response.Content.Headers.ContentLength.Value; double totalMegabytes = ByteSize.FromBytes(total).MegaBytes; totalMegabytes = Math.Round(totalMegabytes, 2); var finalBytes = new byte[total]; var totalRead = 0L; var buffer = new byte[4096]; var isMoreToRead = true; do { int read = await fileStream.ReadAsync(buffer, 0, buffer.Length); if (read == 0) { trackProgress.Next($"{title} | Download Complete"); isMoreToRead = false; } else { var data = new byte[read]; buffer.ToList().CopyTo(0, data, 0, read); data.CopyTo(finalBytes, totalRead); totalRead += read; double percent = totalRead * 1d / (total * 1d) * 100; double totalReadMegabytes = ByteSize.FromBytes(totalRead).MegaBytes; totalReadMegabytes = Math.Round(totalReadMegabytes, 2); trackProgress.Refresh(Convert.ToInt32(percent), $"{title} | {totalReadMegabytes}MB/{totalMegabytes}MB"); } } while (isMoreToRead); return(finalBytes); } }
public void Refresh(int current, string format, params object[] args) { _bar.Refresh(current, format, args); }