diff --git a/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs b/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs index e6c948032..f4cbe9182 100644 --- a/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs +++ b/src/Orchard.Azure.Tests/FileSystems/Media/AzureBlobStorageProviderTests.cs @@ -105,6 +105,17 @@ namespace Orchard.Azure.Tests.FileSystems.Media { Assert.AreEqual("folder", _azureBlobStorageProvider.ListFolders(null).First().GetPath()); } + [Test] + public void CreateFolderWithSubFolder() { + _azureBlobStorageProvider.CreateFolder("folder"); + Assert.AreEqual(0, _azureBlobStorageProvider.ListFolders("folder").Count()); + + _azureBlobStorageProvider.CreateFolder("folder/folder"); + Assert.AreEqual(1, _azureBlobStorageProvider.ListFolders("folder").Count()); + Assert.AreEqual(0, _azureBlobStorageProvider.ListFiles("folder/folder").Count()); + Assert.AreEqual("folder", _azureBlobStorageProvider.ListFolders("folder").First().GetName()); + } + [Test] public void DeleteFolderShouldDeleteFilesAlso() { _azureBlobStorageProvider.CreateFile("folder/foo1.txt"); diff --git a/src/Orchard.Azure/AzureFileSystem.cs b/src/Orchard.Azure/AzureFileSystem.cs index 598974b68..f258e2f8b 100644 --- a/src/Orchard.Azure/AzureFileSystem.cs +++ b/src/Orchard.Azure/AzureFileSystem.cs @@ -51,10 +51,42 @@ namespace Orchard.Azure { } private static void EnsurePathIsRelative(string path) { - if (path.StartsWith("/") || path.StartsWith("http://")) + if ( path.StartsWith("/") || path.StartsWith("http://") || path.StartsWith("https://") ) throw new ArgumentException("Path must be relative"); } + public string Combine(string path1, string path2) { + if ( path1 == null) { + throw new ArgumentNullException("path1"); + } + + if ( path2 == null ) { + throw new ArgumentNullException("path2"); + } + + if ( String.IsNullOrEmpty(path2) ) { + return path1; + } + + if ( String.IsNullOrEmpty(path1) ) { + return path2; + } + + if ( path2.StartsWith("http://") || path2.StartsWith("https://") ) + { + return path2; + } + + var ch = path1[path1.Length - 1]; + + if (ch != '/') + { + return (path1.TrimEnd('/') + '/' + path2.TrimStart('/')); + } + + return (path1 + path2); + } + public IStorageFile GetFile(string path) { EnsurePathIsRelative(path); @@ -75,7 +107,7 @@ namespace Orchard.Azure { EnsurePathIsRelative(path); - string prefix = String.Concat(Container.Name, "/", _root, path); + string prefix = String.Concat(Combine(Container.Name, _root), path); if ( !prefix.EndsWith("/") ) prefix += "/"; @@ -121,7 +153,7 @@ namespace Orchard.Azure { Container.EnsureDirectoryDoesNotExist(String.Concat(_root, path)); // Creating a virtually hidden file to make the directory an existing concept - CreateFile(path + "/" + FolderEntry); + CreateFile(Combine(path, FolderEntry)); } } @@ -263,7 +295,8 @@ namespace Orchard.Azure { } public string GetName() { - return Path.GetDirectoryName(GetPath() + "/"); + var path = GetPath(); + return path.Substring(path.LastIndexOf('/') +1 ); } public string GetPath() { diff --git a/src/Orchard.Web/Modules/Orchard.Media/Services/MediaService.cs b/src/Orchard.Web/Modules/Orchard.Media/Services/MediaService.cs index 3fe95d6d5..e990545bb 100644 --- a/src/Orchard.Web/Modules/Orchard.Media/Services/MediaService.cs +++ b/src/Orchard.Web/Modules/Orchard.Media/Services/MediaService.cs @@ -66,7 +66,7 @@ namespace Orchard.Media.Services { _storageProvider.CreateFolder(name); return; } - _storageProvider.CreateFolder(mediaPath + "\\" + name); + _storageProvider.CreateFolder(_storageProvider.Combine(mediaPath, name)); } public void DeleteFolder(string name) { @@ -79,12 +79,12 @@ namespace Orchard.Media.Services { } public void DeleteFile(string name, string folderName) { - _storageProvider.DeleteFile(folderName + "\\" + name); + _storageProvider.DeleteFile(_storageProvider.Combine(folderName, name)); } public void RenameFile(string name, string newName, string folderName) { if (FileAllowed(newName, false)) { - _storageProvider.RenameFile(folderName + "\\" + name, folderName + "\\" + newName); + _storageProvider.RenameFile(_storageProvider.Combine(folderName, name), _storageProvider.Combine(folderName, newName)); } } @@ -193,14 +193,14 @@ namespace Orchard.Media.Services { } } - private static string RenameFolderPath(string path, string newName) { - var lastIndex = path.LastIndexOf("\\"); + private string RenameFolderPath(string path, string newName) { + var lastIndex = Math.Max(path.LastIndexOf(Path.DirectorySeparatorChar), path.LastIndexOf(Path.AltDirectorySeparatorChar)); if (lastIndex == -1) { return newName; } - return path.Substring(0, lastIndex) + "\\" + newName; + return _storageProvider.Combine(path.Substring(0, lastIndex), newName); } } } diff --git a/src/Orchard/FileSystems/Media/FileSystemStorageProvider.cs b/src/Orchard/FileSystems/Media/FileSystemStorageProvider.cs index 94109df69..5d7a16f48 100644 --- a/src/Orchard/FileSystems/Media/FileSystemStorageProvider.cs +++ b/src/Orchard/FileSystems/Media/FileSystemStorageProvider.cs @@ -148,6 +148,10 @@ namespace Orchard.FileSystems.Media { File.Move(Map(path), Map(newPath)); } + public string Combine(string path1, string path2) { + return Path.Combine(path1, path2); + } + #endregion private class FileSystemStorageFile : IStorageFile { diff --git a/src/Orchard/FileSystems/Media/IStorageProvider.cs b/src/Orchard/FileSystems/Media/IStorageProvider.cs index b2e47c034..d6ae596a9 100644 --- a/src/Orchard/FileSystems/Media/IStorageProvider.cs +++ b/src/Orchard/FileSystems/Media/IStorageProvider.cs @@ -12,5 +12,6 @@ namespace Orchard.FileSystems.Media { void DeleteFile(string path); void RenameFile(string path, string newPath); IStorageFile CreateFile(string path); + string Combine(string path1, string path2); } } \ No newline at end of file