mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Implementing Windows Azure Blob Storage provider module
This commit is contained in:
BIN
lib/windowsazure/Microsoft.WindowsAzure.Configuration.dll
Normal file
BIN
lib/windowsazure/Microsoft.WindowsAzure.Configuration.dll
Normal file
Binary file not shown.
BIN
lib/windowsazure/Microsoft.WindowsAzure.Storage.dll
Normal file
BIN
lib/windowsazure/Microsoft.WindowsAzure.Storage.dll
Normal file
Binary file not shown.
16198
lib/windowsazure/Microsoft.WindowsAzure.Storage.xml
Normal file
16198
lib/windowsazure/Microsoft.WindowsAzure.Storage.xml
Normal file
File diff suppressed because it is too large
Load Diff
12
src/Orchard.Web/Modules/Orchard.AzureBlobStorage/Module.txt
Normal file
12
src/Orchard.Web/Modules/Orchard.AzureBlobStorage/Module.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
Name: Orchard.AzureBlobStorage
|
||||
AntiForgery: enabled
|
||||
Author: The Orchard Team
|
||||
Website: http://orchardproject.net
|
||||
Version: 1.0
|
||||
OrchardVersion: 1.7
|
||||
Description: Description for the module
|
||||
Features:
|
||||
Orchard.AzureBlobStorage:
|
||||
Description: Stores the files on Azure Blob Storage
|
||||
Name: Azure Blob Storage
|
||||
Category: Hosting
|
@@ -0,0 +1,139 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>9.0.30729</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{CBC7993C-57D8-4A6C-992C-19E849DFE71D}</ProjectGuid>
|
||||
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Orchard.AzureBlobStorage</RootNamespace>
|
||||
<AssemblyName>Orchard.AzureBlobStorage</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<MvcBuildViews>false</MvcBuildViews>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<OldToolsVersion>4.0</OldToolsVersion>
|
||||
<UpgradeBackupLocation />
|
||||
<TargetFrameworkProfile />
|
||||
<UseIISExpress>false</UseIISExpress>
|
||||
<IISExpressSSLPort />
|
||||
<IISExpressAnonymousAuthentication />
|
||||
<IISExpressWindowsAuthentication />
|
||||
<IISExpressUseClassicPipelineMode />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="Microsoft.WindowsAzure.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\windowsazure\Microsoft.WindowsAzure.Configuration.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.WindowsAzure.Storage, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\windowsazure\Microsoft.WindowsAzure.Storage.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations">
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.DynamicData" />
|
||||
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Abstractions" />
|
||||
<Reference Include="System.Web.Routing" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Web.config" />
|
||||
<Content Include="Properties\AssemblyInfo.cs" />
|
||||
<Content Include="Module.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Orchard\Orchard.Framework.csproj">
|
||||
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
|
||||
<Name>Orchard.Framework</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Core\Orchard.Core.csproj">
|
||||
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
|
||||
<Name>Orchard.Core</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Services\AzureFileSystem.cs" />
|
||||
<Compile Include="Services\CloudBlobContainerExtensions.cs" />
|
||||
<Compile Include="Services\AzureBlobStorageProvider.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target> -->
|
||||
<Target Name="AfterBuild" DependsOnTargets="AfterBuildCompiler">
|
||||
<PropertyGroup>
|
||||
<AreasManifestDir>$(ProjectDir)\..\Manifests</AreasManifestDir>
|
||||
</PropertyGroup>
|
||||
<!-- If this is an area child project, uncomment the following line:
|
||||
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Child" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
|
||||
-->
|
||||
<!-- If this is an area parent project, uncomment the following lines:
|
||||
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Parent" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
|
||||
<CopyAreaManifests ManifestPath="$(AreasManifestDir)" CrossCopy="false" RenameViews="true" />
|
||||
-->
|
||||
</Target>
|
||||
<Target Name="AfterBuildCompiler" Condition="'$(MvcBuildViews)'=='true'">
|
||||
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
|
||||
</Target>
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
|
||||
<WebProjectProperties>
|
||||
<UseIIS>False</UseIIS>
|
||||
<AutoAssignPort>True</AutoAssignPort>
|
||||
<DevelopmentServerPort>45979</DevelopmentServerPort>
|
||||
<DevelopmentServerVPath>/</DevelopmentServerVPath>
|
||||
<IISUrl>
|
||||
</IISUrl>
|
||||
<NTLMAuthentication>False</NTLMAuthentication>
|
||||
<UseCustomServer>True</UseCustomServer>
|
||||
<CustomServerUrl>http://orchard.codeplex.com</CustomServerUrl>
|
||||
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
|
||||
</WebProjectProperties>
|
||||
</FlavorProperties>
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Orchard.AzureBlobStorage")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyProduct("Orchard")]
|
||||
[assembly: AssemblyCopyright("Copyright <20> Outercurve Foundation 2013")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("bcd6a602-4041-4a40-af4f-82f3dc0ab136")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("1.0")]
|
||||
[assembly: AssemblyFileVersion("1.0")]
|
||||
|
@@ -0,0 +1,61 @@
|
||||
using System.IO;
|
||||
using Microsoft.WindowsAzure;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.FileSystems.Media;
|
||||
|
||||
namespace Orchard.AzureBlobStorage.Services {
|
||||
[OrchardSuppressDependency("Orchard.FileSystems.Media.FileSystemStorageProvider")]
|
||||
public class AzureBlobStorageProvider : AzureFileSystem, IStorageProvider {
|
||||
public AzureBlobStorageProvider(ShellSettings shellSettings, IMimeTypeProvider mimeTypeProvider)
|
||||
: this(shellSettings, CloudConfigurationManager.GetSetting("StorageConnectionString"), mimeTypeProvider) {
|
||||
}
|
||||
|
||||
public AzureBlobStorageProvider(ShellSettings shellSettings, string storageConnectionString, IMimeTypeProvider mimeTypeProvider) : base("media", shellSettings.Name, false, storageConnectionString, mimeTypeProvider) { }
|
||||
|
||||
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(Combine(_absoluteRoot, "/").Length);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetRelativePath(string path) {
|
||||
return GetPublicUrl(path);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,365 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.WindowsAzure.Storage.Blob;
|
||||
using Orchard.FileSystems.Media;
|
||||
|
||||
namespace Orchard.AzureBlobStorage.Services {
|
||||
public class AzureFileSystem {
|
||||
private readonly IMimeTypeProvider _mimeTypeProvider;
|
||||
public const string FolderEntry = "$$$ORCHARD$$$.$$$";
|
||||
|
||||
public string ContainerName { get; protected set; }
|
||||
|
||||
private readonly CloudStorageAccount _storageAccount;
|
||||
protected readonly string _root;
|
||||
protected readonly string _absoluteRoot;
|
||||
public CloudBlobClient BlobClient { get; private set; }
|
||||
public CloudBlobContainer Container { get; private set; }
|
||||
|
||||
public AzureFileSystem(string containerName, string root, bool isPrivate, string storageConnectionString, IMimeTypeProvider mimeTypeProvider) {
|
||||
_mimeTypeProvider = mimeTypeProvider;
|
||||
// Setup the connection to custom storage accountm, e.g. Development Storage
|
||||
_storageAccount = CloudStorageAccount.Parse(storageConnectionString);
|
||||
ContainerName = containerName;
|
||||
_root = String.IsNullOrEmpty(root) ? "" : root + "/";
|
||||
_absoluteRoot = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), root);
|
||||
|
||||
BlobClient = _storageAccount.CreateCloudBlobClient();
|
||||
// Get and create the container if it does not exist
|
||||
// The container is named with DNS naming restrictions (i.e. all lower case)
|
||||
Container = BlobClient.GetContainerReference(ContainerName);
|
||||
|
||||
Container.CreateIfNotExists();
|
||||
|
||||
Container.SetPermissions(isPrivate
|
||||
? new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }
|
||||
: new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
||||
|
||||
}
|
||||
|
||||
private static string ConvertToRelativeUriPath(string path) {
|
||||
var newPath = path.Replace(@"\", "/");
|
||||
|
||||
if (newPath.StartsWith("/") || newPath.StartsWith("http://") || newPath.StartsWith("https://"))
|
||||
throw new ArgumentException("Path must be relative");
|
||||
|
||||
return newPath;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
|
||||
Container.EnsureBlobExists(String.Concat(_root, path));
|
||||
return new AzureBlobFileStorage(Container.GetBlockBlobReference(String.Concat(_root, path)), _absoluteRoot);
|
||||
}
|
||||
|
||||
public bool FileExists(string path) {
|
||||
return Container.BlobExists(String.Concat(_root, path));
|
||||
}
|
||||
|
||||
public bool FolderExists(string path) {
|
||||
return Container.DirectoryExists(String.Concat(_root, path));
|
||||
}
|
||||
|
||||
public IEnumerable<IStorageFile> ListFiles(string path) {
|
||||
|
||||
path = path ?? String.Empty;
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
|
||||
string prefix = Combine(Combine(Container.Name, _root), path);
|
||||
|
||||
if (!prefix.EndsWith("/"))
|
||||
prefix += "/";
|
||||
|
||||
return BlobClient.ListBlobs(prefix, true)
|
||||
.OfType<CloudBlockBlob>()
|
||||
.Where(blobItem => !blobItem.Uri.AbsoluteUri.EndsWith(FolderEntry))
|
||||
.Select(blobItem => new AzureBlobFileStorage(blobItem, _absoluteRoot))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public IEnumerable<IStorageFolder> ListFolders(string path) {
|
||||
|
||||
path = path ?? String.Empty;
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
|
||||
// return root folders
|
||||
if (String.Concat(_root, path) == String.Empty) {
|
||||
return Container.ListBlobs()
|
||||
.OfType<CloudBlobDirectory>()
|
||||
.Select<CloudBlobDirectory, IStorageFolder>(d => new AzureBlobFolderStorage(d, _absoluteRoot))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
if (!Container.DirectoryExists(String.Concat(_root, path))) {
|
||||
try {
|
||||
CreateFolder(path);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new ArgumentException(string.Format("The folder could not be created at path: {0}. {1}",
|
||||
path, ex));
|
||||
}
|
||||
}
|
||||
|
||||
return Container.GetDirectoryReference(String.Concat(_root, path))
|
||||
.ListBlobs()
|
||||
.OfType<CloudBlobDirectory>()
|
||||
.Select<CloudBlobDirectory, IStorageFolder>(d => new AzureBlobFolderStorage(d, _absoluteRoot))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public bool TryCreateFolder(string path) {
|
||||
try {
|
||||
if (!Container.DirectoryExists(String.Concat(_root, path))) {
|
||||
CreateFolder(path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// return false to be consistent with FileSystemProvider's implementation
|
||||
return false;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateFolder(string path) {
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
Container.EnsureDirectoryDoesNotExist(String.Concat(_root, path));
|
||||
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFolder(string path) {
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
|
||||
Container.EnsureDirectoryExists(String.Concat(_root, path));
|
||||
foreach (var blob in Container.GetDirectoryReference(String.Concat(_root, path)).ListBlobs()) {
|
||||
if (blob is CloudBlockBlob)
|
||||
((CloudBlockBlob)blob).Delete();
|
||||
|
||||
if (blob is CloudBlobDirectory)
|
||||
DeleteFolder(blob.Uri.ToString().Substring(Container.Uri.ToString().Length + 1 + _root.Length));
|
||||
}
|
||||
}
|
||||
|
||||
public void RenameFolder(string path, string newPath) {
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
newPath = ConvertToRelativeUriPath(newPath);
|
||||
|
||||
if (!path.EndsWith("/"))
|
||||
path += "/";
|
||||
|
||||
if (!newPath.EndsWith("/"))
|
||||
newPath += "/";
|
||||
foreach (var blob in Container.GetDirectoryReference(_root + path).ListBlobs()) {
|
||||
if (blob is CloudBlockBlob) {
|
||||
string filename = Path.GetFileName(blob.Uri.ToString());
|
||||
string source = String.Concat(path, filename);
|
||||
string destination = String.Concat(newPath, filename);
|
||||
RenameFile(source, destination);
|
||||
}
|
||||
|
||||
if (blob is CloudBlobDirectory) {
|
||||
string foldername = blob.Uri.Segments.Last();
|
||||
string source = String.Concat(path, foldername);
|
||||
string destination = String.Concat(newPath, foldername);
|
||||
RenameFolder(source, destination);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFile(string path) {
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
|
||||
Container.EnsureBlobExists(Combine(_root, path));
|
||||
var blob = Container.GetBlockBlobReference(Combine(_root, path));
|
||||
blob.DeleteIfExists();
|
||||
}
|
||||
|
||||
public void RenameFile(string path, string newPath) {
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
newPath = ConvertToRelativeUriPath(newPath);
|
||||
|
||||
Container.EnsureBlobExists(String.Concat(_root, path));
|
||||
Container.EnsureBlobDoesNotExist(String.Concat(_root, newPath));
|
||||
|
||||
var blob = Container.GetBlockBlobReference(String.Concat(_root, path));
|
||||
var newBlob = Container.GetBlockBlobReference(String.Concat(_root, newPath));
|
||||
newBlob.StartCopyFromBlob(blob);
|
||||
}
|
||||
|
||||
public IStorageFile CreateFile(string path) {
|
||||
path = ConvertToRelativeUriPath(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));
|
||||
var contentType = _mimeTypeProvider.GetMimeType(path);
|
||||
if (!String.IsNullOrWhiteSpace(contentType)) {
|
||||
blob.Properties.ContentType = contentType;
|
||||
}
|
||||
|
||||
blob.UploadFromStream(new MemoryStream(new byte[0]));
|
||||
return new AzureBlobFileStorage(blob, _absoluteRoot);
|
||||
}
|
||||
|
||||
public string GetPublicUrl(string path) {
|
||||
path = ConvertToRelativeUriPath(path);
|
||||
|
||||
Container.EnsureBlobExists(String.Concat(_root, path));
|
||||
return Container.GetBlockBlobReference(String.Concat(_root, path)).Uri.ToString();
|
||||
}
|
||||
|
||||
private class AzureBlobFileStorage : IStorageFile {
|
||||
private CloudBlockBlob _blob;
|
||||
private readonly string _rootPath;
|
||||
|
||||
public AzureBlobFileStorage(CloudBlockBlob blob, string rootPath) {
|
||||
_blob = blob;
|
||||
_rootPath = rootPath;
|
||||
}
|
||||
|
||||
public string GetPath() {
|
||||
return _blob.Uri.ToString().Substring(_rootPath.Length).Trim('/');
|
||||
}
|
||||
|
||||
public string GetName() {
|
||||
return Path.GetFileName(GetPath());
|
||||
}
|
||||
|
||||
public long GetSize() {
|
||||
return _blob.Properties.Length;
|
||||
}
|
||||
|
||||
public DateTime GetLastUpdated() {
|
||||
return _blob.Properties.LastModified.GetValueOrDefault().DateTime;
|
||||
}
|
||||
|
||||
public string GetFileType() {
|
||||
return Path.GetExtension(GetPath());
|
||||
}
|
||||
|
||||
public Stream OpenRead() {
|
||||
return _blob.OpenRead();
|
||||
}
|
||||
|
||||
public Stream OpenWrite() {
|
||||
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();
|
||||
_blob = _blob.Container.GetBlockBlobReference(_blob.Uri.ToString());
|
||||
_blob.UploadFromStream(new MemoryStream(new byte[0]));
|
||||
|
||||
return OpenWrite();
|
||||
}
|
||||
}
|
||||
|
||||
private class AzureBlobFolderStorage : IStorageFolder {
|
||||
private readonly CloudBlobDirectory _blob;
|
||||
private readonly string _rootPath;
|
||||
|
||||
public AzureBlobFolderStorage(CloudBlobDirectory blob, string rootPath) {
|
||||
_blob = blob;
|
||||
_rootPath = rootPath;
|
||||
}
|
||||
|
||||
public string GetName() {
|
||||
var path = GetPath();
|
||||
return path.Substring(path.LastIndexOf('/') + 1);
|
||||
}
|
||||
|
||||
public string GetPath() {
|
||||
return _blob.Uri.ToString().Substring(_rootPath.Length).Trim('/');
|
||||
}
|
||||
|
||||
public long GetSize() {
|
||||
return GetDirectorySize(_blob);
|
||||
}
|
||||
|
||||
public DateTime GetLastUpdated() {
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
|
||||
public IStorageFolder GetParent() {
|
||||
if (_blob.Parent != null) {
|
||||
return new AzureBlobFolderStorage(_blob.Parent, _rootPath);
|
||||
}
|
||||
throw new ArgumentException("Directory " + _blob.Uri + " does not have a parent directory");
|
||||
}
|
||||
|
||||
private static long GetDirectorySize(CloudBlobDirectory directoryBlob) {
|
||||
long size = 0;
|
||||
|
||||
foreach (var blobItem in directoryBlob.ListBlobs()) {
|
||||
if (blobItem is CloudBlockBlob)
|
||||
size += ((CloudBlockBlob)blobItem).Properties.Length;
|
||||
|
||||
if (blobItem is CloudBlobDirectory)
|
||||
size += GetDirectorySize((CloudBlobDirectory)blobItem);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.WindowsAzure.Storage.Blob;
|
||||
|
||||
namespace Orchard.AzureBlobStorage.Services {
|
||||
public static class CloudBlobContainerExtensions {
|
||||
|
||||
public static bool BlobExists(this CloudBlobContainer container, string path) {
|
||||
if (String.IsNullOrEmpty(path) || path.Trim() == String.Empty)
|
||||
throw new ArgumentException("Path can't be empty");
|
||||
|
||||
try {
|
||||
var blob = container.GetBlockBlobReference(path);
|
||||
blob.FetchAttributes();
|
||||
return true;
|
||||
}
|
||||
catch (StorageException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void EnsureBlobExists(this CloudBlobContainer container, string path) {
|
||||
if (!BlobExists(container, path)) {
|
||||
throw new ArgumentException("File " + path + " does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
public static void EnsureBlobDoesNotExist(this CloudBlobContainer container, string path) {
|
||||
if (BlobExists(container, path)) {
|
||||
throw new ArgumentException("File " + path + " already exists");
|
||||
}
|
||||
}
|
||||
|
||||
public static bool DirectoryExists(this CloudBlobContainer container, string path) {
|
||||
if (String.IsNullOrEmpty(path) || path.Trim() == String.Empty)
|
||||
throw new ArgumentException("Path can't be empty");
|
||||
|
||||
return container.GetDirectoryReference(path).ListBlobs().Any();
|
||||
}
|
||||
|
||||
public static void EnsureDirectoryExists(this CloudBlobContainer container, string path) {
|
||||
if (!DirectoryExists(container, path)) {
|
||||
throw new ArgumentException("Directory " + path + " does not exist");
|
||||
}
|
||||
}
|
||||
|
||||
public static void EnsureDirectoryDoesNotExist(this CloudBlobContainer container, string path) {
|
||||
if (DirectoryExists(container, path)) {
|
||||
throw new ArgumentException("Directory " + path + " already exists");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
41
src/Orchard.Web/Modules/Orchard.AzureBlobStorage/Web.config
Normal file
41
src/Orchard.Web/Modules/Orchard.AzureBlobStorage/Web.config
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0"?>
|
||||
<configuration>
|
||||
|
||||
<configSections>
|
||||
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
|
||||
<remove name="host" />
|
||||
<remove name="pages" />
|
||||
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
|
||||
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
|
||||
</sectionGroup>
|
||||
</configSections>
|
||||
|
||||
<system.web.webPages.razor>
|
||||
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<pages pageBaseType="Orchard.Mvc.ViewEngines.Razor.WebViewPage">
|
||||
<namespaces>
|
||||
<add namespace="System.Web.Mvc" />
|
||||
<add namespace="System.Web.Mvc.Ajax" />
|
||||
<add namespace="System.Web.Mvc.Html" />
|
||||
<add namespace="System.Web.Routing" />
|
||||
<add namespace="System.Web.WebPages" />
|
||||
<add namespace="System.Linq"/>
|
||||
<add namespace="System.Collections.Generic"/>
|
||||
<add namespace="Orchard.Mvc.Html"/>
|
||||
</namespaces>
|
||||
</pages>
|
||||
</system.web.webPages.razor>
|
||||
|
||||
<system.web>
|
||||
<compilation targetFramework="4.0">
|
||||
<assemblies>
|
||||
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
|
||||
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
|
||||
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
|
||||
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
|
||||
</assemblies>
|
||||
</compilation>
|
||||
</system.web>
|
||||
|
||||
</configuration>
|
@@ -160,6 +160,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Upgrade", "Orchard.Web\Modu
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules.Deprecated", "Modules.Deprecated", "{902528F6-1444-42A3-8B75-A54B775B539C}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Orchard.AzureBlobStorage", "Orchard.Web\Modules\Orchard.AzureBlobStorage\Orchard.AzureBlobStorage.csproj", "{CBC7993C-57D8-4A6C-992C-19E849DFE71D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
CodeCoverage|Any CPU = CodeCoverage|Any CPU
|
||||
@@ -888,6 +890,16 @@ Global
|
||||
{8A9FDB57-342D-49C2-BAFC-D885AAE5CC7C}.FxCop|Any CPU.Build.0 = Release|Any CPU
|
||||
{8A9FDB57-342D-49C2-BAFC-D885AAE5CC7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8A9FDB57-342D-49C2-BAFC-D885AAE5CC7C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.CodeCoverage|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.CodeCoverage|Any CPU.Build.0 = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Coverage|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Coverage|Any CPU.Build.0 = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.FxCop|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.FxCop|Any CPU.Build.0 = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -945,6 +957,7 @@ Global
|
||||
{73A7688A-5BD3-4F7E-ADFA-CE36C5A10E3B} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5}
|
||||
{6E444FF1-A47C-4CF6-BB3F-507C8EBD776D} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5}
|
||||
{8A9FDB57-342D-49C2-BAFC-D885AAE5CC7C} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5}
|
||||
{CBC7993C-57D8-4A6C-992C-19E849DFE71D} = {E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5}
|
||||
{ABC826D4-2FA1-4F2F-87DE-E6095F653810} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA}
|
||||
{F112851D-B023-4746-B6B1-8D2E5AD8F7AA} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA}
|
||||
{6CB3EB30-F725-45C0-9742-42599BA8E8D2} = {74E681ED-FECC-4034-B9BD-01B0BB1BDECA}
|
||||
@@ -960,8 +973,8 @@ Global
|
||||
{34BE9011-A5A9-49DD-9E53-C3D5CA7D7CE3} = {3E10BF6D-ADA5-417D-B36C-EBB0660B475E}
|
||||
{AC4402A1-61C4-4229-B840-FB1777DAE10C} = {3E10BF6D-ADA5-417D-B36C-EBB0660B475E}
|
||||
{137906EA-15FE-4AD8-A6A0-27528F0477D6} = {902528F6-1444-42A3-8B75-A54B775B539C}
|
||||
{D9A7B330-CD22-4DA1-A95A-8DE1982AD8EB} = {902528F6-1444-42A3-8B75-A54B775B539C}
|
||||
{43D0EC0B-1955-4566-8D31-7B9102DA1703} = {902528F6-1444-42A3-8B75-A54B775B539C}
|
||||
{966EC390-3C7F-4D98-92A6-F0F30D02E9B1} = {902528F6-1444-42A3-8B75-A54B775B539C}
|
||||
{D9A7B330-CD22-4DA1-A95A-8DE1982AD8EB} = {902528F6-1444-42A3-8B75-A54B775B539C}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
Reference in New Issue
Block a user