From b224aa0ed180f4881dda491409753f8080fa2360 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Sat, 30 Jun 2012 09:43:02 -0700 Subject: [PATCH] #18705: Reading mimetype information from web.config when saving blobs in Azure These mimetypse settings are originally used by IIS itself to get the mimetype of specific files on the file system. We also then use it in AzureFileSystem to assign a mime type to the blob to make it comprehensive. Work Item: 18705 --HG-- branch : 1.x --- src/Orchard.Azure/AzureFileSystem.cs | 41 ++++++++++++++++---------- src/Orchard.Azure/Orchard.Azure.csproj | 1 + 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Orchard.Azure/AzureFileSystem.cs b/src/Orchard.Azure/AzureFileSystem.cs index 521f9c1cf..5e49d8f83 100644 --- a/src/Orchard.Azure/AzureFileSystem.cs +++ b/src/Orchard.Azure/AzureFileSystem.cs @@ -298,26 +298,37 @@ namespace Orchard.Azure { return "application/unknown"; } - string applicationHost = System.Environment.ExpandEnvironmentVariables(@"%windir%\system32\inetsrv\config\applicationHost.config"); - if (File.Exists(applicationHost)) { - var xdoc = XDocument.Load(applicationHost); - var mimeMap = xdoc.XPathSelectElements("//staticContent/mimeMap[@fileExtension='" + extension + "']").FirstOrDefault(); - if(mimeMap != null) { - var mimeType = mimeMap.Attribute("mimeType"); - if(mimeType != null) { - return mimeType.Value; + 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; + } + } } } - } - // 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(); + // 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"; } diff --git a/src/Orchard.Azure/Orchard.Azure.csproj b/src/Orchard.Azure/Orchard.Azure.csproj index 3c949b0f3..b1a6d05ff 100644 --- a/src/Orchard.Azure/Orchard.Azure.csproj +++ b/src/Orchard.Azure/Orchard.Azure.csproj @@ -63,6 +63,7 @@ True + 3.5