From 7c6acd59c5016cea4f73490c755d92cf31b682d4 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 7 Nov 2014 16:33:10 -0800 Subject: [PATCH] Changing mime type provider implementation to use System.Web.MimeMapping. --- .../Media/ConfigurationMimeTypeProvider.cs | 71 +++---------------- 1 file changed, 8 insertions(+), 63 deletions(-) diff --git a/src/Orchard/FileSystems/Media/ConfigurationMimeTypeProvider.cs b/src/Orchard/FileSystems/Media/ConfigurationMimeTypeProvider.cs index fde2042ee..5e2585fec 100644 --- a/src/Orchard/FileSystems/Media/ConfigurationMimeTypeProvider.cs +++ b/src/Orchard/FileSystems/Media/ConfigurationMimeTypeProvider.cs @@ -1,72 +1,17 @@ -using System; -using System.IO; -using System.Linq; -using System.Xml.Linq; -using System.Xml.XPath; -using Microsoft.Win32; -using Orchard.Caching; +using System.Web; namespace Orchard.FileSystems.Media { /// - /// Returns the mime-type by looking into IIS configuration and the Registry + /// Returns the mime-type of the specified file path. /// public class ConfigurationMimeTypeProvider : IMimeTypeProvider { - private readonly ICacheManager _cacheManager; - - public ConfigurationMimeTypeProvider(ICacheManager cacheManager) { - _cacheManager = cacheManager; - } - + /// - /// Returns the mime-type of the specified file path + /// Returns the mime-type of the specified file path. /// public string GetMimeType(string path) { - string extension = Path.GetExtension(path); - if (String.IsNullOrWhiteSpace(extension)) { - return "application/unknown"; - } - - return _cacheManager.Get(extension, ctx => { - try { - try { - - string applicationHost = System.Environment.ExpandEnvironmentVariables(@"%windir%\system32\inetsrv\config\applicationHost.config"); - string webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").FilePath; - - // search for custom mime types in web.config and applicationhost.config - foreach (var configFile in new[] { webConfig, applicationHost }) { - if (File.Exists(configFile)) { - var xdoc = XDocument.Load(configFile); - var mimeMap = xdoc.XPathSelectElements("//staticContent/mimeMap[@fileExtension='" + extension + "']").FirstOrDefault(); - if (mimeMap != null) { - var mimeType = mimeMap.Attribute("mimeType"); - if (mimeType != null) { - return mimeType.Value; - } - } - } - } - } - catch { - // ignore issues with web.config to fall back to registry - } - - // search into the registry - RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(extension.ToLower()); - if (regKey != null) { - var contentType = regKey.GetValue("Content Type"); - if (contentType != null) { - return contentType.ToString(); - } - } - } - catch { - // if an exception occured return application/unknown - return "application/unknown"; - } - - return "application/unknown"; - }); - } + // Returns "application/octet-stream" for unmapped / unkown file types. + return MimeMapping.GetMimeMapping(path); + } } -} +} \ No newline at end of file