Пример #1
0
        private async Task StoreDownloadedFile(CancellationToken cancellationToken)
        {
            _destinationStream = Package.FileName == null
                ? new MemoryStream()
                : FileHelper.CreateFile(Package.FileName);
            await _chunkHub.MergeChunks(Package.Chunks, _destinationStream, cancellationToken).ConfigureAwait(false);

            if (_destinationStream is FileStream)
            {
                _destinationStream?.Dispose();
            }

            OnDownloadFileCompleted(new AsyncCompletedEventArgs(null, false, Package));
        }
Пример #2
0
        private async Task StoreDownloadedFile(CancellationToken cancellationToken)
        {
            try
            {
                Package.DestinationStream = Package.FileName == null
                    ? new MemoryStream()
                    : FileHelper.CreateFile(Package.FileName);
                await _chunkHub.MergeChunks(Package.Chunks, Package.DestinationStream, cancellationToken).ConfigureAwait(false);

                OnDownloadFileCompleted(new AsyncCompletedEventArgs(null, false, Package));
            }
            finally
            {
                var isStoreOnMemory = Package?.FileName == null;
                if (isStoreOnMemory == false)
                {
                    Package.DestinationStream?.Dispose();
                }
            }
        }
Пример #3
0
        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();
                }
            }
        }
Пример #4
0
        private async Task CompleteDownload()
        {
            await _chunkHub.MergeChunks(Package.Chunks, Package.FileName).ConfigureAwait(false);

            OnDownloadFileCompleted(new AsyncCompletedEventArgs(null, false, Package));
        }