private async Task <Stream> StartDownload() { try { Package.TotalFileSize = await _requestInstance.GetFileSize().ConfigureAwait(false); OnDownloadStarted(new DownloadStartedEventArgs(Package.FileName, Package.TotalFileSize)); ValidateBeforeChunking(); Package.Chunks ??= _chunkHub.ChunkFile(Package.TotalFileSize, Options.ChunkCount); Package.Validate(); if (Options.ParallelDownload) { await ParallelDownload(_globalCancellationTokenSource.Token).ConfigureAwait(false); } else { await SerialDownload(_globalCancellationTokenSource.Token).ConfigureAwait(false); } await StoreDownloadedFile(_globalCancellationTokenSource.Token).ConfigureAwait(false); } catch (OperationCanceledException exp) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(exp, true, Package)); } catch (Exception exp) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(exp, false, Package)); Debugger.Break(); } finally { if (IsCancelled) { // flush streams Package.Flush(); } else { // remove temp files Package.Clear(); } await Task.Yield(); } return(_destinationStream); }
private async Task <Stream> StartDownload() { try { Package.TotalFileSize = await _requestInstance.GetFileSize(); Validate(); Package.Chunks = _chunkHub.ChunkFile(Package.TotalFileSize, Package.Options.ChunkCount); OnDownloadStarted(new DownloadStartedEventArgs(Package.FileName, Package.TotalFileSize)); if (Package.Options.ParallelDownload) { await ParallelDownload(_globalCancellationTokenSource.Token); } else { await SerialDownload(_globalCancellationTokenSource.Token); } await StoreDownloadedFile(_globalCancellationTokenSource.Token).ConfigureAwait(false); } catch (OperationCanceledException exp) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(exp, true, Package)); } catch (Exception exp) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(exp, false, Package)); throw; } finally { if (IsCancelled == false) { // remove temp files ClearChunks(); } } return(Package.DestinationStream); }
private async Task StartDownload() { try { Package.TotalFileSize = await _requestInstance.GetFileSize(); Validate(); if (File.Exists(Package.FileName)) { File.Delete(Package.FileName); } Package.Chunks = _chunkHub.ChunkFile(Package.TotalFileSize, Package.Options.ChunkCount); OnDownloadStarted(new DownloadStartedEventArgs(Package.FileName, Package.TotalFileSize)); CancellationToken cancellationToken = _globalCancellationTokenSource.Token; List <Task> tasks = new List <Task>(); foreach (Chunk chunk in Package.Chunks) { if (Package.Options.ParallelDownload) { Task <Chunk> task = DownloadChunk(chunk, cancellationToken); tasks.Add(task); } else { await DownloadChunk(chunk, cancellationToken); } } if (Package.Options.ParallelDownload && cancellationToken.IsCancellationRequested == false) { Task.WaitAll(tasks.ToArray(), cancellationToken); } if (cancellationToken.IsCancellationRequested) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(null, true, Package)); return; } // Merge data to single file await _chunkHub.MergeChunks(Package.Chunks, Package.FileName); OnDownloadFileCompleted(new AsyncCompletedEventArgs(null, false, Package)); } catch (OperationCanceledException exp) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(exp, true, Package)); } catch (Exception exp) { OnDownloadFileCompleted(new AsyncCompletedEventArgs(exp, false, Package)); throw; } finally { if (_globalCancellationTokenSource.Token.IsCancellationRequested == false) { // remove temp files ClearChunks(); } } }