mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 03:25:23 +08:00
Using AzureFileSystem in AzureShellSettingsManager
--HG-- branch : dev
This commit is contained in:
@@ -28,8 +28,8 @@ namespace Orchard.Azure {
|
||||
// Setup the connection to custom storage accountm, e.g. Development Storage
|
||||
_storageAccount = storageAccount;
|
||||
ContainerName = containerName;
|
||||
_root = String.IsNullOrEmpty(root) || root == "/" ? String.Empty : root + "/";
|
||||
_absoluteRoot = _storageAccount.BlobEndpoint.AbsoluteUri + containerName + "/" + root + "/";
|
||||
_root = String.IsNullOrEmpty(root) ? "": root + "/";
|
||||
_absoluteRoot = _storageAccount.BlobEndpoint.AbsoluteUri + "/" + containerName + "/" + root;
|
||||
|
||||
using ( new HttpContextWeaver() ) {
|
||||
|
||||
@@ -40,12 +40,11 @@ namespace Orchard.Azure {
|
||||
|
||||
Container.CreateIfNotExist();
|
||||
|
||||
if (isPrivate) {
|
||||
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off });
|
||||
}
|
||||
else {
|
||||
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
|
||||
}
|
||||
Container.SetPermissions(isPrivate
|
||||
? new BlobContainerPermissions
|
||||
{PublicAccess = BlobContainerPublicAccessType.Off}
|
||||
: new BlobContainerPermissions
|
||||
{PublicAccess = BlobContainerPublicAccessType.Container});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -107,7 +106,7 @@ namespace Orchard.Azure {
|
||||
|
||||
EnsurePathIsRelative(path);
|
||||
|
||||
string prefix = String.Concat(Combine(Container.Name, _root), path);
|
||||
string prefix = Combine(Combine(Container.Name, _root), path);
|
||||
|
||||
if ( !prefix.EndsWith("/") )
|
||||
prefix += "/";
|
||||
@@ -128,7 +127,16 @@ namespace Orchard.Azure {
|
||||
|
||||
EnsurePathIsRelative(path);
|
||||
using ( new HttpContextWeaver() ) {
|
||||
if ( !Container.DirectoryExists(String.Concat(_root, 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(String.Concat(_root, path));
|
||||
}
|
||||
@@ -146,14 +154,12 @@ namespace Orchard.Azure {
|
||||
}
|
||||
}
|
||||
|
||||
public void TryCreateFolder(string path)
|
||||
{
|
||||
public void TryCreateFolder(string path) {
|
||||
EnsurePathIsRelative(path);
|
||||
CreateFile(Combine(path, FolderEntry));
|
||||
}
|
||||
|
||||
public void CreateFolder(string path)
|
||||
{
|
||||
public void CreateFolder(string path) {
|
||||
EnsurePathIsRelative(path);
|
||||
using (new HttpContextWeaver()) {
|
||||
Container.EnsureDirectoryDoesNotExist(String.Concat(_root, path));
|
||||
|
@@ -2,10 +2,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.WindowsAzure;
|
||||
using Microsoft.WindowsAzure.ServiceRuntime;
|
||||
using Microsoft.WindowsAzure.StorageClient;
|
||||
using Orchard.Environment.Configuration;
|
||||
using Orchard.Localization;
|
||||
|
||||
@@ -13,97 +11,68 @@ namespace Orchard.Azure.Environment.Configuration {
|
||||
|
||||
public class AzureShellSettingsManager : IShellSettingsManager {
|
||||
public const string ContainerName = "sites"; // container names must be lower cased
|
||||
public const string SettingsFilename = "Settings.txt";
|
||||
|
||||
private readonly IShellSettingsManagerEventHandler _events;
|
||||
private readonly AzureFileSystem _fileSystem;
|
||||
|
||||
private readonly CloudStorageAccount _storageAccount;
|
||||
public CloudBlobClient BlobClient { get; private set; }
|
||||
public CloudBlobContainer Container { get; private set; }
|
||||
|
||||
Localizer T { get; [UsedImplicitly]
|
||||
set; }
|
||||
Localizer T { get; set; }
|
||||
|
||||
public AzureShellSettingsManager(IShellSettingsManagerEventHandler events)
|
||||
: this(CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")), events)
|
||||
{
|
||||
}
|
||||
: this(CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")), events) {}
|
||||
|
||||
public AzureShellSettingsManager(CloudStorageAccount storageAccount, IShellSettingsManagerEventHandler events)
|
||||
{
|
||||
// Setup the connection to custom storage accountm, e.g. Development Storage
|
||||
_storageAccount = storageAccount;
|
||||
public AzureShellSettingsManager(CloudStorageAccount storageAccount, IShellSettingsManagerEventHandler events) {
|
||||
_events = events;
|
||||
|
||||
using ( new HttpContextWeaver() ) {
|
||||
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 = new CloudBlobContainer(ContainerName, BlobClient);
|
||||
Container.CreateIfNotExist();
|
||||
|
||||
// Tenant settings are protected by default
|
||||
Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off });
|
||||
}
|
||||
|
||||
_fileSystem = new AzureFileSystem(ContainerName, String.Empty, true, storageAccount);
|
||||
}
|
||||
|
||||
IEnumerable<ShellSettings> IShellSettingsManager.LoadSettings() {
|
||||
return LoadSettings().ToArray();
|
||||
var settings = LoadSettings().ToArray();
|
||||
return settings;
|
||||
}
|
||||
|
||||
void IShellSettingsManager.SaveSettings(ShellSettings settings) {
|
||||
if ( settings == null )
|
||||
throw new ArgumentException(T("There are no settings to save.").ToString());
|
||||
|
||||
if ( string.IsNullOrEmpty(settings.Name) )
|
||||
throw new ArgumentException(T("Settings \"Name\" is not set.").ToString());
|
||||
var content = ComposeSettings(settings);
|
||||
var filePath = String.Concat(settings.Name, "/", SettingsFilename);
|
||||
var file = _fileSystem.CreateFile(filePath);
|
||||
|
||||
using ( new HttpContextWeaver() ) {
|
||||
var filePath = String.Concat(settings.Name, "/", "Settings.txt");
|
||||
var blob = Container.GetBlockBlobReference(filePath);
|
||||
blob.UploadText(ComposeSettings(settings));
|
||||
using (var stream = file.OpenWrite()) {
|
||||
using (var writer = new StreamWriter(stream)) {
|
||||
writer.Write(content);
|
||||
}
|
||||
}
|
||||
|
||||
_events.Saved(settings);
|
||||
}
|
||||
|
||||
IEnumerable<ShellSettings> LoadSettings() {
|
||||
foreach (var folder in _fileSystem.ListFolders(null))
|
||||
foreach (var file in _fileSystem.ListFiles(folder.GetPath())) {
|
||||
if (!String.Equals(file.GetName(), SettingsFilename))
|
||||
continue;
|
||||
|
||||
using ( new HttpContextWeaver() ) {
|
||||
var settingsBlobs =
|
||||
BlobClient.ListBlobsWithPrefix(Container.Name + "/").OfType<CloudBlobDirectory>()
|
||||
.SelectMany(directory => directory.ListBlobs()).OfType<CloudBlockBlob>()
|
||||
.Where(
|
||||
blob =>
|
||||
string.Equals(Path.GetFileName(blob.Uri.ToString()),
|
||||
"Settings.txt",
|
||||
StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
return settingsBlobs.Select(settingsBlob => ParseSettings(settingsBlob.DownloadText())).ToList();
|
||||
}
|
||||
using (var stream = file.OpenRead())
|
||||
using (var reader = new StreamReader(stream))
|
||||
yield return ParseSettings(reader.ReadToEnd());
|
||||
}
|
||||
}
|
||||
|
||||
static ShellSettings ParseSettings(string text)
|
||||
{
|
||||
static ShellSettings ParseSettings(string text) {
|
||||
var shellSettings = new ShellSettings();
|
||||
if (String.IsNullOrEmpty(text))
|
||||
return shellSettings;
|
||||
|
||||
string[] settings = text.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var setting in settings)
|
||||
{
|
||||
string[] settingFields = setting.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
string[] settings = text.Split(new[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var setting in settings) {
|
||||
string[] settingFields = setting.Split(new[] {":"}, StringSplitOptions.RemoveEmptyEntries);
|
||||
int fieldsLength = settingFields.Length;
|
||||
if (fieldsLength != 2)
|
||||
continue;
|
||||
for (int i = 0; i < fieldsLength; i++)
|
||||
{
|
||||
for (int i = 0; i < fieldsLength; i++) {
|
||||
settingFields[i] = settingFields[i].Trim();
|
||||
}
|
||||
if (settingFields[1] != "null")
|
||||
{
|
||||
switch (settingFields[0])
|
||||
{
|
||||
if (settingFields[1] != "null") {
|
||||
switch (settingFields[0]) {
|
||||
case "Name":
|
||||
shellSettings.Name = settingFields[1];
|
||||
break;
|
||||
@@ -140,23 +109,22 @@ namespace Orchard.Azure.Environment.Configuration {
|
||||
return shellSettings;
|
||||
}
|
||||
|
||||
static string ComposeSettings(ShellSettings settings)
|
||||
{
|
||||
static string ComposeSettings(ShellSettings settings) {
|
||||
if (settings == null)
|
||||
return "";
|
||||
|
||||
return string.Format("Name: {0}\r\nDataProvider: {1}\r\nDataConnectionString: {2}\r\nDataPrefix: {3}\r\nRequestUrlHost: {4}\r\nRequestUrlPrefix: {5}\r\nState: {6}\r\nEncryptionAlgorithm: {7}\r\nEncryptionKey: {8}\r\nEncryptionIV: {9}\r\n",
|
||||
settings.Name,
|
||||
settings.DataProvider,
|
||||
settings.DataConnectionString ?? "null",
|
||||
settings.DataTablePrefix ?? "null",
|
||||
settings.RequestUrlHost ?? "null",
|
||||
settings.RequestUrlPrefix ?? "null",
|
||||
settings.State != null ? settings.State.ToString() : String.Empty,
|
||||
settings.EncryptionAlgorithm ?? "null",
|
||||
settings.EncryptionKey ?? "null",
|
||||
settings.EncryptionIV ?? "null"
|
||||
);
|
||||
settings.Name,
|
||||
settings.DataProvider,
|
||||
settings.DataConnectionString ?? "null",
|
||||
settings.DataTablePrefix ?? "null",
|
||||
settings.RequestUrlHost ?? "null",
|
||||
settings.RequestUrlPrefix ?? "null",
|
||||
settings.State != null ? settings.State.ToString() : String.Empty,
|
||||
settings.EncryptionAlgorithm ?? "null",
|
||||
settings.EncryptionKey ?? "null",
|
||||
settings.EncryptionIV ?? "null"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
108
src/Orchard.Azure/Orchard.Azure.5.0.ReSharper
Normal file
108
src/Orchard.Azure/Orchard.Azure.5.0.ReSharper
Normal file
@@ -0,0 +1,108 @@
|
||||
<Configuration>
|
||||
<CodeStyleSettings>
|
||||
<ExternalPath IsNull="False">
|
||||
</ExternalPath>
|
||||
<Sharing>SOLUTION</Sharing>
|
||||
<CSharp>
|
||||
<FormatSettings>
|
||||
<ALIGN_MULTILINE_ARRAY_AND_OBJECT_INITIALIZER>False</ALIGN_MULTILINE_ARRAY_AND_OBJECT_INITIALIZER>
|
||||
<ALIGN_MULTILINE_FOR_STMT>False</ALIGN_MULTILINE_FOR_STMT>
|
||||
<ALIGN_MULTIPLE_DECLARATION>False</ALIGN_MULTIPLE_DECLARATION>
|
||||
<ANONYMOUS_METHOD_DECLARATION_BRACES>END_OF_LINE</ANONYMOUS_METHOD_DECLARATION_BRACES>
|
||||
<CASE_BLOCK_BRACES>END_OF_LINE</CASE_BLOCK_BRACES>
|
||||
<EMPTY_BLOCK_STYLE>TOGETHER</EMPTY_BLOCK_STYLE>
|
||||
<FORCE_FIXED_BRACES_STYLE>ALWAYS_ADD</FORCE_FIXED_BRACES_STYLE>
|
||||
<FORCE_FOR_BRACES_STYLE>ALWAYS_ADD</FORCE_FOR_BRACES_STYLE>
|
||||
<FORCE_FOREACH_BRACES_STYLE>ALWAYS_ADD</FORCE_FOREACH_BRACES_STYLE>
|
||||
<FORCE_IFELSE_BRACES_STYLE>ALWAYS_ADD</FORCE_IFELSE_BRACES_STYLE>
|
||||
<FORCE_USING_BRACES_STYLE>ALWAYS_ADD</FORCE_USING_BRACES_STYLE>
|
||||
<FORCE_WHILE_BRACES_STYLE>ALWAYS_ADD</FORCE_WHILE_BRACES_STYLE>
|
||||
<INITIALIZER_BRACES>END_OF_LINE</INITIALIZER_BRACES>
|
||||
<INVOCABLE_DECLARATION_BRACES>END_OF_LINE</INVOCABLE_DECLARATION_BRACES>
|
||||
<MODIFIERS_ORDER IsNull="False">
|
||||
<Item>public</Item>
|
||||
<Item>protected</Item>
|
||||
<Item>internal</Item>
|
||||
<Item>private</Item>
|
||||
<Item>new</Item>
|
||||
<Item>abstract</Item>
|
||||
<Item>virtual</Item>
|
||||
<Item>override</Item>
|
||||
<Item>sealed</Item>
|
||||
<Item>static</Item>
|
||||
<Item>readonly</Item>
|
||||
<Item>extern</Item>
|
||||
<Item>unsafe</Item>
|
||||
<Item>volatile</Item>
|
||||
</MODIFIERS_ORDER>
|
||||
<OTHER_BRACES>END_OF_LINE</OTHER_BRACES>
|
||||
<TYPE_DECLARATION_BRACES>END_OF_LINE</TYPE_DECLARATION_BRACES>
|
||||
<WRAP_LINES>False</WRAP_LINES>
|
||||
</FormatSettings>
|
||||
<UsingsSettings />
|
||||
<Naming2>
|
||||
<EventHandlerPatternLong>$object$_On$event$</EventHandlerPatternLong>
|
||||
<EventHandlerPatternShort>$event$Handler</EventHandlerPatternShort>
|
||||
<ExceptionName IsNull="False">
|
||||
</ExceptionName>
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="TypesAndNamespaces" />
|
||||
<PredefinedRule Inspect="True" Prefix="I" Suffix="" Style="AaBb" ElementKind="Interfaces" />
|
||||
<PredefinedRule Inspect="True" Prefix="T" Suffix="" Style="AaBb" ElementKind="TypeParameters" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="MethodPropertyEvent" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Locals" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="LocalConstants" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Parameters" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PublicFields" />
|
||||
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateInstanceFields" />
|
||||
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateStaticFields" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Constants" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateConstants" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="StaticReadonly" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateStaticReadonly" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="EnumMember" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Other" />
|
||||
</Naming2>
|
||||
</CSharp>
|
||||
<VB>
|
||||
<FormatSettings />
|
||||
<ImportsSettings />
|
||||
<Naming2>
|
||||
<EventHandlerPatternLong>$object$_On$event$</EventHandlerPatternLong>
|
||||
<EventHandlerPatternShort>$event$Handler</EventHandlerPatternShort>
|
||||
</Naming2>
|
||||
</VB>
|
||||
<Web>
|
||||
<Naming2 />
|
||||
</Web>
|
||||
<Xaml>
|
||||
<Naming2 />
|
||||
</Xaml>
|
||||
<XML>
|
||||
<FormatSettings />
|
||||
</XML>
|
||||
<GenerateMemberBody />
|
||||
<Naming2>
|
||||
<EventHandlerPatternLong>$object$_On$event$</EventHandlerPatternLong>
|
||||
<EventHandlerPatternShort>$event$Handler</EventHandlerPatternShort>
|
||||
<ExceptionName IsNull="False">
|
||||
</ExceptionName>
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="TypesAndNamespaces" />
|
||||
<PredefinedRule Inspect="True" Prefix="I" Suffix="" Style="AaBb" ElementKind="Interfaces" />
|
||||
<PredefinedRule Inspect="True" Prefix="T" Suffix="" Style="AaBb" ElementKind="TypeParameters" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="MethodPropertyEvent" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Locals" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="LocalConstants" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Parameters" />
|
||||
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PublicFields" />
|
||||
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateInstanceFields" />
|
||||
<PredefinedRule Inspect="True" Prefix="_" Suffix="" Style="aaBb" ElementKind="PrivateStaticFields" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Constants" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateConstants" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="StaticReadonly" />
|
||||
<PredefinedRule Inspect="False" Prefix="" Suffix="" Style="AaBb" ElementKind="PrivateStaticReadonly" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="EnumMember" />
|
||||
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Other" />
|
||||
<Abbreviation Text="SQ" />
|
||||
</Naming2>
|
||||
</CodeStyleSettings>
|
||||
</Configuration>
|
@@ -1,6 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ServiceDefinition name="OrchardCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
|
||||
<WebRole name="Orchard.Azure.Web">
|
||||
<!--<Sites>
|
||||
<Site name="Web">
|
||||
<Bindings>
|
||||
<Binding name="HttpIn" endpointName="HttpIn" />
|
||||
</Bindings>
|
||||
</Site>
|
||||
</Sites>-->
|
||||
<ConfigurationSettings>
|
||||
<Setting name="DiagnosticsConnectionString" />
|
||||
<Setting name="DataConnectionString" />
|
||||
|
Reference in New Issue
Block a user