Example #1
0
        /// <summary>
        /// Gets a file out of the download cache. This does not download the
        /// file if it is not in the cache; use "DownloadAndCache" before
        /// using this method download the file and cache it.
        /// </summary>
        /// <param name="baseUri">
        /// URI to base relative URI's off of. If null, it defaults to the HTML
        /// page's URI.
        /// </param>
        /// <param name="relativeUri">URI relative to the base URI</param>
        /// <returns>A stream for the URI</returns>
        protected override Stream GetFileInternal(object baseUri, Uri relativeUri)
        {
            // check in the XAP first
            // TODO: Can this check happen further up?
            if (!relativeUri.IsAbsoluteUri)
            {
                var stream = XapPAL.PAL.VirtualFilesystem.GetFile(relativeUri);
                if (stream != null)
                {
                    return(stream);
                }
            }

            var fullUri = DynamicApplication.MakeUri((Uri)baseUri, relativeUri);

            if (_cache.Has(fullUri))
            {
                return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(_cache[fullUri])));
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Gets a file out of the download cache. This does not download the
        /// file if it is not in the cache; use "DownloadAndCache" before
        /// using this method download the file and cache it.
        /// </summary>
        /// <param name="baseUri">
        /// URI to base relative URI's off of. If null, it defaults to the HTML
        /// page's URI.
        /// </param>
        /// <param name="relativeUri">URI relative to the base URI</param>
        /// <returns>A stream for the URI</returns>
        protected override Stream GetFileInternal(object baseUri, Uri relativeUri)
        {
            if (!relativeUri.IsAbsoluteUri)
            {
                // TODO: HttpVirtualFilesystem shouldn't manage all these checks,
                // needs to be re-organized.

                // check in the XAP first
                var stream = XapPAL.PAL.VirtualFilesystem.GetFile(relativeUri);
                if (stream != null)
                {
                    return(stream);
                }

                // also check any ZIP files
                if (HtmlPage.IsEnabled && (DynamicApplication.Current != null && DynamicApplication.Current.ScriptTags != null))
                {
                    foreach (var zip in DynamicApplication.Current.ScriptTags.ZipPackages)
                    {
                        var relUriStr = relativeUri.ToString();
                        var dirname   = Path.GetDirectoryName(relUriStr);
                        if (dirname.Length == 0)
                        {
                            continue;
                        }

                        var dirs = dirname.Split('/');
                        if (dirs.Length == 0)
                        {
                            continue;
                        }

                        var toplevelDir = dirs[0];
                        if (toplevelDir != Path.GetFileNameWithoutExtension(zip.ToString()))
                        {
                            continue;
                        }

                        var rest = relUriStr.Split(new string[] { toplevelDir + "/" }, StringSplitOptions.None);
                        if (rest.Length <= 1)
                        {
                            continue;
                        }

                        var pathToFileInZip = rest[1];
                        var file            = XapPAL.PAL.VirtualFilesystem.GetFile(
                            new StreamResourceInfo(GetFileInternal(baseUri, zip), null), pathToFileInZip
                            );

                        if (file != null)
                        {
                            return(file);
                        }
                    }
                }
            }

            var fullUri = DynamicApplication.MakeUri((Uri)baseUri, relativeUri);

            if (_cache.Has(fullUri))
            {
                byte[] bytes      = _cache.GetBinary(fullUri);
                string strContent = _cache.Get(fullUri);
                return(new MemoryStream(
                           strContent != null ?
                           System.Text.Encoding.UTF8.GetBytes(strContent) :
                           bytes
                           ));
            }
            else
            {
                return(null);
            }
        }