Refactored Azure classes

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-04-26 16:46:55 -07:00
parent 4b52a0aee1
commit 1bb29f266f
4 changed files with 59 additions and 74 deletions

View File

@@ -4,9 +4,9 @@ using System.Linq;
using Microsoft.WindowsAzure.StorageClient; using Microsoft.WindowsAzure.StorageClient;
namespace Orchard.Azure { namespace Orchard.Azure {
public class AzureHelper { public static class CloudBlobContainerExtensions {
public static bool BlobExists(CloudBlobContainer container, string path) { public static bool BlobExists(this CloudBlobContainer container, string path) {
if ( String.IsNullOrEmpty(path) || path.Trim() == String.Empty ) if ( String.IsNullOrEmpty(path) || path.Trim() == String.Empty )
throw new ArgumentException("Path can't be empty"); throw new ArgumentException("Path can't be empty");
@@ -24,59 +24,35 @@ namespace Orchard.Azure {
} }
} }
public static void EnsurePathIsRelative(string path) { public static void EnsureBlobExists(this CloudBlobContainer container, string path) {
if ( path.StartsWith("/") )
throw new ArgumentException("Path must be relative");
}
public static void EnsureBlobExists(CloudBlobContainer container, string path) {
if ( !BlobExists(container, path) ) { if ( !BlobExists(container, path) ) {
throw new ArgumentException("File " + path + " does not exist"); throw new ArgumentException("File " + path + " does not exist");
} }
} }
public static void EnsureBlobDoesNotExist(CloudBlobContainer container, string path) { public static void EnsureBlobDoesNotExist(this CloudBlobContainer container, string path) {
if ( BlobExists(container, path) ) { if ( BlobExists(container, path) ) {
throw new ArgumentException("File " + path + " already exists"); throw new ArgumentException("File " + path + " already exists");
} }
} }
public static bool DirectoryExists(CloudBlobContainer container, string path) { public static bool DirectoryExists(this CloudBlobContainer container, string path) {
if ( String.IsNullOrEmpty(path) || path.Trim() == String.Empty ) if ( String.IsNullOrEmpty(path) || path.Trim() == String.Empty )
throw new ArgumentException("Path can't be empty"); throw new ArgumentException("Path can't be empty");
return container.GetDirectoryReference(path).ListBlobs().Count() > 0; return container.GetDirectoryReference(path).ListBlobs().Count() > 0;
} }
public static void EnsureDirectoryExists(CloudBlobContainer container, string path) { public static void EnsureDirectoryExists(this CloudBlobContainer container, string path) {
if ( !DirectoryExists(container, path) ) { if ( !DirectoryExists(container, path) ) {
throw new ArgumentException("Directory " + path + " does not exist"); throw new ArgumentException("Directory " + path + " does not exist");
} }
} }
public static void EnsureDirectoryDoesNotExist(CloudBlobContainer container, string path) { public static void EnsureDirectoryDoesNotExist(this CloudBlobContainer container, string path) {
if ( DirectoryExists(container, path) ) { if ( DirectoryExists(container, path) ) {
throw new ArgumentException("Directory " + path + " already exists"); throw new ArgumentException("Directory " + path + " already exists");
} }
} }
public static string Combine(string path1, string path2) {
EnsurePathIsRelative(path1);
EnsurePathIsRelative(path2);
if ( path1 == null || path2 == null )
throw new ArgumentException("One or more path is null");
if ( path1.Trim() == String.Empty )
return path2;
if ( path2.Trim() == String.Empty )
return path1;
var uri1 = new Uri(path1);
var uri2 = new Uri(path2);
return uri2.IsAbsoluteUri ? uri2.ToString() : new Uri(uri1, uri2).ToString();
}
} }
} }

View File

@@ -50,7 +50,7 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AzureHelper.cs" /> <Compile Include="CloudBlobContainerExtensions.cs" />
<Compile Include="Environment\Configuration\AzureShellSettingsManager.cs" /> <Compile Include="Environment\Configuration\AzureShellSettingsManager.cs" />
<Compile Include="Storage\AzureBlobStorageProvider.cs" /> <Compile Include="Storage\AzureBlobStorageProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -10,37 +10,47 @@ namespace Orchard.Azure.Storage {
public interface IDependency { } public interface IDependency { }
public class AzureBlobStorageProvider : IStorageProvider { public class AzureBlobStorageProvider : IStorageProvider {
public const string ContainerName = "media"; // container names must be lower cased
private readonly CloudStorageAccount _storageAccount; private readonly CloudStorageAccount _storageAccount;
private string _shellName;
public CloudBlobClient BlobClient { get; private set; } public CloudBlobClient BlobClient { get; private set; }
public CloudBlobContainer Container { get; private set; } public CloudBlobContainer Container { get; private set; }
public AzureBlobStorageProvider(string containerName) public AzureBlobStorageProvider(string shellName)
: this(containerName, CloudStorageAccount.FromConfigurationSetting("DataConnectionString")) { : this(shellName, CloudStorageAccount.FromConfigurationSetting("DataConnectionString")) {
} }
public AzureBlobStorageProvider(string containerName, CloudStorageAccount storageAccount) { public AzureBlobStorageProvider(string shellName, CloudStorageAccount storageAccount) {
// Setup the connection to custom storage accountm, e.g. Development Storage // Setup the connection to custom storage accountm, e.g. Development Storage
_storageAccount = storageAccount; _storageAccount = storageAccount;
_shellName = shellName;
BlobClient = _storageAccount.CreateCloudBlobClient(); BlobClient = _storageAccount.CreateCloudBlobClient();
// Get and create the container if it does not exist // Get and create the container if it does not exist
// The container is named with DNS naming restrictions (i.e. all lower case) // The container is named with DNS naming restrictions (i.e. all lower case)
Container = BlobClient.GetContainerReference(containerName); Container = BlobClient.GetContainerReference(ContainerName);
Container.CreateIfNotExist(); Container.CreateIfNotExist();
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container }); Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
} }
private static void EnsurePathIsRelative(string path) {
if ( path.StartsWith("/") || path.StartsWith("http://"))
throw new ArgumentException("Path must be relative");
}
public IStorageFile GetFile(string path) { public IStorageFile GetFile(string path) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
AzureHelper.EnsureBlobExists(Container, path); Container.EnsureBlobExists(path);
return new AzureBlobFileStorage(Container.GetBlockBlobReference(path)); return new AzureBlobFileStorage(Container.GetBlockBlobReference(path));
} }
public IEnumerable<IStorageFile> ListFiles(string path) { public IEnumerable<IStorageFile> ListFiles(string path) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
string prefix = String.Concat(Container.Name, "/", path); string prefix = String.Concat(Container.Name, "/", _shellName, "/", path);
if ( !prefix.EndsWith("/") ) if ( !prefix.EndsWith("/") )
prefix += "/"; prefix += "/";
@@ -50,8 +60,10 @@ namespace Orchard.Azure.Storage {
} }
public IEnumerable<IStorageFolder> ListFolders(string path) { public IEnumerable<IStorageFolder> ListFolders(string path) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
if ( !AzureHelper.DirectoryExists(Container, path) ) { path = String.Concat(_shellName, "/", path);
if ( !Container.DirectoryExists(path) ) {
try { try {
CreateFolder(path); CreateFolder(path);
} }
@@ -68,25 +80,31 @@ namespace Orchard.Azure.Storage {
} }
public void CreateFolder(string path) { public void CreateFolder(string path) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
AzureHelper.EnsureDirectoryDoesNotExist(Container, path); path = String.Concat(_shellName, "/", path);
Container.EnsureDirectoryDoesNotExist(path);
Container.GetDirectoryReference(path); Container.GetDirectoryReference(path);
} }
public void DeleteFolder(string path) { public void DeleteFolder(string path) {
AzureHelper.EnsureDirectoryExists(Container, path); EnsurePathIsRelative(path);
path = String.Concat(_shellName, "/", path);
Container.EnsureDirectoryExists(path);
foreach ( var blob in Container.GetDirectoryReference(path).ListBlobs() ) { foreach ( var blob in Container.GetDirectoryReference(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()); DeleteFolder(blob.Uri.ToString().Substring(Container.Uri.ToString().Length + 2 + _shellName.Length));
} }
} }
public void RenameFolder(string path, string newPath) { public void RenameFolder(string path, string newPath) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
AzureHelper.EnsurePathIsRelative(newPath);
EnsurePathIsRelative(newPath);
if ( !path.EndsWith("/") ) if ( !path.EndsWith("/") )
path += "/"; path += "/";
@@ -94,7 +112,7 @@ namespace Orchard.Azure.Storage {
if ( !newPath.EndsWith("/") ) if ( !newPath.EndsWith("/") )
newPath += "/"; newPath += "/";
foreach ( var blob in Container.GetDirectoryReference(path).ListBlobs() ) { foreach ( var blob in Container.GetDirectoryReference(_shellName + "/" + 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);
@@ -113,17 +131,23 @@ namespace Orchard.Azure.Storage {
} }
public void DeleteFile(string path) { public void DeleteFile(string path) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
AzureHelper.EnsureBlobExists(Container, path); path = String.Concat(_shellName, "/", path);
Container.EnsureBlobExists(path);
var blob = Container.GetBlockBlobReference(path); var blob = Container.GetBlockBlobReference(path);
blob.Delete(); blob.Delete();
} }
public void RenameFile(string path, string newPath) { public void RenameFile(string path, string newPath) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
AzureHelper.EnsurePathIsRelative(newPath); path = String.Concat(_shellName, "/", path);
AzureHelper.EnsureBlobExists(Container, path);
AzureHelper.EnsureBlobDoesNotExist(Container, newPath); EnsurePathIsRelative(newPath);
newPath = String.Concat(_shellName, "/", newPath);
Container.EnsureBlobExists(path);
Container.EnsureBlobDoesNotExist(newPath);
var blob = Container.GetBlockBlobReference(path); var blob = Container.GetBlockBlobReference(path);
var newBlob = Container.GetBlockBlobReference(newPath); var newBlob = Container.GetBlockBlobReference(newPath);
@@ -132,8 +156,10 @@ namespace Orchard.Azure.Storage {
} }
public IStorageFile CreateFile(string path) { public IStorageFile CreateFile(string path) {
AzureHelper.EnsurePathIsRelative(path); EnsurePathIsRelative(path);
if ( AzureHelper.BlobExists(Container, path) ) { path = String.Concat(_shellName, "/", path);
if ( Container.BlobExists(path) ) {
throw new ArgumentException("File " + path + " already exists"); throw new ArgumentException("File " + path + " already exists");
} }
@@ -220,13 +246,5 @@ namespace Orchard.Azure.Storage {
} }
} }
#region IStorageProvider Members
public string Combine(string path1, string path2) {
return AzureHelper.Combine(path1, path2);
}
#endregion
} }
} }

View File

@@ -11,14 +11,5 @@ namespace Orchard.Storage {
void DeleteFile(string path); void DeleteFile(string path);
void RenameFile(string path, string newPath); void RenameFile(string path, string newPath);
IStorageFile CreateFile(string path); IStorageFile CreateFile(string path);
/// <summary>
/// Combines two path strings
/// </summary>
/// <param name="path1">The first path</param>
/// <param name="path2">The second path</param>
/// <returns>A string containing the combined paths. If one of the specified paths is a zero-length string, this method returns the other path.
/// If path2 contains an absolute path, this method returns path2.</returns>
string Combine(string path1, string path2);
} }
} }