mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Removing HttpContextWeaver from Azure solution
No more needed as internal bug has been fixed
This commit is contained in:
@@ -33,19 +33,16 @@ namespace Orchard.Azure {
|
|||||||
_root = String.IsNullOrEmpty(root) ? "" : root + "/";
|
_root = String.IsNullOrEmpty(root) ? "" : root + "/";
|
||||||
_absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), root);
|
_absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), root);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
BlobClient = _storageAccount.CreateCloudBlobClient();
|
||||||
|
// Get and create the container if it does not exist
|
||||||
|
// The container is named with DNS naming restrictions (i.e. all lower case)
|
||||||
|
Container = BlobClient.GetContainerReference(ContainerName);
|
||||||
|
|
||||||
BlobClient = _storageAccount.CreateCloudBlobClient();
|
Container.CreateIfNotExist();
|
||||||
// Get and create the container if it does not exist
|
|
||||||
// The container is named with DNS naming restrictions (i.e. all lower case)
|
|
||||||
Container = BlobClient.GetContainerReference(ContainerName);
|
|
||||||
|
|
||||||
Container.CreateIfNotExist();
|
Container.SetPermissions(isPrivate
|
||||||
|
? new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }
|
||||||
Container.SetPermissions(isPrivate
|
: new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
||||||
? new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }
|
|
||||||
: new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,22 +89,16 @@ namespace Orchard.Azure {
|
|||||||
|
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
Container.EnsureBlobExists(String.Concat(_root, path));
|
||||||
Container.EnsureBlobExists(String.Concat(_root, path));
|
return new AzureBlobFileStorage(Container.GetBlockBlobReference(String.Concat(_root, path)), _absoluteRoot);
|
||||||
return new AzureBlobFileStorage(Container.GetBlockBlobReference(String.Concat(_root, path)), _absoluteRoot);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool FileExists(string path) {
|
public bool FileExists(string path) {
|
||||||
using (new HttpContextWeaver()) {
|
return Container.BlobExists(String.Concat(_root, path));
|
||||||
return Container.BlobExists(String.Concat(_root, path));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool FolderExists(string path) {
|
public bool FolderExists(string path) {
|
||||||
using (new HttpContextWeaver()) {
|
return Container.DirectoryExists(String.Concat(_root, path));
|
||||||
return Container.DirectoryExists(String.Concat(_root, path));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IStorageFile> ListFiles(string path) {
|
public IEnumerable<IStorageFile> ListFiles(string path) {
|
||||||
@@ -120,14 +111,12 @@ namespace Orchard.Azure {
|
|||||||
if (!prefix.EndsWith("/"))
|
if (!prefix.EndsWith("/"))
|
||||||
prefix += "/";
|
prefix += "/";
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
return BlobClient
|
||||||
return BlobClient
|
.ListBlobsWithPrefix(prefix)
|
||||||
.ListBlobsWithPrefix(prefix)
|
.OfType<CloudBlockBlob>()
|
||||||
.OfType<CloudBlockBlob>()
|
.Where(blobItem => !blobItem.Uri.AbsoluteUri.EndsWith(FolderEntry))
|
||||||
.Where(blobItem => !blobItem.Uri.AbsoluteUri.EndsWith(FolderEntry))
|
.Select(blobItem => new AzureBlobFileStorage(blobItem, _absoluteRoot))
|
||||||
.Select(blobItem => new AzureBlobFileStorage(blobItem, _absoluteRoot))
|
.ToArray();
|
||||||
.ToArray();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IStorageFolder> ListFolders(string path) {
|
public IEnumerable<IStorageFolder> ListFolders(string path) {
|
||||||
@@ -135,32 +124,29 @@ namespace Orchard.Azure {
|
|||||||
path = path ?? String.Empty;
|
path = path ?? String.Empty;
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
// return root folders
|
||||||
|
if (String.Concat(_root, path) == String.Empty) {
|
||||||
// return root folders
|
return Container.ListBlobs()
|
||||||
if (String.Concat(_root, path) == String.Empty) {
|
|
||||||
return Container.ListBlobs()
|
|
||||||
.OfType<CloudBlobDirectory>()
|
|
||||||
.Select<CloudBlobDirectory, IStorageFolder>(d => new AzureBlobFolderStorage(d, _absoluteRoot))
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Container.DirectoryExists(String.Concat(_root, path))) {
|
|
||||||
try {
|
|
||||||
CreateFolder(path);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
throw new ArgumentException(string.Format("The folder could not be created at path: {0}. {1}",
|
|
||||||
path, ex));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Container.GetDirectoryReference(String.Concat(_root, path))
|
|
||||||
.ListBlobs()
|
|
||||||
.OfType<CloudBlobDirectory>()
|
.OfType<CloudBlobDirectory>()
|
||||||
.Select<CloudBlobDirectory, IStorageFolder>(d => new AzureBlobFolderStorage(d, _absoluteRoot))
|
.Select<CloudBlobDirectory, IStorageFolder>(d => new AzureBlobFolderStorage(d, _absoluteRoot))
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Container.DirectoryExists(String.Concat(_root, path))) {
|
||||||
|
try {
|
||||||
|
CreateFolder(path);
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
throw new ArgumentException(string.Format("The folder could not be created at path: {0}. {1}",
|
||||||
|
path, ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Container.GetDirectoryReference(String.Concat(_root, path))
|
||||||
|
.ListBlobs()
|
||||||
|
.OfType<CloudBlobDirectory>()
|
||||||
|
.Select<CloudBlobDirectory, IStorageFolder>(d => new AzureBlobFolderStorage(d, _absoluteRoot))
|
||||||
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryCreateFolder(string path) {
|
public bool TryCreateFolder(string path) {
|
||||||
@@ -180,18 +166,16 @@ namespace Orchard.Azure {
|
|||||||
|
|
||||||
public void CreateFolder(string path) {
|
public void CreateFolder(string path) {
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
using (new HttpContextWeaver()) {
|
Container.EnsureDirectoryDoesNotExist(String.Concat(_root, path));
|
||||||
Container.EnsureDirectoryDoesNotExist(String.Concat(_root, path));
|
|
||||||
|
|
||||||
// Creating a virtually hidden file to make the directory an existing concept
|
// Creating a virtually hidden file to make the directory an existing concept
|
||||||
CreateFile(Combine(path, FolderEntry));
|
CreateFile(Combine(path, FolderEntry));
|
||||||
|
|
||||||
int lastIndex;
|
int lastIndex;
|
||||||
while ((lastIndex = path.LastIndexOf('/')) > 0) {
|
while ((lastIndex = path.LastIndexOf('/')) > 0) {
|
||||||
path = path.Substring(0, lastIndex);
|
path = path.Substring(0, lastIndex);
|
||||||
if (!Container.DirectoryExists(String.Concat(_root, path))) {
|
if (!Container.DirectoryExists(String.Concat(_root, path))) {
|
||||||
CreateFile(Combine(path, FolderEntry));
|
CreateFile(Combine(path, FolderEntry));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,15 +183,13 @@ namespace Orchard.Azure {
|
|||||||
public void DeleteFolder(string path) {
|
public void DeleteFolder(string path) {
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
Container.EnsureDirectoryExists(String.Concat(_root, path));
|
||||||
Container.EnsureDirectoryExists(String.Concat(_root, path));
|
foreach (var blob in Container.GetDirectoryReference(String.Concat(_root, path)).ListBlobs()) {
|
||||||
foreach (var blob in Container.GetDirectoryReference(String.Concat(_root, path)).ListBlobs()) {
|
if (blob is CloudBlob)
|
||||||
if (blob is CloudBlob)
|
((CloudBlob)blob).Delete();
|
||||||
((CloudBlob)blob).Delete();
|
|
||||||
|
|
||||||
if (blob is CloudBlobDirectory)
|
if (blob is CloudBlobDirectory)
|
||||||
DeleteFolder(blob.Uri.ToString().Substring(Container.Uri.ToString().Length + 1 + _root.Length));
|
DeleteFolder(blob.Uri.ToString().Substring(Container.Uri.ToString().Length + 1 + _root.Length));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,21 +202,19 @@ namespace Orchard.Azure {
|
|||||||
|
|
||||||
if (!newPath.EndsWith("/"))
|
if (!newPath.EndsWith("/"))
|
||||||
newPath += "/";
|
newPath += "/";
|
||||||
using (new HttpContextWeaver()) {
|
foreach (var blob in Container.GetDirectoryReference(_root + path).ListBlobs()) {
|
||||||
foreach (var blob in Container.GetDirectoryReference(_root + path).ListBlobs()) {
|
if (blob is CloudBlob) {
|
||||||
if (blob is CloudBlob) {
|
string filename = Path.GetFileName(blob.Uri.ToString());
|
||||||
string filename = Path.GetFileName(blob.Uri.ToString());
|
string source = String.Concat(path, filename);
|
||||||
string source = String.Concat(path, filename);
|
string destination = String.Concat(newPath, filename);
|
||||||
string destination = String.Concat(newPath, filename);
|
RenameFile(source, destination);
|
||||||
RenameFile(source, destination);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (blob is CloudBlobDirectory) {
|
if (blob is CloudBlobDirectory) {
|
||||||
string foldername = blob.Uri.Segments.Last();
|
string foldername = blob.Uri.Segments.Last();
|
||||||
string source = String.Concat(path, foldername);
|
string source = String.Concat(path, foldername);
|
||||||
string destination = String.Concat(newPath, foldername);
|
string destination = String.Concat(newPath, foldername);
|
||||||
RenameFolder(source, destination);
|
RenameFolder(source, destination);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -242,26 +222,22 @@ namespace Orchard.Azure {
|
|||||||
public void DeleteFile(string path) {
|
public void DeleteFile(string path) {
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
Container.EnsureBlobExists(Combine(_root, path));
|
||||||
Container.EnsureBlobExists(Combine(_root, path));
|
var blob = Container.GetBlockBlobReference(Combine(_root, path));
|
||||||
var blob = Container.GetBlockBlobReference(Combine(_root, path));
|
blob.Delete();
|
||||||
blob.Delete();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RenameFile(string path, string newPath) {
|
public void RenameFile(string path, string newPath) {
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
newPath = ConvertToRelativeUriPath(newPath);
|
newPath = ConvertToRelativeUriPath(newPath);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
Container.EnsureBlobExists(String.Concat(_root, path));
|
||||||
Container.EnsureBlobExists(String.Concat(_root, path));
|
Container.EnsureBlobDoesNotExist(String.Concat(_root, newPath));
|
||||||
Container.EnsureBlobDoesNotExist(String.Concat(_root, newPath));
|
|
||||||
|
|
||||||
var blob = Container.GetBlockBlobReference(String.Concat(_root, path));
|
var blob = Container.GetBlockBlobReference(String.Concat(_root, path));
|
||||||
var newBlob = Container.GetBlockBlobReference(String.Concat(_root, newPath));
|
var newBlob = Container.GetBlockBlobReference(String.Concat(_root, newPath));
|
||||||
newBlob.CopyFromBlob(blob);
|
newBlob.CopyFromBlob(blob);
|
||||||
blob.Delete();
|
blob.Delete();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IStorageFile CreateFile(string path) {
|
public IStorageFile CreateFile(string path) {
|
||||||
@@ -293,10 +269,8 @@ namespace Orchard.Azure {
|
|||||||
public string GetPublicUrl(string path) {
|
public string GetPublicUrl(string path) {
|
||||||
path = ConvertToRelativeUriPath(path);
|
path = ConvertToRelativeUriPath(path);
|
||||||
|
|
||||||
using (new HttpContextWeaver()) {
|
Container.EnsureBlobExists(String.Concat(_root, path));
|
||||||
Container.EnsureBlobExists(String.Concat(_root, path));
|
return Container.GetBlockBlobReference(String.Concat(_root, path)).Uri.ToString();
|
||||||
return Container.GetBlockBlobReference(String.Concat(_root, path)).Uri.ToString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -411,15 +385,11 @@ namespace Orchard.Azure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public string GetPath() {
|
public string GetPath() {
|
||||||
using (new HttpContextWeaver()) {
|
return _blob.Uri.ToString().Substring(_rootPath.Length).Trim('/');
|
||||||
return _blob.Uri.ToString().Substring(_rootPath.Length).Trim('/');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long GetSize() {
|
public long GetSize() {
|
||||||
using (new HttpContextWeaver()) {
|
return GetDirectorySize(_blob);
|
||||||
return GetDirectorySize(_blob);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DateTime GetLastUpdated() {
|
public DateTime GetLastUpdated() {
|
||||||
@@ -427,10 +397,8 @@ namespace Orchard.Azure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IStorageFolder GetParent() {
|
public IStorageFolder GetParent() {
|
||||||
using (new HttpContextWeaver()) {
|
if (_blob.Parent != null) {
|
||||||
if (_blob.Parent != null) {
|
return new AzureBlobFolderStorage(_blob.Parent, _rootPath);
|
||||||
return new AzureBlobFolderStorage(_blob.Parent, _rootPath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
throw new ArgumentException("Directory " + _blob.Uri + " does not have a parent directory");
|
throw new ArgumentException("Directory " + _blob.Uri + " does not have a parent directory");
|
||||||
}
|
}
|
||||||
|
@@ -11,11 +11,8 @@ namespace Orchard.Azure {
|
|||||||
throw new ArgumentException("Path can't be empty");
|
throw new ArgumentException("Path can't be empty");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
using ( new HttpContextWeaver() )
|
var blob = container.GetBlockBlobReference(path);
|
||||||
{
|
blob.FetchAttributes();
|
||||||
var blob = container.GetBlockBlobReference(path);
|
|
||||||
blob.FetchAttributes();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch ( StorageClientException e ) {
|
catch ( StorageClientException e ) {
|
||||||
|
@@ -1,28 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Web;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Orchard.Azure {
|
|
||||||
/// <summary>
|
|
||||||
/// THIS IS A HACK
|
|
||||||
/// There is a bug in ASP.NET 4.0 in HttpEncoder.Current, which prevents some calls to HttpUtiliy.Decode/Encode
|
|
||||||
/// from Application_Start, on IIS or Azure. This hack will be removed when the bug is corrected.
|
|
||||||
/// This is fired by the assembly Microsoft.WindowsAzure.StorageClient. Should be corrected in .NET4 SP1
|
|
||||||
/// </summary>
|
|
||||||
public class HttpContextWeaver : IDisposable {
|
|
||||||
private readonly HttpContext _current;
|
|
||||||
|
|
||||||
public HttpContextWeaver()
|
|
||||||
{
|
|
||||||
_current = HttpContext.Current;
|
|
||||||
HttpContext.Current = new HttpContext(new HttpRequest(String.Empty, "http://localhost", String.Empty), new HttpResponse(new StringWriter()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose() {
|
|
||||||
HttpContext.Current = _current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -84,7 +84,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="CloudBlobContainerExtensions.cs" />
|
<Compile Include="CloudBlobContainerExtensions.cs" />
|
||||||
<Compile Include="HttpContextWeaver.cs" />
|
|
||||||
<Compile Include="Environment\Configuration\AzureShellSettingsManager.cs" />
|
<Compile Include="Environment\Configuration\AzureShellSettingsManager.cs" />
|
||||||
<Compile Include="FileSystems\Media\AzureBlobStorageProvider.cs" />
|
<Compile Include="FileSystems\Media\AzureBlobStorageProvider.cs" />
|
||||||
<Compile Include="Logging\AzureAppender.cs" />
|
<Compile Include="Logging\AzureAppender.cs" />
|
||||||
|
Reference in New Issue
Block a user