// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // The image helpers. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Helpers { using System.IO; using System.Text; using System.Text.RegularExpressions; using ImageProcessor.Configuration; using ImageProcessor.Imaging.Formats; /// /// The image helpers. /// public static class ImageHelpers { /// /// The regex pattern. /// public static readonly string ExtensionRegexPattern = BuildExtensionRegexPattern(); /// /// The exclude regex for matching things to ignore when parsing image extensions. /// I'd like to make something more extensible than this. /// private static readonly Regex ExcludeRegex = new Regex(@"mask=[\w+-]+.", RegexOptions.IgnoreCase); /// /// The image format regex. /// private static readonly Regex FormatRegex = new Regex(@"(\.?)(png8|" + ExtensionRegexPattern + ")", RegexOptions.IgnoreCase | RegexOptions.RightToLeft); /// /// The image format regex for matching the file format at the end of a string. /// private static readonly Regex EndFormatRegex = new Regex(@"(\.)" + ExtensionRegexPattern + "$", RegexOptions.IgnoreCase | RegexOptions.RightToLeft); /// /// Checks a given string to check whether the value contains a valid image extension. /// /// The string containing the filename to check. /// True the value contains a valid image extension, otherwise false. public static bool IsValidImageExtension(string fileName) { return EndFormatRegex.IsMatch(fileName); } /// /// Returns the correct file extension for the given string input /// /// /// The string to parse. /// /// /// The correct file extension for the given string input if it can find one; otherwise an empty string. /// public static string GetExtension(string input) { // First filter out any troublesome elements. foreach (Match exclude in ExcludeRegex.Matches(input)) { input = input.Replace(exclude.Value, string.Empty); } Match match = FormatRegex.Match(input); if (match.Success) { // Ah the enigma that is the png file. if (match.Value.ToLowerInvariant().EndsWith("png8")) { return "png"; } return match.Value; } return string.Empty; } /// /// Get the correct mime-type for the given string input. /// /// /// The path to the cached image. /// /// /// The matching the correct mime-type. /// public static string GetMimeType(string path) { using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false)) { ISupportedImageFormat format = FormatUtilities.GetFormat(file); if (format != null) { return format.MimeType; } } return string.Empty; } /// /// Builds a regular expression from the type, this allows extensibility. /// /// /// The to match matrix filters. /// private static string BuildExtensionRegexPattern() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("("); int counter = 0; foreach (ISupportedImageFormat imageFormat in ImageProcessorBootstrapper.Instance.SupportedImageFormats) { foreach (string fileExtension in imageFormat.FileExtensions) { if (counter == 0) { stringBuilder.Append(fileExtension.ToLowerInvariant()); } else { stringBuilder.AppendFormat("|{0}", fileExtension.ToLowerInvariant()); } } counter++; } stringBuilder.Append(")"); return stringBuilder.ToString(); } } }