Batman.MVC.Assets.Filters.CSSFixImagesAndFonts.Filter C# (CSharp) Method

Filter() public method

Filters the assets
public Filter ( IList Assets ) : IList
Assets IList Assets to filter
return IList
        public IList<IAsset> Filter(IList<IAsset> Assets)
        {
            if (Assets == null || Assets.Count == 0)
                return new List<IAsset>();
            if (Assets.FirstOrDefault().Type != AssetType.CSS)
                return Assets;
            FileManager FileSystem = BatComputer.Bootstrapper.Resolve<FileManager>();
            foreach (IAsset Asset in Assets)
            {
                foreach (Match ImageMatch in ImageRegex.Matches(Asset.Content))
                {
                    string TempFile = ImageMatch.Groups["File"].Value;
                    string MatchString = ImageMatch.Value;
                    IFile File = FileSystem.File(TempFile);
                    File = DetermineFile(File, FileSystem, Asset, TempFile);
                    if (File == null || !File.Exists)
                    {
                        IFile AssetFile = FileSystem.File(Asset.Path);
                        File = FileSystem.File(AssetFile.Directory.FullName + "\\" + TempFile);
                    }
                    if (File.Exists)
                    {
                        if (File.Extension.ToUpperInvariant() == ".TTF"
                            || File.Extension.ToUpperInvariant() == ".OTF"
                            || File.Extension.ToUpperInvariant() == ".WOFF"
                            || File.Extension.ToUpperInvariant() == ".SVG"
                            || File.Extension.ToUpperInvariant() == ".EOT")
                        {
                            string MIME = "";
                            switch (File.Extension.ToUpperInvariant())
                            {
                                case ".WOFF":
                                    MIME = "application/x-font-woff";
                                    break;

                                case ".OTF":
                                    MIME = "application/x-font-opentype";
                                    break;

                                case ".TTF":
                                    MIME = "application/x-font-ttf";
                                    break;

                                case ".SVG":
                                    MIME = "image/svg+xml";
                                    break;

                                case ".EOT":
                                    MIME = "application/vnd.ms-fontobject";
                                    break;
                            }

                            Asset.Content = Asset.Content.Replace(MatchString, "url(data:" + MIME + ";base64," + File.ReadBinary().ToString(Base64FormattingOptions.None) + ")");
                        }
                        else
                        {
                            try
                            {
                                using (Bitmap TempImage = new Bitmap(File.FullName))
                                {
                                    string MIMEType = "image/jpeg";
                                    string Content = "";
                                    if (File.FullName.ToUpperInvariant().EndsWith(".PNG", StringComparison.Ordinal))
                                    {
                                        MIMEType = "image/png";
                                        Content = TempImage.ToBase64(ImageFormat.Png);
                                    }
                                    else if (File.FullName.ToUpperInvariant().EndsWith(".JPG", StringComparison.Ordinal) || File.FullName.ToUpperInvariant().EndsWith(".JPEG", StringComparison.Ordinal))
                                    {
                                        MIMEType = "image/jpeg";
                                        Content = TempImage.ToBase64(ImageFormat.Jpeg);
                                    }
                                    else if (File.FullName.ToUpperInvariant().EndsWith(".GIF", StringComparison.Ordinal))
                                    {
                                        MIMEType = "image/gif";
                                        Content = TempImage.ToBase64(ImageFormat.Gif);
                                    }
                                    else if (File.FullName.ToUpperInvariant().EndsWith(".TIFF", StringComparison.Ordinal))
                                    {
                                        MIMEType = "image/tiff";
                                        Content = TempImage.ToBase64(ImageFormat.Tiff);
                                    }
                                    else if (File.FullName.ToUpperInvariant().EndsWith(".BMP", StringComparison.Ordinal))
                                    {
                                        MIMEType = "image/bmp";
                                        Content = TempImage.ToBase64(ImageFormat.Bmp);
                                    }
                                    Asset.Content = Asset.Content.Replace(MatchString, "url(data:" + MIMEType + ";base64," + Content + ")");
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
            return Assets;
        }
CSSFixImagesAndFonts