Fixing Azure file system bugs

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-04-08 17:14:07 -07:00
parent 66ddf43730
commit ac9d37de3f
3 changed files with 65 additions and 8 deletions

View File

@@ -128,6 +128,14 @@ namespace Orchard.Azure.Tests.FileSystems.Media {
Assert.AreEqual("folder", _azureBlobStorageProvider.ListFolders("folder").First().GetName());
}
[Test]
public void FoldersShouldBeCreatedRecursively() {
_azureBlobStorageProvider.CreateFolder("foo/bar/baz");
Assert.That(_azureBlobStorageProvider.ListFolders("").Count(), Is.EqualTo(1));
Assert.That(_azureBlobStorageProvider.ListFolders("foo").Count(), Is.EqualTo(1));
Assert.That(_azureBlobStorageProvider.ListFolders("foo/bar").Count(), Is.EqualTo(1));
}
[Test]
public void ShouldDeleteFiles() {
_azureBlobStorageProvider.CreateFile("folder/foo1.txt");
@@ -215,5 +223,25 @@ namespace Orchard.Azure.Tests.FileSystems.Media {
Assert.AreEqual(teststring, content);
}
[Test]
public void ShouldTruncateFile() {
var sf = _azureBlobStorageProvider.CreateFile("folder/foo1.txt");
using (var sw = new StreamWriter(sf.OpenWrite())) {
sw.Write("foo");
}
using (var sw = new StreamWriter(sf.CreateFile())) {
sw.Write("fo");
}
sf = _azureBlobStorageProvider.GetFile("folder/foo1.txt");
string content;
using (var sr = new StreamReader(sf.OpenRead())) {
content = sr.ReadToEnd();
}
Assert.That(content, Is.EqualTo("fo"));
}
}
}

View File

@@ -9,7 +9,7 @@ using Orchard.FileSystems.Media;
namespace Orchard.Azure {
public class AzureFileSystem {
private const string FolderEntry = "$$$ORCHARD$$$.$$$";
public const string FolderEntry = "$$$ORCHARD$$$.$$$";
public string ContainerName { get; protected set; }
@@ -88,7 +88,7 @@ namespace Orchard.Azure {
using ( new HttpContextWeaver() ) {
Container.EnsureBlobExists(String.Concat(_root, path));
return new AzureBlobFileStorage(Container.GetBlockBlobReference(path), _absoluteRoot);
return new AzureBlobFileStorage(Container.GetBlockBlobReference(String.Concat(_root, path)), _absoluteRoot);
}
}
@@ -155,11 +155,11 @@ namespace Orchard.Azure {
try {
if (!Container.DirectoryExists(String.Concat(_root, path))) {
CreateFolder(path);
// return false to be consistent with FileSystemProvider's implementation
return false;
return true;
}
return true;
// return false to be consistent with FileSystemProvider's implementation
return false;
}
catch {
return false;
@@ -173,6 +173,14 @@ namespace Orchard.Azure {
// Creating a virtually hidden file to make the directory an existing concept
CreateFile(Combine(path, FolderEntry));
int lastIndex;
while ((lastIndex = path.LastIndexOf('/')) > 0) {
path = path.Substring(0, lastIndex);
if(!Container.DirectoryExists(String.Concat(_root, path))) {
CreateFile(Combine(path, FolderEntry));
}
}
}
}
@@ -246,11 +254,20 @@ namespace Orchard.Azure {
public IStorageFile CreateFile(string path) {
EnsurePathIsRelative(path);
if ( Container.BlobExists(String.Concat(_root, path)) ) {
throw new ArgumentException("File " + path + " already exists");
}
// create all folder entries in the hierarchy
int lastIndex;
var localPath = path;
while ((lastIndex = localPath.LastIndexOf('/')) > 0) {
localPath = localPath.Substring(0, lastIndex);
var folder = Container.GetBlockBlobReference(String.Concat(_root, Combine(localPath, FolderEntry)));
folder.OpenWrite().Dispose();
}
var blob = Container.GetBlockBlobReference(String.Concat(_root, path));
blob.OpenWrite().Dispose(); // force file creation
return new AzureBlobFileStorage(blob, _absoluteRoot);
@@ -266,7 +283,7 @@ namespace Orchard.Azure {
}
private class AzureBlobFileStorage : IStorageFile {
private readonly CloudBlockBlob _blob;
private CloudBlockBlob _blob;
private readonly string _rootPath;
public AzureBlobFileStorage(CloudBlockBlob blob, string rootPath) {
@@ -306,6 +323,9 @@ namespace Orchard.Azure {
// as opposed to the File System implementation, if nothing is done on the stream
// the file will be emptied, because Azure doesn't implement FileMode.Truncate
_blob.DeleteIfExists();
_blob = _blob.Container.GetBlockBlobReference(_blob.Uri.ToString());
_blob.OpenWrite().Dispose(); // force file creation
return OpenWrite();
}
}

View File

@@ -129,6 +129,15 @@ namespace Orchard.Tests.Storage {
}
}
[Test]
public void ShouldCreateFolders() {
Directory.Delete(_folderPath, true);
_storageProvider.CreateFolder("foo/bar/baz");
Assert.That(_storageProvider.ListFolders("").Count(), Is.EqualTo(1));
Assert.That(_storageProvider.ListFolders("foo").Count(), Is.EqualTo(1));
Assert.That(_storageProvider.ListFolders("foo/bar").Count(), Is.EqualTo(1));
}
[Test]
public void RenameFolderTakesShortPathWithAnyKindOfSlash() {
Assert.That(GetFolder(@"SubFolder1/SubSubFolder1"), Is.Not.Null);