コード例 #1
0
        /// <summary>
        /// Return stream for requested  asset file  (used for search current and base themes assets)
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="searchInGlobalThemeOnly"></param>
        /// <returns></returns>
        public Stream GetAssetStream(string filePath, bool searchInGlobalThemeOnly = false)
        {
            Stream retVal = null;
            var    filePathWithoutExtension = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)).Replace("\\", "/");
            //file.ext => file.ext || file || file.liquid || file.ext.liquid
            var searchPatterns = new[] { filePath, filePathWithoutExtension, string.Format(_liquidTemplateFormat, filePathWithoutExtension), string.Format(_liquidTemplateFormat, filePath) };

            string currentThemeFilePath = null;
            //search in global theme first
            var globalThemeFilePath = searchPatterns.SelectMany(x => _globalThemeBlobProvider.Search("assets", x, true)).FirstOrDefault();

            if (!searchInGlobalThemeOnly)
            {
                //try to search in current store theme
                if (_themeBlobProvider.PathExists(CurrentThemePath + "\\assets"))
                {
                    currentThemeFilePath = searchPatterns.SelectMany(x => _themeBlobProvider.Search(CurrentThemePath + "\\assets", x, true)).FirstOrDefault();
                }
            }

            if (currentThemeFilePath != null)
            {
                retVal   = _themeBlobProvider.OpenRead(currentThemeFilePath);
                filePath = currentThemeFilePath;
            }
            else if (globalThemeFilePath != null)
            {
                retVal   = _globalThemeBlobProvider.OpenRead(globalThemeFilePath);
                filePath = globalThemeFilePath;
            }

            if (retVal != null && filePath.EndsWith(".liquid"))
            {
                var shopifyContext = WorkContext.ToShopifyModel(UrlBuilder);
                var parameters     = shopifyContext.ToLiquid() as Dictionary <string, object>;
                var settings       = GetSettings("''");
                parameters.Add("settings", settings);

                var templateContent = retVal.ReadToString();
                retVal.Dispose();

                var template = RenderTemplate(templateContent, parameters);
                retVal = new MemoryStream(Encoding.UTF8.GetBytes(template));
            }

            if (retVal != null && (filePath.Contains(".scss.") || filePath.EndsWith(".scss")))
            {
                var content = retVal.ReadToString();
                retVal.Dispose();

                try
                {
                    //handle scss resources
                    content = _saasCompiler.Compile(content);
                    retVal  = new MemoryStream(Encoding.UTF8.GetBytes(content));
                }
                catch (Exception ex)
                {
                    throw new SaasCompileException(filePath, content, ex);
                }
            }

            return(retVal);
        }
コード例 #2
0
        /// <summary>
        /// Return stream for requested  asset file  (used for search current and base themes assets)
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Stream GetAssetStream(string fileName, bool searchInGlobalThemeOnly = false)
        {
            Stream retVal           = null;
            var    fileExtensions   = System.IO.Path.GetExtension(fileName);
            string currentThemePath = null;
            string globalThemePath  = _globalThemeBlobProvider.Search("assets", fileName, true).FirstOrDefault();

            if (!searchInGlobalThemeOnly)
            {
                //try to search in current store theme
                if (_themeBlobProvider.PathExists(CurrentThemePath + "\\assets"))
                {
                    currentThemePath = _themeBlobProvider.Search(CurrentThemePath + "\\assets", fileName, true).FirstOrDefault();
                }
            }

            //We find requested asset need return resulting stream
            if (currentThemePath != null)
            {
                retVal = _themeBlobProvider.OpenRead(currentThemePath);
            }
            else if (globalThemePath != null)
            {
                retVal = _globalThemeBlobProvider.OpenRead(globalThemePath);
            }
            else
            {
                //Otherwise it may be liquid template
                fileName = fileName.Replace(".scss.css", ".scss");
                var settings = GetSettings("''");
                //Try to parse liquid asset resource
                var themeAssetPath = ResolveTemplatePath(fileName, searchInGlobalThemeOnly);
                if (themeAssetPath != null)
                {
                    var templateContent = ReadTemplateByPath(themeAssetPath);
                    var content         = RenderTemplate(templateContent, new Dictionary <string, object>()
                    {
                        { "settings", settings }
                    });

                    if (fileName.EndsWith(".scss"))
                    {
                        try
                        {
                            //handle scss resources
                            content = _saasCompiler.Compile(content);
                        }
                        catch (Exception ex)
                        {
                            throw new SaasCompileException(fileName, content, ex);
                        }
                    }
                    if (content != null)
                    {
                        retVal = new MemoryStream(Encoding.UTF8.GetBytes(content));
                    }
                }
            }

            return(retVal);
        }