#17572: Setting mime-type of Azure blobs

Work Item: 17572

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-12-05 11:04:39 -08:00
parent cc4a9693eb
commit 307937b3ec
2 changed files with 60 additions and 2 deletions

View File

@@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Win32;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
@@ -268,7 +271,12 @@ namespace Orchard.Azure {
}
var blob = Container.GetBlockBlobReference(String.Concat(_root, path));
blob.OpenWrite().Dispose(); // force file creation
var contentType = GetContentType(path);
if (!String.IsNullOrWhiteSpace(contentType)) {
blob.Properties.ContentType = contentType;
}
blob.UploadByteArray(new byte[0]);
return new AzureBlobFileStorage(blob, _absoluteRoot);
}
@@ -281,6 +289,39 @@ namespace Orchard.Azure {
}
}
/// <summary>
/// Returns the mime-type of the specified file path, looking into IIS configuration and the Registry
/// </summary>
private string GetContentType(string path) {
string extension = Path.GetExtension(path);
if (String.IsNullOrWhiteSpace(extension)) {
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;
}
}
}
// 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();
}
}
return "application/unknown";
}
private class AzureBlobFileStorage : IStorageFile {
private CloudBlockBlob _blob;
private readonly string _rootPath;