From 1bb29f266f6a0cfcd549bf851d3076efc9b0cfc8 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Mon, 26 Apr 2010 16:46:55 -0700 Subject: [PATCH] Refactored Azure classes --HG-- branch : dev --- ...per.cs => CloudBlobContainerExtensions.cs} | 38 ++------- src/Orchard.Azure/Orchard.Azure.csproj | 2 +- .../Storage/AzureBlobStorageProvider.cs | 84 +++++++++++-------- src/Orchard/Storage/IStorageProvider.cs | 9 -- 4 files changed, 59 insertions(+), 74 deletions(-) rename src/Orchard.Azure/{AzureHelper.cs => CloudBlobContainerExtensions.cs} (54%) diff --git a/src/Orchard.Azure/AzureHelper.cs b/src/Orchard.Azure/CloudBlobContainerExtensions.cs similarity index 54% rename from src/Orchard.Azure/AzureHelper.cs rename to src/Orchard.Azure/CloudBlobContainerExtensions.cs index 06aaa0c5a..030d4040d 100644 --- a/src/Orchard.Azure/AzureHelper.cs +++ b/src/Orchard.Azure/CloudBlobContainerExtensions.cs @@ -4,9 +4,9 @@ using System.Linq; using Microsoft.WindowsAzure.StorageClient; 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 ) throw new ArgumentException("Path can't be empty"); @@ -24,59 +24,35 @@ namespace Orchard.Azure { } } - public static void EnsurePathIsRelative(string path) { - if ( path.StartsWith("/") ) - throw new ArgumentException("Path must be relative"); - } - - public static void EnsureBlobExists(CloudBlobContainer container, string path) { + public static void EnsureBlobExists(this CloudBlobContainer container, string path) { if ( !BlobExists(container, path) ) { 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) ) { 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 ) throw new ArgumentException("Path can't be empty"); 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) ) { 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) ) { 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(); - } } } diff --git a/src/Orchard.Azure/Orchard.Azure.csproj b/src/Orchard.Azure/Orchard.Azure.csproj index eac004767..f23dc2e67 100644 --- a/src/Orchard.Azure/Orchard.Azure.csproj +++ b/src/Orchard.Azure/Orchard.Azure.csproj @@ -50,7 +50,7 @@ - + diff --git a/src/Orchard.Azure/Storage/AzureBlobStorageProvider.cs b/src/Orchard.Azure/Storage/AzureBlobStorageProvider.cs index b8b7efee4..2181919c8 100644 --- a/src/Orchard.Azure/Storage/AzureBlobStorageProvider.cs +++ b/src/Orchard.Azure/Storage/AzureBlobStorageProvider.cs @@ -10,37 +10,47 @@ namespace Orchard.Azure.Storage { public interface IDependency { } public class AzureBlobStorageProvider : IStorageProvider { + public const string ContainerName = "media"; // container names must be lower cased + private readonly CloudStorageAccount _storageAccount; + private string _shellName; public CloudBlobClient BlobClient { get; private set; } public CloudBlobContainer Container { get; private set; } - public AzureBlobStorageProvider(string containerName) - : this(containerName, CloudStorageAccount.FromConfigurationSetting("DataConnectionString")) { + public AzureBlobStorageProvider(string shellName) + : 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 _storageAccount = storageAccount; + _shellName = shellName; + 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); + Container = BlobClient.GetContainerReference(ContainerName); Container.CreateIfNotExist(); 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) { - AzureHelper.EnsurePathIsRelative(path); - AzureHelper.EnsureBlobExists(Container, path); + EnsurePathIsRelative(path); + Container.EnsureBlobExists(path); return new AzureBlobFileStorage(Container.GetBlockBlobReference(path)); } public IEnumerable 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("/") ) prefix += "/"; @@ -50,8 +60,10 @@ namespace Orchard.Azure.Storage { } public IEnumerable ListFolders(string path) { - AzureHelper.EnsurePathIsRelative(path); - if ( !AzureHelper.DirectoryExists(Container, path) ) { + EnsurePathIsRelative(path); + path = String.Concat(_shellName, "/", path); + + if ( !Container.DirectoryExists(path) ) { try { CreateFolder(path); } @@ -68,25 +80,31 @@ namespace Orchard.Azure.Storage { } public void CreateFolder(string path) { - AzureHelper.EnsurePathIsRelative(path); - AzureHelper.EnsureDirectoryDoesNotExist(Container, path); + EnsurePathIsRelative(path); + path = String.Concat(_shellName, "/", path); + + Container.EnsureDirectoryDoesNotExist(path); Container.GetDirectoryReference(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() ) { if ( blob is CloudBlob ) ( (CloudBlob)blob ).Delete(); 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) { - AzureHelper.EnsurePathIsRelative(path); - AzureHelper.EnsurePathIsRelative(newPath); + EnsurePathIsRelative(path); + + EnsurePathIsRelative(newPath); if ( !path.EndsWith("/") ) path += "/"; @@ -94,7 +112,7 @@ namespace Orchard.Azure.Storage { if ( !newPath.EndsWith("/") ) newPath += "/"; - foreach ( var blob in Container.GetDirectoryReference(path).ListBlobs() ) { + foreach ( var blob in Container.GetDirectoryReference(_shellName + "/" + path).ListBlobs() ) { if ( blob is CloudBlob ) { string filename = Path.GetFileName(blob.Uri.ToString()); string source = String.Concat(path, filename); @@ -113,17 +131,23 @@ namespace Orchard.Azure.Storage { } public void DeleteFile(string path) { - AzureHelper.EnsurePathIsRelative(path); - AzureHelper.EnsureBlobExists(Container, path); + EnsurePathIsRelative(path); + path = String.Concat(_shellName, "/", path); + + Container.EnsureBlobExists(path); var blob = Container.GetBlockBlobReference(path); blob.Delete(); } public void RenameFile(string path, string newPath) { - AzureHelper.EnsurePathIsRelative(path); - AzureHelper.EnsurePathIsRelative(newPath); - AzureHelper.EnsureBlobExists(Container, path); - AzureHelper.EnsureBlobDoesNotExist(Container, newPath); + EnsurePathIsRelative(path); + path = String.Concat(_shellName, "/", path); + + EnsurePathIsRelative(newPath); + newPath = String.Concat(_shellName, "/", newPath); + + Container.EnsureBlobExists(path); + Container.EnsureBlobDoesNotExist(newPath); var blob = Container.GetBlockBlobReference(path); var newBlob = Container.GetBlockBlobReference(newPath); @@ -132,8 +156,10 @@ namespace Orchard.Azure.Storage { } public IStorageFile CreateFile(string path) { - AzureHelper.EnsurePathIsRelative(path); - if ( AzureHelper.BlobExists(Container, path) ) { + EnsurePathIsRelative(path); + path = String.Concat(_shellName, "/", path); + + if ( Container.BlobExists(path) ) { 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 } } diff --git a/src/Orchard/Storage/IStorageProvider.cs b/src/Orchard/Storage/IStorageProvider.cs index 25d4e3eea..79b260556 100644 --- a/src/Orchard/Storage/IStorageProvider.cs +++ b/src/Orchard/Storage/IStorageProvider.cs @@ -11,14 +11,5 @@ namespace Orchard.Storage { void DeleteFile(string path); void RenameFile(string path, string newPath); IStorageFile CreateFile(string path); - - /// - /// Combines two path strings - /// - /// The first path - /// The second path - /// 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. - string Combine(string path1, string path2); } }