Beispiel #1
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);
            }
        }