public static async Task<Stream> CopyStreamWithProgress( Stream source, Stream destination, IProgress<MediaFireOperationProgress> progress, CancellationToken token, MediaFireOperationProgress progressData, int bufferSize ) { int read, offset = 0; var buffer = new byte[bufferSize]; using (source) { do { read = await source.ReadAsync(buffer, 0, bufferSize, token); await destination.WriteAsync(buffer, 0, read, token); offset += read; progressData.CurrentSize = offset; progress.Report(progressData); } while (read != 0); } return destination; }
public async Task Download(string quickKey, Stream destination, CancellationToken token, IProgress<MediaFireOperationProgress> progress = null) { var links = await GetLinks(quickKey, MediaFireLinkType.DirectDownload); if (links.Count != 0 && string.IsNullOrEmpty(links[0].DirectDownload)) throw new MediaFireException(MediaFireErrorMessages.FileMustContainADirectLink); var fileLink = links[0].DirectDownload; if (progress == null) progress = new Progress<MediaFireOperationProgress>(); var cli = new HttpClient(); var resp = await cli.SendAsync(new HttpRequestMessage(HttpMethod.Get, fileLink), HttpCompletionOption.ResponseHeadersRead, token); resp.EnsureSuccessStatusCode(); var stream = await resp.Content.ReadAsStreamAsync(); var progressData = new MediaFireOperationProgress { TotalSize = resp.Content.Headers.ContentLength.Value }; using (destination) { await MediaFireHttpHelpers.CopyStreamWithProgress(stream, destination, progress, token, progressData, _agent.Configuration.ChunkTransferBufferSize); } }