Exemplo n.º 1
0
        /// <summary>
        /// Applies no transformation, but does try to infer the content type if not set
        /// using the first file in the response (for js and css files only)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="response"></param>
        public void Process(BundleContext context, BundleResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            // No transform done, just set the content type if specified
            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                // Try to infer content type from the bundle files if not specified.
                if (String.IsNullOrEmpty(response.ContentType) && response.Files != null)
                {
                    // only set the content type if the first file has a js or css extension

                    BundleFile firstFile = response.Files.FirstOrDefault();
                    if (firstFile != null)
                    {
                        string extension = VirtualPathUtility.GetExtension(firstFile.VirtualFile.VirtualPath);
                        if (String.Equals(extension, ".js", StringComparison.OrdinalIgnoreCase))
                        {
                            response.ContentType = JsMinify.JsContentType;
                        }
                        else if (String.Equals(extension, ".css", StringComparison.OrdinalIgnoreCase))
                        {
                            response.ContentType = CssMinify.CssContentType;
                        }
                    }
                }
            }
        }
        // Look for <fileName>.<replacementExtension>(.<ext>) in the diretory path
        private static BundleFile FindReplacementFile(BundleContext context, BundleFile file, string replacementExtension)
        {
            string directoryPath       = Path.GetDirectoryName(file.VirtualFile.VirtualPath);
            string extension           = Path.GetExtension(file.VirtualFile.Name);
            string fileName            = Path.GetFileNameWithoutExtension(file.VirtualFile.Name);
            string replacementFileName = fileName + "." + replacementExtension;

            if (extension.Length > 0)
            {
                replacementFileName += extension;
            }

            // NOTE: VirtualPathUtility.Combine does not work as it uses '/' while
            // the default VPP requires '\' to find files, this might have result
            // in issues with Custom VPP implementations and this feature
            string replacementFilePath    = Path.Combine(directoryPath, replacementFileName);
            string replacementIncludePath = Path.Combine(Path.GetDirectoryName(file.IncludedVirtualPath), replacementFileName);

            // Need to fixup the slashes
            replacementIncludePath = replacementIncludePath.Replace('\\', '/');

            // NOTE: We cache every bundle response, so we shouldn't be hitting FileExists too often
            if (context.VirtualPathProvider.FileExists(replacementFilePath))
            {
                return(new BundleFile(replacementIncludePath,
                                      context.VirtualPathProvider.GetFile(replacementFilePath)));
            }
            return(null);
        }