コード例 #1
0
        /// <summary>
        /// Load all themes
        /// </summary>
        /// <param name="loadOnlyInstalledModules"></param>
        /// <returns></returns>
        public virtual IList <ThemeInfo> LoadThemes()
        {
            if (_themes == null)
            {
                using (new WriteLockDisposable(_locker))
                {
                    //load all theme descriptors
                    _fileProvider.CreateDirectory(_fileProvider.MapPath(ThemesPath));
                    var themeFolder = new DirectoryInfo(_fileProvider.MapPath(ThemesPath));
                    _themes = new List <ThemeInfo>();
                    foreach (var themeInfoFile in themeFolder.GetFiles(ThemeInfoFileName, SearchOption.AllDirectories))
                    {
                        var text = File.ReadAllText(themeInfoFile.FullName);
                        if (string.IsNullOrEmpty(text))
                        {
                            continue;
                        }

                        //deserialize module information file to ModuleInfo object
                        var themeInfo = JsonConvert.DeserializeObject <ThemeInfo>(File.ReadAllText(themeInfoFile.FullName));

                        //validation
                        if (string.IsNullOrEmpty(themeInfo?.ThemeName))
                        {
                            throw new Exception($"A theme info '{themeInfoFile.FullName}' has no system name");
                        }

                        _themes.Add(themeInfo);
                    }
                }
            }

            return(_themes);
        }
コード例 #2
0
        public virtual void Log(LogLevel logLevel,
                                string shortMessage,
                                string fullMessage = null,
                                string ipAddress   = null,
                                string pageUrl     = null,
                                string referrerUrl = null)
        {
            if (string.IsNullOrEmpty(shortMessage))
            {
                return;
            }

            string logMessage = string.Format("{0} [{1}] {2}",
                                              DateTime.UtcNow.ToString("dd.MM.yyyy HH:mm:ss.fff zzz"),
                                              logLevel.ToString(),
                                              shortMessage);

            _fileProvider.CreateDirectory(_fileProvider.MapContentPath("logs"));
            using (StreamWriter sw = File.AppendText(_fileProvider.MapContentPath(string.Format("logs/{0:dd.MM.yyyy}.txt", DateTime.UtcNow))))
            {
                sw.WriteLine(logMessage);
            }
        }
コード例 #3
0
ファイル: HeadBuilder.cs プロジェクト: dovanduy/microCommerce
        /// <summary>
        /// Generate all script parts
        /// </summary>
        /// <param name="urlHelper">URL Helper</param>
        /// <param name="location">A location of the script element</param>
        /// <param name="bundleFiles">A value indicating whether to bundle script elements</param>
        /// <returns>Generated string</returns>
        public virtual string RenderScripts(IUrlHelper urlHelper, ResourceLocation location)
        {
            if (!_scriptParts.ContainsKey(location) || _scriptParts[location] == null)
            {
                return(string.Empty);
            }

            if (!_scriptParts.Any())
            {
                return(string.Empty);
            }

            if (_seoSettings.EnableJsBundling)
            {
                var partsToBundle = _scriptParts[location]
                                    .Where(x => !x.ExcludeFromBundle)
                                    .Distinct()
                                    .ToArray();

                var partsToDontBundle = _scriptParts[location]
                                        .Where(x => x.ExcludeFromBundle)
                                        .Distinct()
                                        .ToArray();

                var result = new StringBuilder();

                //parts to  bundle
                if (partsToBundle.Any())
                {
                    //ensure \bundles directory exists
                    _fileProvider.CreateDirectory(_fileProvider.GetAbsolutePath(BundleFolder));

                    var bundle = new Bundle();
                    foreach (var item in partsToBundle)
                    {
                        string src = $"wwwroot{urlHelper.Content(item.Path)}";
                        bundle.InputFiles.Add(src);
                    }

                    //output file
                    var outputFileName = GetBundleFileName(partsToBundle.Select(x => x.Path).ToArray());
                    bundle.OutputFileName = $"wwwroot/{BundleFolder}/{outputFileName}.js";

                    //save
                    var configFilePath = $"{_hostingEnvironment.ContentRootPath}\\{outputFileName}.json";
                    bundle.FileName = configFilePath;
                    lock (s_lock)
                    {
                        var cacheKey      = string.Format(ScriptsCacheKey, outputFileName);
                        var shouldRebuild = _cacheManager.Get(cacheKey, RecheckBundledFilesPeriod, () => true);
                        if (shouldRebuild)
                        {
                            //process
                            _processor.Process(configFilePath, new List <Bundle> {
                                bundle
                            });
                            _cacheManager.Set(cacheKey, false, RecheckBundledFilesPeriod);
                        }
                    }

                    //render
                    result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content($"~/{BundleFolder}/" + outputFileName + ".min.js"));
                    result.Append(Environment.NewLine);
                }

                //parts to not bundle
                foreach (var item in partsToDontBundle)
                {
                    result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content(item.Path));
                    result.Append(Environment.NewLine);
                }

                return(result.ToString());
            }
            else
            {
                //bundling is disabled
                var result = new StringBuilder();
                foreach (var item in _scriptParts[location].Distinct())
                {
                    result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content(item.Path));
                    result.Append(Environment.NewLine);
                }

                return(result.ToString());
            }
        }
コード例 #4
0
        /// <summary>
        /// Generate all script parts
        /// </summary>
        /// <param name="urlHelper">URL Helper</param>
        /// <param name="location">A location of the script element</param>
        /// <param name="bundleFiles">A value indicating whether to bundle script elements</param>
        /// <returns>Generated string</returns>
        public virtual string RenderScripts(IUrlHelper urlHelper, ResourceLocation location)
        {
            if (!_scriptParts.ContainsKey(location) || _scriptParts[location] == null)
            {
                return("");
            }

            if (!_scriptParts.Any())
            {
                return("");
            }

            if (_seoSettings.EnableJsBundling)
            {
                var partsToBundle = _scriptParts[location]
                                    .Where(x => !x.ExcludeFromBundle)
                                    .Distinct()
                                    .ToArray();

                var partsToDontBundle = _scriptParts[location]
                                        .Where(x => x.ExcludeFromBundle)
                                        .Distinct()
                                        .ToArray();

                var result = new StringBuilder();

                //parts to  bundle
                if (partsToBundle.Any())
                {
                    //ensure \bundles directory exists
                    _fileProvider.CreateDirectory(_fileProvider.GetAbsolutePath("bundles"));

                    var bundle = new Bundle();
                    foreach (var item in partsToBundle)
                    {
                        new PathString(urlHelper.Content(item.Src))
                        .StartsWithSegments(urlHelper.ActionContext.HttpContext.Request.PathBase, out PathString path);
                        var src = path.Value.TrimStart('/');

                        //check whether this file exists, if not it should be stored into /wwwroot directory
                        if (!_fileProvider.FileExists(_fileProvider.MapPath(path)))
                        {
                            src = $"wwwroot/{src}";
                        }

                        bundle.InputFiles.Add(src);
                    }
                    //output file
                    var outputFileName = GetBundleFileName(partsToBundle.Select(x => x.Src).ToArray());
                    bundle.OutputFileName = "wwwroot/bundles/" + outputFileName + ".js";
                    //save
                    var configFilePath = _hostingEnvironment.ContentRootPath + "\\" + outputFileName + ".json";
                    bundle.FileName = configFilePath;
                    lock (s_lock)
                    {
                        //performance optimization. do not bundle and minify for each HTTP request
                        //we periodically re-check already bundles file
                        //so if we have minification enabled, it could take up to several minutes to see changes in updated resource files (or just reset the cache or restart the site)
                        var cacheKey      = $"minification.shouldrebuild.scripts-{outputFileName}";
                        var shouldRebuild = _cacheManager.Get(cacheKey, RecheckBundledFilesPeriod, () => true);
                        if (shouldRebuild)
                        {
                            //process
                            _processor.Process(configFilePath, new List <Bundle> {
                                bundle
                            });
                            _cacheManager.Set(cacheKey, false, RecheckBundledFilesPeriod);
                        }
                    }

                    //render
                    result.AppendFormat("<script src=\"{0}\"></script>", urlHelper.Content("~/bundles/" + outputFileName + ".min.js"));
                    result.Append(Environment.NewLine);
                }

                //parts to not bundle
                foreach (var item in partsToDontBundle)
                {
                    result.AppendFormat("<script {1}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : "");
                    result.Append(Environment.NewLine);
                }

                return(result.ToString());
            }
            else
            {
                //bundling is disabled
                var result = new StringBuilder();
                foreach (var item in _scriptParts[location].Distinct())
                {
                    result.AppendFormat("<script {1}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : "");
                    result.Append(Environment.NewLine);
                }

                return(result.ToString());
            }
        }