/// <summary> /// Minify supplied source according to settings. /// </summary> /// <param name="resource">Resource to minifz</param> /// <param name="minifier">Minifier to use when creating minified version of the data</param> /// <param name="minificationEnabled">True, if the data should be minified, otherwise false</param> /// <param name="compressionEnabled">True, if data should be compressed, otherwise false</param> private static void MinifyResource(CMSOutputResource resource, IResourceMinifier minifier, bool minificationEnabled, bool compressionEnabled) { if (resource == null) { return; } // Set up the settings if (minificationEnabled && (minifier != null)) { resource.MinifiedData = minifier.Minify(resource.Data); } // Compress if (RequestHelper.AllowResourceCompression && compressionEnabled) { resource.CompressedData = Compress(resource.Data); } // Compress and minify if (minificationEnabled && RequestHelper.AllowResourceCompression && compressionEnabled) { resource.MinifiedCompressedData = Compress(resource.MinifiedData); } }
public LongUrlCombinerService(ICacheService cacheService, IResourceMinifier minifier, ILoggingService logger) { _cacheService = cacheService; _minifier = minifier; _logger = logger; }
/// <summary> /// Minify supplied source according to settings. /// </summary> /// <param name="resource">Resource to minifz</param> /// <param name="minifier">Minifier to use when creating minified version of the data</param> /// <param name="minificationEnabled">True, if the data should be minified, otherwise false</param> /// <param name="compressionEnabled">True, if data should be compressed, otherwise false</param> private static void MinifyResource(CMSOutputResource resource, IResourceMinifier minifier, bool minificationEnabled, bool compressionEnabled) { if (resource == null) { return; } // Set up the settings if (minificationEnabled && (minifier != null)) { resource.MinifiedData = minifier.Minify(resource.Data); } // Check whether the compression is enabled if (compressionEnabled && CMSAppBase.ConnectionAvailable) { compressionEnabled &= RequestHelper.AllowResourceCompression; } // Compress if (compressionEnabled) { resource.CompressedData = Compress(resource.Data); } // Compress and minify if (minificationEnabled && compressionEnabled) { resource.MinifiedCompressedData = Compress(resource.MinifiedData); } }
/// <summary> /// Processes a request for a file. /// </summary> /// <param name="context">An HTTPContext object that provides references to the intrinsic server objects used to service HTTP requests</param> /// <param name="queryArgument">Name of the argument whose value specifies the location of the data</param> /// <param name="fileExtension">File extension to check against (to prevent serving unauthorized content)</param> /// <param name="minifier">Minifier that should be used to transform the original data</param> /// <param name="contentType">Content type to use when sending a response</param> /// <param name="minificationEnabled">True, if the data should be minified, otherwise false</param> private static void ProcessFileRequest(HttpContext context, string queryArgument, string fileExtension, IResourceMinifier minifier, string contentType, bool minificationEnabled) { // Get URL to the resource file, resolve it in case it's virtual and map to physical path string url = QueryHelper.GetString(queryArgument, string.Empty); string path = URLHelper.GetPhysicalPath(URLHelper.GetVirtualPath(url)); // If this is revalidation request, try quick revalidation check before reading the file CheckRevalidation(context, path); CMSOutputResource resource = null; // Try to get data from cache (or store them if not found) using (CachedSection <CMSOutputResource> cachedSection = new CachedSection <CMSOutputResource>(ref resource, PhysicalFilesCacheMinutes, true, null, "getresource", path, URLHelper.IsSSL)) { // Not found in cache; load the data if (cachedSection.LoadData) { // Retrieve the file resource, rebase client URLs and wrap it in output container resource = GetFile(url, fileExtension); MinifyResource(resource, minifier, minificationEnabled, true); // Cache the file if ((resource != null) && (cachedSection.Cached)) { cachedSection.CacheDependency = new CMSCacheDependency(path); cachedSection.Data = resource; } } } // Send response if there's something to send if (resource != null) { bool allowCache = CacheHelper.AlwaysCacheResources; SendResponse(context, resource, contentType, allowCache, minificationEnabled, PhysicalFilesCacheMinutes); } else { SendNotFoundResponse(context); } }
/// <summary> /// Wraps a piece of text in a data container. /// </summary> /// <param name="resource">Text to wrap</param> /// <param name="name">Identifier for the file</param> /// <param name="etag">Etag to use for versioning</param> /// <param name="lastModified">Timestamp of the last modification of data</param> /// <param name="minifier">Minifier to use when creating minified version of the data</param> /// <param name="minificationEnabled">True, if the data should be minified, otherwise false</param> /// <returns>Data container containing the piece of text</returns> private static CMSOutputResource WrapObject(string resource, string name, string etag, DateTime lastModified, IResourceMinifier minifier, bool minificationEnabled) { if (resource == null) { return(null); } // Prepare new output resource object CMSOutputResource wrapper = new CMSOutputResource() { Name = name, Etag = etag, LastModified = lastModified, Data = resource, }; // Set up the settings if (minificationEnabled) { wrapper.MinifiedData = minifier.Minify(wrapper.Data); } if (RequestHelper.AllowResourceCompression) { wrapper.CompressedData = Compress(resource); } if (minificationEnabled && RequestHelper.AllowResourceCompression) { wrapper.MinifiedCompressedData = Compress(wrapper.MinifiedData); } return(wrapper); }
/// <summary> /// Wraps a piece of text in a data container. /// </summary> /// <param name="resource">Text to wrap</param> /// <param name="name">Identifier for the file</param> /// <param name="etag">Etag to use for versioning</param> /// <param name="lastModified">Timestamp of the last modification of data</param> /// <param name="minifier">Minifier to use when creating minified version of the data</param> /// <param name="minificationEnabled">True, if the data should be minified, otherwise false</param> /// <returns>Data container containing the piece of text</returns> private static CMSOutputResource WrapObject(string resource, string name, string etag, DateTime lastModified, IResourceMinifier minifier, bool minificationEnabled) { if (resource == null) { return null; } // Prepare new output resource object CMSOutputResource wrapper = new CMSOutputResource() { Name = name, Etag = etag, LastModified = lastModified, Data = resource, }; // Set up the settings if (minificationEnabled) { wrapper.MinifiedData = minifier.Minify(wrapper.Data); } if (RequestHelper.AllowResourceCompression) { wrapper.CompressedData = Compress(resource); } if (minificationEnabled && RequestHelper.AllowResourceCompression) { wrapper.MinifiedCompressedData = Compress(wrapper.MinifiedData); } return wrapper; }
/// <summary> /// Processes a request for a file. /// </summary> /// <param name="context">An HTTPContext object that provides references to the intrinsic server objects used to service HTTP requests</param> /// <param name="queryArgument">Name of the argument whose value specifies the location of the data</param> /// <param name="fileExtension">File extension to check against (to prevent serving unauthorized content)</param> /// <param name="minifier">Minifier that should be used to transform the original data</param> /// <param name="contentType">Content type to use when sending a response</param> /// <param name="minificationEnabled">True, if the data should be minified, otherwise false</param> private static void ProcessFileRequest(HttpContext context, string queryArgument, string fileExtension, IResourceMinifier minifier, string contentType, bool minificationEnabled) { // Get URL to the resource file, resolve it in case it's virtual and map to physical path string url = QueryHelper.GetString(queryArgument, string.Empty); string path = URLHelper.GetPhysicalPath(URLHelper.GetVirtualPath(url)); // If this is revalidation request, try quick revalidation check before reading the file CheckRevalidation(context, path); CMSOutputResource resource = null; // Try to get data from cache (or store them if not found) using (CachedSection<CMSOutputResource> cachedSection = new CachedSection<CMSOutputResource>(ref resource, PhysicalFilesCacheMinutes, true, null, "getresource", path, URLHelper.IsSSL)) { // Not found in cache; load the data if (cachedSection.LoadData) { // Retrieve the file resource, rebase client URLs and wrap it in output container resource = GetFile(url, fileExtension); MinifyResource(resource, minifier, minificationEnabled, true); // Cache the file if ((resource != null) && (cachedSection.Cached)) { cachedSection.CacheDependency = new CMSCacheDependency(path); cachedSection.Data = resource; } } } // Send response if there's something to send if (resource != null) { bool allowCache = CacheHelper.AlwaysCacheResources; SendResponse(context, resource, contentType, allowCache, minificationEnabled, PhysicalFilesCacheMinutes); } else { SendNotFoundResponse(context); } }