Пример #1
0
        public virtual void ResetError()
        {
            manager = null;
#if DESKTOP
            prefetchingTask = null;
            var z     = new LazyUri(Url);
            var cache = Caching.GetFileCachePath(z);
            if (cache != null)
            {
                Caching.DeleteFileCacheAsync(z).FireAndForget();
            }
#endif
            OnChanged();
        }
Пример #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();
            }
        }
Пример #3
0
        internal async Task <Stream> OpenStreamAsync(bool synchronous, bool skipCache = false, bool linger = true)
        {
            var url = new LazyUri(Url);
            await Utils.CheckLocalFileAccessAsync(url);

            var mgr = this.manager;
            Func <long, Task <HttpResponseMessage> > createStream = null;

#if !STANDALONE && DESKTOP
            if (Caching.AzureApi != null && (mgr == null || !mgr.IsAlive) && !skipCache)
            {
                var container          = Caching.GetAzureContainer(url);
                HashSet <string> files = null;
                if (synchronous)
                {
                    ObjectManager.SynchronizationContext.Send(async() =>
                    {
                        files = await Caching.GetAzureCachedFiles(container);
                    });
                }
                else
                {
                    await ObjectManager.SynchronizationContext.SendAsync(async() =>
                    {
                        files = await Caching.GetAzureCachedFiles(container);
                    });
                }
                var name = Caching.GetFileCachePath(url);
                if (files.Contains(name))
                {
                    createStream = offset => Caching.GetAzureResponseAsync(container, name, offset, this);
                }
            }
            else
#endif
            if (
#if !STANDALONE && DESKTOP
                Caching.AzureApi == null &&
#endif
                !skipCache)
            {
#if DESKTOP
                var cache = Caching.GetFileCachePath(url);

                if (File.Exists(cache))
                {
                    var str = new FileStream(cache, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
                    if (str.Length == 0)
                    {
                        var errfile = Path.ChangeExtension(cache, ".err");
                        if (File.Exists(errfile))
                        {
                            str.Dispose();
                            var errlines = File.ReadAllText(errfile);
                            return(new MediaStream(MediaStream.ExceptionFromCachedResponse(errlines), this));
                        }
                    }
                    Sanity.AssertFastReadByte(str);
                    return(str);
                }
#endif
            }

            lock (this)
            {
                if (manager == null)
                {
                    manager = new MediaStreamManager(createStream ?? GetResponseAsync, true);
                }

                var stream = manager.TryCreateStream(this, 0, linger);
                if (stream == null)
                {
                    manager = new MediaStreamManager(createStream ?? GetResponseAsync, true);
                    stream  = manager.TryCreateStream(this, 0, linger);
                    Sanity.Assert(stream != null);
                }
                Sanity.AssertFastReadByte(stream);
                return(stream);
            }
        }
Пример #4
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();
            }
        }