Exemple #1
0
        protected StreamQuery CreateDownloadStreamQuery(RangeRequest rangeRequest)
        {
            rangeRequest = rangeRequest ?? Config?.RangeRequest;

            var downloadQuery = DownloadSpecification == null
                ? new StreamQuery(Client).Uri(Item.GetObjectUri()).Action("Download")
                : new StreamQuery(Client).Uri(DownloadSpecification.DownloadUrl);

            downloadQuery.AddHeader("Accept", "*/*");

            if (rangeRequest != null)
            {
                if (!rangeRequest.End.HasValue)
                {
                    downloadQuery.AddHeader("Range", $"bytes={rangeRequest.Begin.GetValueOrDefault()}-");
                }
                else
                {
                    downloadQuery.AddHeader("Range",
                                            $"bytes={rangeRequest.Begin.GetValueOrDefault()}-{rangeRequest.End.GetValueOrDefault()}");
                }
            }

            return(downloadQuery as StreamQuery);
        }
        public override void DownloadTo(Stream outputStream,
                                        Dictionary <string, object> transferMetadata = null,
                                        CancellationToken cancellationToken          = default(CancellationToken),
                                        RangeRequest rangeRequest = null)
        {
            if (rangeRequest != null && DownloadSpecification == null)
            {
                throw new InvalidOperationException("Downloader instance has not been prepared. In order to supply a RangeRequest instance, you must first call PrepareDownload.");
            }

            if (DownloadSpecification == null)
            {
                PrepareDownload(cancellationToken);
            }
            else if (Item != null && !SupportsDownloadSpecification(Item.GetObjectUri()))
            {
                throw new NotSupportedException("Provider does not support download with DownloadSpecification)");
            }

            rangeRequest = rangeRequest ?? Config.RangeRequest;

            var downloadQuery = CreateDownloadStreamQuery(rangeRequest);
            CancellationTokenSource downloadCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            Stream downloadStream = null;

            try
            {
                progressReporter.StartReporting(transferMetadata, downloadCancellationSource.Token);
                downloadStream = downloadQuery.Execute();
                downloadStream = ExpectedLengthStream(downloadStream);

                int bytesRead;
                var buffer = new byte[Configuration.BufferSize];
                do
                {
                    TryPause();
                    if (downloadCancellationSource.Token.IsCancellationRequested)
                    {
                        throw new TaskCanceledException();
                    }

                    bytesRead = downloadStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead > 0)
                    {
                        outputStream.Write(buffer, 0, bytesRead);
                        progressReporter.ReportProgress(bytesRead);
                    }
                } while (bytesRead > 0);
            }
            finally
            {
                downloadCancellationSource.Cancel();
                downloadCancellationSource.Dispose();
                downloadStream?.Dispose();
            }
            progressReporter.ReportCompletion();
        }
        protected override async Task InternalDownloadAsync(Stream outputStream, RangeRequest rangeRequest, CancellationToken cancellationToken)
        {
            var    streamQuery    = CreateDownloadStreamQuery(rangeRequest);
            Stream downloadStream = null;

            try
            {
                downloadStream = await streamQuery.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                downloadStream = ExpectedLengthStream(downloadStream);
                await ReadAllBytesAsync(outputStream, downloadStream, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                downloadStream?.Dispose();
            }
        }
Exemple #4
0
        protected override async Task InternalDownloadAsync(Stream stream, RangeRequest rangeRequest, CancellationToken cancellationToken)
        {
            if (!(stream is FileStream))
            {
                throw new ArgumentException($"Stream argument to {nameof(AsyncMemoryMappedFileDownloader)} must be a {nameof(FileStream)}");
            }

            using (var inputStream = await DownloadStreamAsync(cancellationToken).ConfigureAwait(false))
            {
                memoryMappedFile = CreateMemoryMappedFile((FileStream)stream);
                using (var outputStream = memoryMappedFile.CreateViewStream(0, fileSize))
                {
                    var readTask        = inputStream.CopyToAsync(new ProgressStream(outputStream, OnProgress));
                    var cancelCheckTask = CancellationCheckerAsync(cancellationToken, TimeSpan.FromSeconds(1));
                    await Task.WhenAny(readTask, cancelCheckTask).ConfigureAwait(false);
                }
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new TaskCanceledException();
                }
            }
        }
        /// <summary>
        /// Downloads the file to the provided Stream.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="transferMetadata"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="rangeRequest">
        ///     Overrides Config.RangeRequest. ShareFile may have some restrictions on the number of times a range request can be issued for a given download session.
        ///     There is a chance that providing this can result in failures.
        /// </param>
        public virtual async Task DownloadToAsync(
            Stream stream,
            CancellationToken cancellationToken          = default(CancellationToken),
            Dictionary <string, object> transferMetadata = null,
            RangeRequest rangeRequest = null)
        {
            if (rangeRequest != null && DownloadSpecification == null)
            {
                throw new InvalidOperationException("Downloader instance has not been prepared. In order to supply a RangeRequest, you must first call PrepareDownloadAsync.");
            }

            if (DownloadSpecification == null)
            {
                await PrepareDownloadAsync(cancellationToken);
            }
            else if (Item != null && !await SupportsDownloadSpecificationAsync(Item.GetObjectUri()))
            {
                throw new NotSupportedException("Provider does not support download with DownloadSpecification)");
            }

            rangeRequest = rangeRequest ?? Config.RangeRequest;

            CancellationTokenSource downloadCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);

            try
            {
                progressReporter.StartReporting(transferMetadata, downloadCancellationSource.Token);
                await InternalDownloadAsync(stream, rangeRequest, cancellationToken);

                progressReporter.ReportCompletion();
            }
            finally
            {
                downloadCancellationSource.Cancel();
                downloadCancellationSource.Dispose();
            }
        }
Exemple #6
0
 protected StreamQuery CreateDownloadQuery(RangeRequest rangeRequest)
 {
     return(CreateDownloadStreamQuery(Config.RangeRequest));
 }
 /// <summary>
 /// Downloads the file to the provided Stream.
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="transferMetadata"></param>
 /// <param name="cancellationToken"></param>
 /// <param name="rangeRequest">
 ///     Overrides Config.RangeRequest. ShareFile may have some restrictions on the number of times a range request can be issued for a given download session.
 ///     There is a chance that providing this can result in failures.
 /// </param>
 public abstract void DownloadTo(Stream stream,
                                 Dictionary <string, object> transferMetadata = null,
                                 CancellationToken cancellationToken          = default(CancellationToken),
                                 RangeRequest rangeRequest = null);
 protected abstract Task InternalDownloadAsync(Stream stream, RangeRequest rangeRequest, CancellationToken cancellationToken);