Added FileSystemProviders config section

Added FileSystemProviderManager class for fetching a file system provider by alias (this should probably be moved into the Umbraco context in 4.10)
This commit is contained in:
Matt@MBP13-PC
2012-08-13 13:26:06 -01:00
parent ad291265f3
commit 524244d1ec
10 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace Umbraco.Core.Configuration
{
public class FileSystemProviderElement : ConfigurationElement
{
private const string ALIAS_KEY = "alias";
private const string TYPE_KEY = "type";
private const string PARAMETERS_KEY = "Parameters";
[ConfigurationProperty(ALIAS_KEY, IsKey = true, IsRequired = true)]
public string Alias
{
get
{
return ((string)(base[ALIAS_KEY]));
}
}
[ConfigurationProperty(TYPE_KEY, IsKey = false, IsRequired = true)]
public string Type
{
get
{
return ((string)(base[TYPE_KEY]));
}
}
[ConfigurationProperty(PARAMETERS_KEY, IsDefaultCollection = true, IsRequired = false)]
public KeyValueConfigurationCollection Parameters
{
get
{
return ((KeyValueConfigurationCollection)(base[PARAMETERS_KEY]));
}
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace Umbraco.Core.Configuration
{
[ConfigurationCollection(typeof(FileSystemProviderElement), AddItemName = "Provider")]
public class FileSystemProviderElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FileSystemProviderElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((FileSystemProviderElement)(element)).Alias;
}
new public FileSystemProviderElement this[string key]
{
get
{
return (FileSystemProviderElement)BaseGet(key);
}
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace Umbraco.Core.Configuration
{
public class FileSystemProvidersSection : ConfigurationSection
{
private const string PROVIDERS_KEY = "providers";
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public FileSystemProviderElementCollection Providers
{
get { return ((FileSystemProviderElementCollection)(base[""])); }
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Umbraco.Core.IO
{
internal class FileSystemProvider
{
public const string Media = "media";
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Umbraco.Core.Configuration;
namespace Umbraco.Core.IO
{
internal class FileSystemProviderManager
{
private readonly FileSystemProvidersSection _config;
#region Singleton
private static readonly FileSystemProviderManager Instance = new FileSystemProviderManager();
public static FileSystemProviderManager Current
{
get { return Instance; }
}
#endregion
#region Constructors
public FileSystemProviderManager()
{
_config = (FileSystemProvidersSection)ConfigurationManager.GetSection("FileSystemProviders");
}
#endregion
public IFileSystem GetFileSystemProvider(string alias)
{
var providerConfig = _config.Providers[alias];
if(providerConfig == null)
throw new ArgumentException(string.Format("No provider found with the alias '{0}'", alias));
var providerType = Type.GetType(providerConfig.Type);
if(providerType == null)
throw new InvalidOperationException(string.Format("Could not find type '{0}'", providerConfig.Type));
if (providerType.IsAssignableFrom(typeof(IFileSystem)))
throw new InvalidOperationException(string.Format("The type '{0}' does not implement IFileSystem", providerConfig.Type));
var paramCount = providerConfig.Parameters != null ? providerConfig.Parameters.Count : 0;
var constructor = providerType.GetConstructors()
.SingleOrDefault(x => x.GetParameters().Count() == paramCount
&& x.GetParameters().All(y => providerConfig.Parameters.AllKeys.Contains(y.Name)));
if(constructor == null)
throw new InvalidOperationException(string.Format("Could not find constructor for type '{0}' which accepts {1} parameters", providerConfig.Type, paramCount));
var parameters = new object[paramCount];
for(var i = 0; i < paramCount; i++)
parameters[i] = providerConfig.Parameters[providerConfig.Parameters.AllKeys[i]].Value;
return (IFileSystem) constructor.Invoke(parameters);
}
}
}

View File

@@ -32,6 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
@@ -44,9 +45,14 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Configuration\FileSystemProviderElement.cs" />
<Compile Include="Configuration\FileSystemProviderElementCollection.cs" />
<Compile Include="Configuration\FileSystemProvidersSection.cs" />
<Compile Include="DelegateEqualityComparer.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="IfExtensions.cs" />
<Compile Include="IO\FileSystemProvider.cs" />
<Compile Include="IO\FileSystemProviderManager.cs" />
<Compile Include="IO\IFileSystem.cs" />
<Compile Include="IO\PhysicalFileSystem.cs" />
<Compile Include="IThumbnailProvider.cs" />

View File

@@ -264,6 +264,10 @@
<None Include="config\ClientDependency.Release.config">
<DependentUpon>ClientDependency.config</DependentUpon>
</None>
<Content Include="config\FileSystemProviders.config" />
<Content Include="config\FileSystemProviders.Release.config">
<DependentUpon>FileSystemProviders.config</DependentUpon>
</Content>
<None Include="config\xsltExtensions.Release.config">
<DependentUpon>xsltExtensions.config</DependentUpon>
</None>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<FileSystemProviders>
<!-- Media -->
<Provider alias="media" type="Umbraco.Core.IO.PhysicalFileSystem, Umbraco.Core">
<Parameters>
<add key="virtualRoot" value="~/Media/" />
</Parameters>
</Provider>
</FileSystemProviders>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<FileSystemProviders>
<!-- Media -->
<Provider alias="media" type="Umbraco.Core.IO.PhysicalFileSystem, Umbraco.Core">
<Parameters>
<add key="virtualRoot" value="~/Media/" />
</Parameters>
</Provider>
</FileSystemProviders>

View File

@@ -11,6 +11,7 @@
<section name="clientDependency" type="ClientDependency.Core.Config.ClientDependencySection, ClientDependency.Core" requirePermission="false"/>
<section name="Examine" type="Examine.Config.ExamineSettings, Examine" requirePermission="false"/>
<section name="ExamineLuceneIndexSets" type="UmbracoExamine.Config.ExamineLuceneIndexes, UmbracoExamine" requirePermission="false"/>
<section name="FileSystemProviders" type="Umbraco.Core.Configuration.FileSystemProvidersSection, Umbraco.Core" requirePermission="false"/>
<!-- Added in Umbraco 4.6.2 -->
<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">
@@ -19,11 +20,13 @@
</sectionGroup>
<!-- End of added in Umbraco 4.6.2 -->
</configSections>
<urlrewritingnet configSource="config\UrlRewriting.config" />
<microsoft.scripting configSource="config\scripting.config" />
<clientDependency configSource="config\ClientDependency.config" />
<Examine configSource="config\ExamineSettings.config" />
<ExamineLuceneIndexSets configSource="config\ExamineIndex.config" />
<FileSystemProviders configSource="config\FileSystemProviders.config" />
<appSettings>
<add key="umbracoDbDSN" value="" />