コード例 #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);
        }
コード例 #2
0
        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();
        }
コード例 #3
0
        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();
            }
        }
コード例 #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();
                }
            }
        }
コード例 #5
0
        /// <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();
            }
        }
コード例 #6
0
 protected StreamQuery CreateDownloadQuery(RangeRequest rangeRequest)
 {
     return(CreateDownloadStreamQuery(Config.RangeRequest));
 }
コード例 #7
0
 /// <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);
コード例 #8
0
 protected abstract Task InternalDownloadAsync(Stream stream, RangeRequest rangeRequest, CancellationToken cancellationToken);