#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.

<system.webServer>
        <staticContent>
            <remove fileExtension=".ogg" />
            <mimeMap fileExtension=".ogg" mimeType="audio/ogg" />
     </staticContent>
</system.webServer>

Work Item: 18705

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-06-30 09:43:02 -07:00
parent 9588189c82
commit b224aa0ed1
2 changed files with 27 additions and 15 deletions

View File

@@ -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";
}

View File

@@ -63,6 +63,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>