--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2011-04-08 15:31:44 -07:00
6 changed files with 72 additions and 23 deletions

View File

@@ -190,9 +190,9 @@ namespace Orchard.Azure.Tests.FileSystems.Media {
}
[Test]
public void CanCreateAlreadyExistingFolder() {
public void TryCreateFolderShouldReturnFalseIfFolderAlreadyExists() {
_azureBlobStorageProvider.CreateFile("folder1/foo.txt");
Assert.That(_azureBlobStorageProvider.TryCreateFolder("folder1"), Is.True);
Assert.That(_azureBlobStorageProvider.TryCreateFolder("folder1"), Is.False);
}
[Test]

View File

@@ -8,8 +8,7 @@ using Microsoft.WindowsAzure.StorageClient;
using Orchard.FileSystems.Media;
namespace Orchard.Azure {
public class AzureFileSystem
{
public class AzureFileSystem {
private const string FolderEntry = "$$$ORCHARD$$$.$$$";
public string ContainerName { get; protected set; }
@@ -71,15 +70,13 @@ namespace Orchard.Azure {
return path2;
}
if ( path2.StartsWith("http://") || path2.StartsWith("https://") )
{
if ( path2.StartsWith("http://") || path2.StartsWith("https://") ) {
return path2;
}
var ch = path1[path1.Length - 1];
if (ch != '/')
{
if (ch != '/') {
return (path1.TrimEnd('/') + '/' + path2.TrimStart('/'));
}
@@ -158,6 +155,9 @@ 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;
}
@@ -302,6 +302,12 @@ namespace Orchard.Azure {
return _blob.OpenWrite();
}
public Stream CreateFile() {
// 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();
return OpenWrite();
}
}
private class AzureBlobFolderStorage : IStorageFolder {

View File

@@ -1,4 +1,5 @@
using Microsoft.WindowsAzure;
using System.IO;
using Microsoft.WindowsAzure;
using Orchard.Environment.Configuration;
using Orchard.FileSystems.Media;
@@ -12,12 +13,31 @@ namespace Orchard.Azure.FileSystems.Media {
public AzureBlobStorageProvider(ShellSettings shellSettings, CloudStorageAccount storageAccount) : base("media", shellSettings.Name, false, storageAccount) { }
public bool TrySaveStream(string path, System.IO.Stream inputStream) {
throw new System.NotImplementedException();
public bool TrySaveStream(string path, Stream inputStream) {
try {
SaveStream(path, inputStream);
}
catch {
return false;
}
return true;
}
public void SaveStream(string path, System.IO.Stream inputStream) {
throw new System.NotImplementedException();
public void SaveStream(string path, Stream inputStream) {
// Create the file.
// The CreateFile method will map the still relative path
var file = CreateFile(path);
using(var outputStream = file.OpenWrite()) {
var buffer = new byte[8192];
for (;;) {
var length = inputStream.Read(buffer, 0, buffer.Length);
if (length <= 0)
break;
outputStream.Write(buffer, 0, length);
}
}
}
}
}

View File

@@ -10,7 +10,7 @@ using Orchard.Recipes.Models;
namespace Orchard.Recipes.Services {
public class RecipeJournalManager : IRecipeJournal {
private readonly IStorageProvider _storageProvider;
private readonly string _recipeJournalFolder = "RecipeJournal" + Path.DirectorySeparatorChar;
private const string RecipeJournalFolder = "RecipeJournal";
private const string WebConfig =
@"
<configuration>
@@ -92,10 +92,10 @@ namespace Orchard.Recipes.Services {
private IStorageFile GetJournalFile(string executionId) {
IStorageFile journalFile;
var journalPath = Path.Combine(_recipeJournalFolder, executionId);
var journalPath = _storageProvider.Combine(RecipeJournalFolder, executionId);
try {
if (_storageProvider.TryCreateFolder(_recipeJournalFolder)) {
var webConfigPath = Path.Combine(_recipeJournalFolder, "web.config");
if (_storageProvider.TryCreateFolder(RecipeJournalFolder)) {
var webConfigPath = _storageProvider.Combine(RecipeJournalFolder, "web.config");
var webConfigFile = _storageProvider.CreateFile(webConfigPath);
WriteWebConfig(webConfigFile);
}
@@ -121,10 +121,9 @@ namespace Orchard.Recipes.Services {
private static void WriteJournal(IStorageFile journalFile, XElement journal) {
string content = journal.ToString();
using (var stream = journalFile.OpenWrite()) {
using (var stream = journalFile.CreateFile()) {
using (var tw = new StreamWriter(stream)) {
tw.Write(content);
stream.SetLength(stream.Position);
}
}
}

View File

@@ -135,8 +135,18 @@ namespace Orchard.FileSystems.Media {
/// <param name="path">The relative path to the folder to be created.</param>
/// <returns>True if success; False otherwise.</returns>
public bool TryCreateFolder(string path) {
try { CreateFolder(path); }
catch { return false; }
try {
// prevent unnecessary exception
DirectoryInfo directoryInfo = new DirectoryInfo(MapStorage(path));
if (directoryInfo.Exists) {
return false;
}
CreateFolder(path);
}
catch {
return false;
}
return true;
}
@@ -250,8 +260,12 @@ namespace Orchard.FileSystems.Media {
/// <param name="inputStream">The stream to be saved.</param>
/// <returns>True if success; False otherwise.</returns>
public bool TrySaveStream(string path, Stream inputStream) {
try { SaveStream(path, inputStream); }
catch { return false; }
try {
SaveStream(path, inputStream);
}
catch {
return false;
}
return true;
}
@@ -334,6 +348,10 @@ namespace Orchard.FileSystems.Media {
return new FileStream(_fileInfo.FullName, FileMode.Open, FileAccess.ReadWrite);
}
public Stream CreateFile() {
return new FileStream(_fileInfo.FullName, FileMode.Truncate, FileAccess.ReadWrite);
}
#endregion
}

View File

@@ -18,5 +18,11 @@ namespace Orchard.FileSystems.Media {
/// Creates a stream for writing to the file.
/// </summary>
Stream OpenWrite();
/// <summary>
/// Creates a stream for writing to the file, and truncates the existing content.
/// </summary>
Stream CreateFile();
}
}