Skip to content

Commit

Permalink
Refactor based from Lars comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Buckley committed Jan 15, 2019
1 parent 493cafb commit a121412
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/UmbracoFileSystemProviders.Azure/AzureBlobFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public class AzureBlobFileSystem : IFileSystem
/// </summary>
private const string UsePrivateContainerKey = Constants.Configuration.UsePrivateContainer;


private AzureBlobFileSystemConfig config;

public AzureBlobFileSystem(AzureBlobFileSystemConfig config)
{
this.config = config;
this.FileSystem = AzureFileSystem.GetInstance(config.ContainerName, config.RootUrl, config.ConnectionString, config.MaxDays, config.UseDefaultRoute, config.UsePrivateContainer);
}

/// <summary>
/// Initializes a new instance of the <see cref="AzureBlobFileSystem"/> class.
/// </summary>
Expand Down
17 changes: 5 additions & 12 deletions src/UmbracoFileSystemProviders.Azure/AzureFileSystemComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@

namespace Our.Umbraco.FileSystemProviders.Azure
{
using System;
using System.Configuration;
using global::Umbraco.Core.Components;
using global::Umbraco.Core.IO;

public class AzureFileSystemComponent : IComponent
{
/// <summary>
/// The configuration key for disabling the virtual path provider.
/// </summary>
private const string DisableVirtualPathProviderKey = Constants.Configuration.DisableVirtualPathProviderKey;

private readonly SupportingFileSystems supportingFileSystems;
private readonly AzureBlobFileSystemConfig config;

public AzureFileSystemComponent(SupportingFileSystems supportingFileSystems)
public AzureFileSystemComponent(SupportingFileSystems supportingFileSystems, AzureBlobFileSystemConfig config)
{
this.supportingFileSystems = supportingFileSystems;
this.config = config;
}

public void Initialize()
{
bool disable = ConfigurationManager.AppSettings[DisableVirtualPathProviderKey] != null
&& ConfigurationManager.AppSettings[DisableVirtualPathProviderKey]
.Equals("true", StringComparison.InvariantCultureIgnoreCase);

var azureFs = this.supportingFileSystems.For<IMediaFileSystem>() as AzureBlobFileSystem;
if (!disable && azureFs != null)
if (!this.config.DisableVirtualPathProvider && azureFs != null)
{
AzureFileSystem azureFileSystem = azureFs.FileSystem;

Expand Down
36 changes: 32 additions & 4 deletions src/UmbracoFileSystemProviders.Azure/AzureFileSystemComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,34 @@
using System.Configuration;
using global::Umbraco.Core;
using global::Umbraco.Core.Components;
using global::Umbraco.Core.Composing;
using global::Umbraco.Core.Exceptions;
using global::Umbraco.Core.IO;

[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class AzureFileSystemComposer : IComposer
{
public void Compose(Composition composition)
{
//Configuration
var config = CreateConfiguration();

//Reads config from AppSetting keys
composition.RegisterUnique(config);

//Set the Media FS to use our Azure FS with our config from AppSettings
composition.SetMediaFileSystem(_ => new AzureBlobFileSystem(config));

//Register component that deals with the VirtualPathProvider
composition.Components().Append<AzureFileSystemComponent>();
}

private AzureBlobFileSystemConfig CreateConfiguration()
{
var containerName = ConfigurationManager.AppSettings[Constants.Configuration.ContainerNameKey];
var rootUrl = ConfigurationManager.AppSettings[Constants.Configuration.RootUrlKey];
var connectionString = ConfigurationManager.AppSettings[Constants.Configuration.ConnectionStringKey];
var maxDays = ConfigurationManager.AppSettings[Constants.Configuration.MaxDaysKey];
var useDefaultRoute = ConfigurationManager.AppSettings[Constants.Configuration.UseDefaultRouteKey];
var useDefaultRoute = ConfigurationManager.AppSettings[Constants.Configuration.UseDefaultRouteKey];
var usePrivateContainer = ConfigurationManager.AppSettings[Constants.Configuration.UsePrivateContainer];

//Check we have all values set - otherwise make sure Umbraco does NOT boot so it can be configured correctly
Expand All @@ -38,8 +53,21 @@ public void Compose(Composition composition)
if (string.IsNullOrEmpty(usePrivateContainer))
throw new ArgumentNullOrEmptyException("usePrivateContainer", $"The Azure File System is missing the value '{Constants.Configuration.UsePrivateContainer}' from AppSettings");

composition.SetMediaFileSystem(_ => new AzureBlobFileSystem(containerName, rootUrl, connectionString, maxDays, useDefaultRoute, usePrivateContainer));
composition.Components().Append<AzureFileSystemComponent>();
bool disableVirtualPathProvider = ConfigurationManager.AppSettings[Constants.Configuration.DisableVirtualPathProviderKey] != null
&& ConfigurationManager.AppSettings[Constants.Configuration.DisableVirtualPathProviderKey]
.Equals("true", StringComparison.InvariantCultureIgnoreCase);

return new AzureBlobFileSystemConfig
{
DisableVirtualPathProvider = disableVirtualPathProvider,
ContainerName = containerName,
RootUrl = rootUrl,
ConnectionString = connectionString,
MaxDays = maxDays,
UseDefaultRoute = useDefaultRoute,
UsePrivateContainer = usePrivateContainer
};
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AzureBlobFileSystem.cs" />
<Compile Include="AzureBlobFileSystemConfig.cs" />
<Compile Include="AzureFileSystem.cs" />
<Compile Include="AzureFileSystemComponent.cs" />
<Compile Include="AzureFileSystemComposer.cs" />
Expand Down

1 comment on commit a121412

@lars-erik
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good. :)
I'd prefer two different config objects though, but good enough for now. I'm sure we'll polish a bit before ultimate launch.

Please sign in to comment.