Orchard/src/Orchard.Azure/FileSystems/Media/AzureBlobStorageProvider.cs
Sebastien Ros 2889d29498 Refactoring the Media Library
- Removed dependency on Orchard.Taxonomies
- Removed dependency on Orchard.Media

You will need to remove the Orchard_MediaLibrary tables and also
the Data Migration record corresponding to this module if you want to upgrade
from 1.x. You can also apply the database changes by hand, remove all the
records then use the Upgrade module to import all media.

--HG--
branch : 1.x
extra : rebase_source : 46100428d9546c3082bf65ce85d8d891543dc0a9
2013-07-02 18:10:43 -07:00

60 lines
2.0 KiB
C#

using System.IO;
using Microsoft.WindowsAzure;
using Orchard.Environment.Configuration;
using Orchard.FileSystems.Media;
namespace Orchard.Azure.FileSystems.Media {
public class AzureBlobStorageProvider : AzureFileSystem, IStorageProvider {
public AzureBlobStorageProvider(ShellSettings shellSettings)
: this(shellSettings, CloudStorageAccount.FromConfigurationSetting("DataConnectionString")) {
}
public AzureBlobStorageProvider(ShellSettings shellSettings, CloudStorageAccount storageAccount) : base("media", shellSettings.Name, false, storageAccount) { }
public bool TrySaveStream(string path, Stream inputStream) {
try {
SaveStream(path, inputStream);
}
catch {
return false;
}
return true;
}
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);
}
}
}
/// <summary>
/// Retrieves the local path for a given url within the storage provider.
/// </summary>
/// <param name="url">The public url of the media.</param>
/// <returns>The local path.</returns>
public string GetStoragePath(string url) {
if (url.StartsWith(_absoluteRoot)) {
return url.Substring(_absoluteRoot.Length);
}
return url;
}
public string GetRelativePath(string path) {
return GetPublicUrl(path);
}
}
}