Fixing folder renaming in AzureFileSystem

New Combine in IStorageProvider

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-11-22 18:03:49 -08:00
parent 1918a78646
commit 2fd235d818
5 changed files with 59 additions and 10 deletions

View File

@@ -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");

View File

@@ -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() {

View File

@@ -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);
}
}
}

View File

@@ -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 {

View File

@@ -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);
}
}