2011-04-09 05:53:26 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using Microsoft.WindowsAzure;
|
2010-05-12 00:53:30 +08:00
|
|
|
|
using Orchard.Environment.Configuration;
|
2010-05-17 04:35:35 +08:00
|
|
|
|
using Orchard.FileSystems.Media;
|
2010-04-24 08:39:23 +08:00
|
|
|
|
|
2010-05-17 04:35:35 +08:00
|
|
|
|
namespace Orchard.Azure.FileSystems.Media {
|
2010-05-08 03:41:37 +08:00
|
|
|
|
public class AzureBlobStorageProvider : AzureFileSystem, IStorageProvider {
|
2010-04-24 08:39:23 +08:00
|
|
|
|
|
2010-05-12 00:53:30 +08:00
|
|
|
|
public AzureBlobStorageProvider(ShellSettings shellSettings)
|
|
|
|
|
: this(shellSettings, CloudStorageAccount.FromConfigurationSetting("DataConnectionString")) {
|
2010-04-24 08:39:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-12 00:53:30 +08:00
|
|
|
|
public AzureBlobStorageProvider(ShellSettings shellSettings, CloudStorageAccount storageAccount) : base("media", shellSettings.Name, false, storageAccount) { }
|
2011-02-15 04:34:05 +08:00
|
|
|
|
|
2011-04-09 05:53:26 +08:00
|
|
|
|
public bool TrySaveStream(string path, Stream inputStream) {
|
|
|
|
|
try {
|
|
|
|
|
SaveStream(path, inputStream);
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2011-02-15 04:34:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 05:53:26 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-15 04:34:05 +08:00
|
|
|
|
}
|
2013-06-08 07:04:44 +08:00
|
|
|
|
|
|
|
|
|
/// <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>
|
2013-07-03 09:10:43 +08:00
|
|
|
|
public string GetStoragePath(string url) {
|
2013-06-08 07:04:44 +08:00
|
|
|
|
if (url.StartsWith(_absoluteRoot)) {
|
|
|
|
|
return url.Substring(_absoluteRoot.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetRelativePath(string path) {
|
|
|
|
|
return GetPublicUrl(path);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-24 08:39:23 +08:00
|
|
|
|
}
|
2010-05-17 04:35:35 +08:00
|
|
|
|
}
|