Example #1
0
        public async Task DownloadAsync(string destination, CancellationToken ct, IProgress <DataTransferProgress> progress)
        {
            var temp = MaskedFile.GetMaskedPathFromFile(destination);

            try
            {
                using (var fs = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.Delete))
                    using (var stream = await OpenStreamAsync())
                    {
                        var mediaStream = stream as MediaStream;
                        if (progress != null)
                        {
                            progress.Report(new DataTransferProgress(default(FileSize), null, default(FileSize)));
                        }
                        var buffer           = new byte[4096];
                        var transferredBytes = 0L;
                        while (true)
                        {
                            var readBytes = await stream.ReadAsync(buffer, 0, buffer.Length);

                            transferredBytes += readBytes;
                            var t = new FileSize(transferredBytes);
                            if (progress != null && mediaStream != null)
                            {
                                progress.Report(new DataTransferProgress(t, mediaStream.Size, mediaStream.DataPerSecond));
                            }
                            if (readBytes == 0)
                            {
                                break;
                            }
                            await fs.WriteAsync(buffer, 0, readBytes);
                        }
#if !NET35
                        var m = stream as MediaStream;
                        if (m != null)
                        {
                            var response = await m.Manager.GetResponseAsync();

                            if (response != null)
                            {
                                var lm = response.Content.Headers.LastModified;
                                if (lm != null)
                                {
                                    fs.Dispose();
                                    File.SetLastWriteTimeUtc(temp, lm.Value.UtcDateTime);
                                }
                            }
                        }
#endif
                    }
                MaskedFile.PublishMaskedFile(temp, destination);
            }
            finally
            {
                File.Delete(temp);
            }
        }
Example #2
0
        internal void SaveResponseInfo(HttpResponseMessage partialDownload, bool continueDownload)
        {
            if (partialDownload != null)
            {
#if NET35
                var len = partialDownload.ContentLength == -1 ? null : (long?)partialDownload.ContentLength;
#else
                var len = partialDownload.Content.Headers.ContentLength;
#endif
                if (len != null)
                {
                    Size = new FileSize(len.Value);
                }

// HACK: ignore content disposition in .net 35
#if !NET35
                var contentDisposition = partialDownload.Content.Headers.ContentDisposition;
                if (contentDisposition != null)
                {
                    contentDispositionFileName = contentDisposition.FileName;
                }
#endif

                contentTypeExtension = HttpUtils.GetFileExtensionFromMime(
#if NET35
                    HttpUtils.GetMimeFromContentType(partialDownload.Headers["Content-Type"])
#else
                    partialDownload.Content.Headers.ContentType?.MediaType
#endif
                    );

                if (continueDownload && (manager == null || !manager.IsAlive))
                {
                    this.partialDownload = partialDownload;
                    manager = new MediaStreamManager(GetResponseAsync, true);
                }
                else
                {
                    partialDownload.AbortAndDispose();
                }
                OnChanged();
            }
        }
 public DataTransferProgress(FileSize transferredData, FileSize? total, FileSize dataPerSecond)
 {
     this.transferredData = transferredData;
     this.total = total;
     this.dataPerSecond = dataPerSecond;
 }
Example #4
0
 public async Task DownloadAsync(string destination, CancellationToken ct, IProgress<DataTransferProgress> progress)
 {
     var temp = MaskedFile.GetMaskedPathFromFile(destination);
     try
     {
         using (var fs = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.Delete))
         using (var stream = await OpenStreamAsync())
         {
             var mediaStream = stream as MediaStream;
             if (progress != null) progress.Report(new DataTransferProgress(default(FileSize), null, default(FileSize)));
             var buffer = new byte[4096];
             var transferredBytes = 0L;
             while (true)
             {
                 var readBytes = await stream.ReadAsync(buffer, 0, buffer.Length);
                 transferredBytes += readBytes;
                 var t = new FileSize(transferredBytes);
                 if (progress != null && mediaStream != null) progress.Report(new DataTransferProgress(t, mediaStream.Size, mediaStream.DataPerSecond));
                 if (readBytes == 0)
                 {
                     break;
                 }
                 await fs.WriteAsync(buffer, 0, readBytes);
             }
         }
         MaskedFile.PublishMaskedFile(temp, destination);
     }
     finally
     {
         File.Delete(temp);
     }
 }
Example #5
0
        internal void SaveResponseInfo(HttpResponseMessage partialDownload, bool continueDownload)
        {
            if (partialDownload != null)
            {
#if NET35
                var len = partialDownload.ContentLength == -1 ? null : (long?)partialDownload.ContentLength;
#else
                var len = partialDownload.Content.Headers.ContentLength;
#endif
                if (len != null) Size = new FileSize(len.Value);

// HACK: ignore content disposition in .net 35
#if !NET35
                var contentDisposition = partialDownload.Content.Headers.ContentDisposition;
                if (contentDisposition != null) contentDispositionFileName = contentDisposition.FileName;
#endif

                contentTypeExtension = HttpUtils.GetFileExtensionFromMime(
#if NET35
                    HttpUtils.GetMimeFromContentType(partialDownload.Headers["Content-Type"])
#else
                    partialDownload.Content.Headers.ContentType?.MediaType
#endif
                );

                if (continueDownload && (manager == null || !manager.IsAlive))
                {
                    this.partialDownload = partialDownload;
                    manager = new MediaStreamManager(GetResponseAsync, true);
                }
                else
                {
                    partialDownload.AbortAndDispose();
                }
                OnChanged();
            }
        }
Example #6
0
        internal void SaveResponseInfo(HttpResponseMessage partialDownload, bool?continueDownload)
        {
            if (partialDownload != null)
            {
#if NET35
                var len = partialDownload.ContentLength == -1 ? null : (long?)partialDownload.ContentLength;
#else
                var len = partialDownload.Content.Headers.ContentLength;
#endif
                if (len != null)
                {
                    Size = new FileSize(len.Value);
                }

#if !WEBCLIENT
                var lastModified = partialDownload.Content.Headers.LastModified?.UtcDateTime;
                if (partialDownload.Headers.TryGetValues("Memento-Datetime", out var k))
                {
                    if (DateTimeOffset.TryParse(k.First(), CultureInfo.InvariantCulture, DateTimeStyles.None, out var m))
                    {
                        if (lastModified == null || m.UtcDateTime < lastModified)
                        {
                            lastModified = m.UtcDateTime;
                        }
                    }
                }

                if (lastModified != null)
                {
                    LastModified = lastModified;
                }
#endif

                // HACK: ignore content disposition in .net 35
#if !NET35
                var contentDisposition = partialDownload.Content.Headers.ContentDisposition;
                if (contentDisposition != null)
                {
                    contentDispositionFileName = contentDisposition.FileName;
                }
#endif

                contentTypeExtension = HttpUtils.GetFileExtensionFromMime(
#if NET35
                    HttpUtils.GetMimeFromContentType(partialDownload.Headers["Content-Type"])
#else
                    partialDownload.Content.Headers.ContentType?.MediaType
#endif
                    );

                if (continueDownload != null)
                {
                    if (continueDownload == true && (manager == null || !manager.IsAlive))
                    {
                        this.partialDownload = partialDownload;
                        manager = new MediaStreamManager(GetResponseAsync, true);
                    }
                    else
                    {
                        partialDownload.AbortAndDispose();
                    }
                }
                OnChanged();
            }
        }