Пример #1
0
        public override IList <ResourceRequiredContext> BuildRequiredResources(string stringResourceType)
        {
            // It's necessary to make a copy since making a change to the local variable also changes the private one.
            var resources = new List <ResourceRequiredContext>(base.BuildRequiredResources(stringResourceType));

            var settingsPart = _cacheManager.Get("Piedone.Combinator.CombinatorSettingsPart", ctx =>
            {
                _combinatorEventMonitor.MonitorConfigurationChanged(ctx);

                return(_siteService.GetSiteSettings().As <CombinatorSettingsPart>());
            });

            if (resources.Count == 0 ||
                Orchard.UI.Admin.AdminFilter.IsApplied(_httpContextAccessor.Current().Request.RequestContext) && !settingsPart.EnableForAdmin)
            {
                return(resources);
            }

            var resourceType = ResourceTypeHelper.StringTypeToEnum(stringResourceType);

            try
            {
                var settings = new CombinatorSettings
                {
                    CombineCDNResources     = settingsPart.CombineCDNResources,
                    EmbedCssImages          = settingsPart.EmbedCssImages,
                    EmbeddedImagesMaxSizeKB = settingsPart.EmbeddedImagesMaxSizeKB,
                    MinifyResources         = settingsPart.MinifyResources
                };

                if (!String.IsNullOrEmpty(settingsPart.CombinationExcludeRegex))
                {
                    settings.CombinationExcludeFilter = new Regex(settingsPart.CombinationExcludeRegex);
                }
                if (!String.IsNullOrEmpty(settingsPart.EmbedCssImagesStylesheetExcludeRegex))
                {
                    settings.EmbedCssImagesStylesheetExcludeFilter = new Regex(settingsPart.EmbedCssImagesStylesheetExcludeRegex);
                }
                if (!String.IsNullOrEmpty(settingsPart.MinificationExcludeRegex))
                {
                    settings.MinificationExcludeFilter = new Regex(settingsPart.MinificationExcludeRegex);
                }

                if (!String.IsNullOrEmpty(settingsPart.ResourceSetRegexes))
                {
                    var setRegexes = new List <Regex>();
                    foreach (var regex in settingsPart.ResourceSetRegexes.Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (!String.IsNullOrEmpty(regex))
                        {
                            setRegexes.Add(new Regex(regex));
                        }
                    }
                    settings.ResourceSetFilters = setRegexes.ToArray();
                }

                if (resourceType == ResourceType.Style)
                {
                    // Checking for overridden stylesheets
                    var currentTheme = _themeManager.GetRequestTheme(_httpContextAccessor.Current().Request.RequestContext);
                    var shapeTable   = _shapeTableLocator.Lookup(currentTheme.Id);

                    foreach (var resource in resources)
                    {
                        var shapeName = StylesheetBindingStrategy.GetAlternateShapeNameFromFileName(resource.Resource.GetFullPath());

                        // Simply included CDN stylesheets are not in the ShapeTable, so we have to check
                        if (shapeTable.Bindings.ContainsKey("Style__" + shapeName))
                        {
                            var binding = shapeTable.Bindings["Style__" + shapeName].BindingSource;
                            resource.Resource.SetUrl(binding, null);
                        }
                    }

                    return(_combinatorService.CombineStylesheets(resources, settings));
                }
                else if (resourceType == ResourceType.JavaScript)
                {
                    return(_combinatorService.CombineScripts(resources, settings));
                }

                return(base.BuildRequiredResources(stringResourceType));
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }
                Logger.Error(ex, "Error when combining " + resourceType + " files");
                return(base.BuildRequiredResources(stringResourceType));
            }
        }
        public override IList <ResourceRequiredContext> BuildRequiredResources(string stringResourceType)
        {
            // It's necessary to make a copy since making a change to the local variable also changes the private one.
            var resources = new List <ResourceRequiredContext>(base.BuildRequiredResources(stringResourceType));

            var settingsPart = _siteService.GetSiteSettings().As <CombinatorSettingsPart>();

            if (resources.Count == 0 ||
                Orchard.UI.Admin.AdminFilter.IsApplied(_httpContextAccessor.Current().Request.RequestContext) && !settingsPart.EnableForAdmin)
            {
                return(resources);
            }

            var resourceType = ResourceTypeHelper.StringTypeToEnum(stringResourceType);

            try
            {
                Uri resourceBaseUri = null;
                if (!string.IsNullOrEmpty(settingsPart.ResourceBaseUrl))
                {
                    resourceBaseUri = UriHelper.CreateUri(settingsPart.ResourceBaseUrl);
                }

                var settings = new CombinatorSettings
                {
                    CombineCdnResources     = settingsPart.CombineCdnResources,
                    ResourceBaseUri         = resourceBaseUri,
                    EmbedCssImages          = settingsPart.EmbedCssImages,
                    EmbeddedImagesMaxSizeKB = settingsPart.EmbeddedImagesMaxSizeKB,
                    GenerateImageSprites    = settingsPart.GenerateImageSprites,
                    MinifyResources         = settingsPart.MinifyResources,
                    EnableResourceSharing   = settingsPart.EnableResourceSharing
                };

                if (!string.IsNullOrEmpty(settingsPart.CombinationExcludeRegex))
                {
                    settings.CombinationExcludeFilter = new Regex(settingsPart.CombinationExcludeRegex);
                }
                if (!string.IsNullOrEmpty(settingsPart.RemoteStorageUrlRegex))
                {
                    settings.RemoteStorageUrlPattern = new Regex(settingsPart.RemoteStorageUrlRegex);
                }
                if (!string.IsNullOrEmpty(settingsPart.EmbedCssImagesStylesheetExcludeRegex))
                {
                    settings.EmbedCssImagesStylesheetExcludeFilter = new Regex(settingsPart.EmbedCssImagesStylesheetExcludeRegex);
                }
                if (!string.IsNullOrEmpty(settingsPart.MinificationExcludeRegex))
                {
                    settings.MinificationExcludeFilter = new Regex(settingsPart.MinificationExcludeRegex);
                }
                if (!string.IsNullOrEmpty(settingsPart.ResourceSharingExcludeRegex))
                {
                    settings.ResourceSharingExcludeFilter = new Regex(settingsPart.ResourceSharingExcludeRegex);
                }

                if (!string.IsNullOrEmpty(settingsPart.ResourceSetRegexes))
                {
                    var setRegexes = new List <Regex>();
                    foreach (var regex in settingsPart.ResourceSetRegexesEnumerable)
                    {
                        if (!string.IsNullOrEmpty(regex))
                        {
                            setRegexes.Add(new Regex(regex));
                        }
                    }
                    settings.ResourceSetFilters = setRegexes.ToArray();
                }

                DiscoverResourceOverrides(resources, resourceType);

                IList <ResourceRequiredContext> result;

                if (resourceType == ResourceType.Style)
                {
                    result = _combinatorService.CombineStylesheets(resources, settings);
                }
                else if (resourceType == ResourceType.JavaScript)
                {
                    result = _combinatorService.CombineScripts(resources, settings);
                }
                else
                {
                    return(base.BuildRequiredResources(stringResourceType));
                }

                RemoveOriginalResourceShapes(result, resourceType);

                return(result);
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }
                Logger.Error(ex, "Error when combining " + resourceType + " files");
                return(base.BuildRequiredResources(stringResourceType));
            }
        }