mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
#17572: Setting mime-type of Azure blobs
Work Item: 17572 --HG-- branch : 1.x
This commit is contained in:
@@ -255,7 +255,24 @@ namespace Orchard.Azure.Tests.FileSystems.Media {
|
|||||||
foreach(var f in _azureBlobStorageProvider.ListFiles(""))
|
foreach(var f in _azureBlobStorageProvider.ListFiles(""))
|
||||||
{
|
{
|
||||||
Assert.That(HttpContext.Current, Is.Null);
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using System.Xml.XPath;
|
||||||
|
using Microsoft.Win32;
|
||||||
using Microsoft.WindowsAzure;
|
using Microsoft.WindowsAzure;
|
||||||
using Microsoft.WindowsAzure.ServiceRuntime;
|
using Microsoft.WindowsAzure.ServiceRuntime;
|
||||||
using Microsoft.WindowsAzure.StorageClient;
|
using Microsoft.WindowsAzure.StorageClient;
|
||||||
@@ -268,7 +271,12 @@ namespace Orchard.Azure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var blob = Container.GetBlockBlobReference(String.Concat(_root, path));
|
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);
|
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 class AzureBlobFileStorage : IStorageFile {
|
||||||
private CloudBlockBlob _blob;
|
private CloudBlockBlob _blob;
|
||||||
private readonly string _rootPath;
|
private readonly string _rootPath;
|
||||||
|
|||||||
Reference in New Issue
Block a user