diff --git a/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs b/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs index 41b1c8c59..633a6613f 100644 --- a/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs +++ b/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs @@ -255,7 +255,24 @@ namespace Orchard.Azure.Tests.FileSystems.Media { foreach(var f in _azureBlobStorageProvider.ListFiles("")) { Assert.That(HttpContext.Current, Is.Null); - }; + } } + + [Test] + public void MimeTypeShouldBeSet() { + _azureBlobStorageProvider.CreateFile("foo1.txt"); + var file = _azureBlobStorageProvider.Container.GetBlockBlobReference("default/foo1.txt"); + file.FetchAttributes(); + Assert.That(file.Properties.ContentType, Is.EqualTo("text/plain")); + } + + [Test] + public void UnknownMimeTypeShouldBeAssigned() { + _azureBlobStorageProvider.CreateFile("foo1.xyz"); + var file = _azureBlobStorageProvider.Container.GetBlockBlobReference("default/foo1.xyz"); + file.FetchAttributes(); + Assert.That(file.Properties.ContentType, Is.EqualTo("application/unknown")); + } + } } \ No newline at end of file diff --git a/src/Orchard.Azure/AzureFileSystem.cs b/src/Orchard.Azure/AzureFileSystem.cs index 06b57bdbc..6e93dbbc3 100644 --- a/src/Orchard.Azure/AzureFileSystem.cs +++ b/src/Orchard.Azure/AzureFileSystem.cs @@ -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 { } } + /// + /// Returns the mime-type of the specified file path, looking into IIS configuration and the Registry + /// + 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;