Refactoring to use object resolver, instead of ProviderBase
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace Umbraco.Core.Configuration
|
||||
{
|
||||
public class ProviderFeatureSection : ConfigurationSection
|
||||
{
|
||||
private readonly ConfigurationProperty _defaultProvider = new ConfigurationProperty("defaultProvider", typeof(string), null);
|
||||
|
||||
private readonly ConfigurationProperty _providers = new ConfigurationProperty("providers", typeof(ProviderSettingsCollection), null);
|
||||
|
||||
private readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
|
||||
|
||||
public ProviderFeatureSection()
|
||||
{
|
||||
_properties.Add(_providers);
|
||||
_properties.Add(_defaultProvider);
|
||||
}
|
||||
|
||||
[ConfigurationProperty("defaultProvider")]
|
||||
public string DefaultProvider
|
||||
{
|
||||
get { return (string)base[_defaultProvider]; }
|
||||
set { base[_defaultProvider] = value; }
|
||||
}
|
||||
|
||||
[ConfigurationProperty("providers")]
|
||||
public ProviderSettingsCollection Providers
|
||||
{
|
||||
get { return (ProviderSettingsCollection)base[_providers]; }
|
||||
}
|
||||
|
||||
protected override ConfigurationPropertyCollection Properties
|
||||
{
|
||||
get { return _properties; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
using System;
|
||||
using System.Configuration.Provider;
|
||||
|
||||
namespace Umbraco.Core
|
||||
{
|
||||
public class GenericProviderCollection<TProvider> : ProviderCollection where TProvider : ProviderBase
|
||||
{
|
||||
public override void Add(ProviderBase provider)
|
||||
{
|
||||
// make sure the provider supplied is not null
|
||||
if (provider == null)
|
||||
throw new ArgumentNullException("provider");
|
||||
|
||||
if (provider as TProvider == null)
|
||||
{
|
||||
string providerTypeName = typeof(TProvider).ToString();
|
||||
throw new ArgumentException("Provider must implement provider type", providerTypeName);
|
||||
}
|
||||
|
||||
base.Add(provider);
|
||||
}
|
||||
|
||||
new public TProvider this[string name]
|
||||
{
|
||||
get
|
||||
{
|
||||
return (TProvider)base[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/Umbraco.Core/Media/IImageUrlProvider.cs
Normal file
11
src/Umbraco.Core/Media/IImageUrlProvider.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace Umbraco.Core.Media
|
||||
{
|
||||
public interface IImageUrlProvider
|
||||
{
|
||||
string Name { get; }
|
||||
string GetImageUrlFromMedia(int mediaId, NameValueCollection parameters);
|
||||
string GetImageUrlFromFileName(string specifiedSrc, NameValueCollection parameters);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using System.Collections.Specialized;
|
||||
using System.Configuration.Provider;
|
||||
|
||||
namespace Umbraco.Core.Media
|
||||
{
|
||||
public abstract class ImageUrlProviderBase : ProviderBase
|
||||
{
|
||||
public abstract string GetImageUrlFromMedia(int mediaId, NameValueCollection parameters);
|
||||
public abstract string GetImageUrlFromFileName(string specifiedSrc, NameValueCollection parameters);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Umbraco.Core.ObjectResolution
|
||||
{
|
||||
public interface IHttpContextFactory
|
||||
{
|
||||
HttpContextBase Context { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using System.Web;
|
||||
|
||||
namespace Umbraco.Core.ObjectResolution
|
||||
{
|
||||
public class WebHttpContextFactory : IHttpContextFactory
|
||||
{
|
||||
public HttpContextBase Context
|
||||
{
|
||||
get { return new HttpContextWrapper(HttpContext.Current); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.Configuration.Provider;
|
||||
using System.Web.Configuration;
|
||||
using Umbraco.Core.Configuration;
|
||||
|
||||
namespace Umbraco.Core
|
||||
{
|
||||
public abstract class ProviderFeature<TProvider> where TProvider : ProviderBase
|
||||
{
|
||||
private static bool _initialized;
|
||||
private static object _lock = new object();
|
||||
|
||||
public static TProvider Provider { get; private set; }
|
||||
|
||||
public static GenericProviderCollection<TProvider> Providers { get; private set; }
|
||||
|
||||
protected static void Initialize(string sectionName)
|
||||
{
|
||||
if (!_initialized)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_initialized)
|
||||
return;
|
||||
|
||||
var section = (ProviderFeatureSection) ConfigurationManager.GetSection(sectionName);
|
||||
if (section == null)
|
||||
throw new Exception(string.Format("{0} is not configured for this application", sectionName));
|
||||
|
||||
Providers = new GenericProviderCollection<TProvider>();
|
||||
|
||||
ProvidersHelper.InstantiateProviders(section.Providers, Providers, typeof (TProvider));
|
||||
|
||||
Provider = Providers[section.DefaultProvider];
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,6 @@
|
||||
<Compile Include="Configuration\FileSystemProviderElement.cs" />
|
||||
<Compile Include="Configuration\FileSystemProviderElementCollection.cs" />
|
||||
<Compile Include="Configuration\FileSystemProvidersSection.cs" />
|
||||
<Compile Include="Configuration\ProviderFeatureSection.cs" />
|
||||
<Compile Include="CoreBootManager.cs" />
|
||||
<Compile Include="DataTableExtensions.cs" />
|
||||
<Compile Include="DictionaryExtensions.cs" />
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ImageUrl defaultProvider="umbracoImageUrl">
|
||||
<providers>
|
||||
<clear/>
|
||||
<add name="umbracoImageUrl" type="Umbraco.Web.Media.ImageUrlProviders.ImageUrlProvider, umbraco"/>
|
||||
</providers>
|
||||
</ImageUrl>
|
||||
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<ImageUrl defaultProvider="umbracoImageUrl">
|
||||
<providers>
|
||||
<clear/>
|
||||
<add name="umbracoImageUrl" type="Umbraco.Web.Media.ImageUrlProviders.ImageUrlProvider, umbraco"/>
|
||||
</providers>
|
||||
</ImageUrl>
|
||||
@@ -14,6 +14,13 @@
|
||||
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
|
||||
</connectionStrings>
|
||||
-->
|
||||
<appSettings>
|
||||
<add xdt:Transform="Replace" xdt:Locator="Match(key)" key="umbracoDbDSN"
|
||||
value="datalayer=SQLCE4Umbraco.SqlCEHelper,SQLCE4Umbraco;data source=|DataDirectory|\Umbraco.sdf"/>
|
||||
<add xdt:Transform="Replace" xdt:Locator="Match(key)" key="umbracoConfigurationStatus"
|
||||
value="4.10.0"/>
|
||||
</appSettings>
|
||||
|
||||
<system.web>
|
||||
<compilation debug="true" xdt:Transform="SetAttributes(debug)" />
|
||||
<trust level="Medium" xdt:Transform="Remove" />
|
||||
|
||||
@@ -13,30 +13,23 @@ using umbraco;
|
||||
|
||||
namespace Umbraco.Web.Media
|
||||
{
|
||||
public class ImageUrl : ProviderFeature<ImageUrlProviderBase>
|
||||
public class ImageUrl
|
||||
{
|
||||
|
||||
private static IHttpContextFactory _contextFactory;
|
||||
|
||||
static ImageUrl()
|
||||
{
|
||||
_contextFactory = new WebHttpContextFactory();
|
||||
Initialize("ImageUrl");
|
||||
}
|
||||
|
||||
public static string GetImageUrl(string specifiedSrc, string field, string provider, string parameters, int? nodeId = null)
|
||||
public static string GetImageUrl(string specifiedSrc, string field, string provider, string parameters,
|
||||
int? nodeId = null)
|
||||
{
|
||||
string url;
|
||||
ImageUrlProviderBase p = GetProvider(provider);
|
||||
IImageUrlProvider p = GetProvider(provider);
|
||||
|
||||
NameValueCollection parsedParameters = string.IsNullOrEmpty(parameters)?
|
||||
new NameValueCollection() :
|
||||
HttpUtility.ParseQueryString(parameters);
|
||||
NameValueCollection parsedParameters = string.IsNullOrEmpty(parameters)
|
||||
? new NameValueCollection()
|
||||
: HttpUtility.ParseQueryString(parameters);
|
||||
|
||||
string fieldValue = string.Empty;
|
||||
if (!string.IsNullOrEmpty(field))
|
||||
{
|
||||
if(nodeId.HasValue)
|
||||
string fieldValue = string.Empty;
|
||||
if (nodeId.HasValue)
|
||||
{
|
||||
var contentFromCache = GetContentFromCache(nodeId.GetValueOrDefault(), field);
|
||||
if (contentFromCache != null)
|
||||
@@ -45,17 +38,23 @@ namespace Umbraco.Web.Media
|
||||
}
|
||||
else
|
||||
{
|
||||
page itemPage =new page(content.Instance.XmlContent.GetElementById(nodeId.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)));
|
||||
var itemPage = new page(content.Instance.XmlContent.GetElementById(nodeId.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)));
|
||||
var value = itemPage.Elements[field];
|
||||
fieldValue = value != null ? value.ToString() : string.Empty;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var context = _contextFactory.Context;
|
||||
Hashtable elements = context.Items["pageElements"] as Hashtable;
|
||||
var value = elements[field];
|
||||
fieldValue = value != null ? value.ToString() : string.Empty;
|
||||
var context = HttpContext.Current;
|
||||
if (context != null)
|
||||
{
|
||||
var elements = context.Items["pageElements"] as Hashtable;
|
||||
if (elements != null)
|
||||
{
|
||||
var value = elements[field];
|
||||
fieldValue = value != null ? value.ToString() : string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
int mediaId;
|
||||
if (int.TryParse(fieldValue, out mediaId))
|
||||
@@ -66,7 +65,7 @@ namespace Umbraco.Web.Media
|
||||
else
|
||||
{
|
||||
//assume file path
|
||||
url = p.GetImageUrlFromFileName(fieldValue, parsedParameters);
|
||||
url = p.GetImageUrlFromFileName(fieldValue, parsedParameters);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -77,18 +76,17 @@ namespace Umbraco.Web.Media
|
||||
return url;
|
||||
}
|
||||
|
||||
private static ImageUrlProviderBase GetProvider(string provider)
|
||||
private static IImageUrlProvider GetProvider(string provider)
|
||||
{
|
||||
return string.IsNullOrEmpty(provider) ?
|
||||
Provider :
|
||||
Providers[provider];
|
||||
return ImageUrlProviderResolver.Current.Provider(provider);
|
||||
}
|
||||
|
||||
private static object GetContentFromCache(int nodeIdInt, string field)
|
||||
{
|
||||
object content = _contextFactory.Context.Cache[String.Format("contentItem{0}_{1}", nodeIdInt.ToString(CultureInfo.InvariantCulture), field)];
|
||||
object content =
|
||||
ContextFactory.Context.Cache[
|
||||
String.Format("contentItem{0}_{1}", nodeIdInt.ToString(CultureInfo.InvariantCulture), field)];
|
||||
return content;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
19
src/Umbraco.Web/Media/ImageUrlProviderResolver.cs
Normal file
19
src/Umbraco.Web/Media/ImageUrlProviderResolver.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Media;
|
||||
using Umbraco.Core.ObjectResolution;
|
||||
using Umbraco.Web.Media.ImageUrlProviders;
|
||||
|
||||
namespace Umbraco.Web.Media
|
||||
{
|
||||
internal sealed class ImageUrlProviderResolver : ManyObjectsResolverBase<ImageUrlProviderResolver, IImageUrlProvider>
|
||||
{
|
||||
internal ImageUrlProviderResolver(IEnumerable<Type> value) : base(value) { }
|
||||
|
||||
public IImageUrlProvider Provider(string provider)
|
||||
{
|
||||
return string.IsNullOrEmpty(provider) ? Values.First(v => v.Name == ImageUrlProvider.DefaultName) : Values.First(v => v.Name == provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,15 @@ using umbraco;
|
||||
|
||||
namespace Umbraco.Web.Media.ImageUrlProviders
|
||||
{
|
||||
public class ImageUrlProvider : ImageUrlProviderBase
|
||||
public class ImageUrlProvider : IImageUrlProvider
|
||||
{
|
||||
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
|
||||
public const string DefaultName = "umbracoUpload";
|
||||
public string Name
|
||||
{
|
||||
//Get default values from the provider config here?
|
||||
base.Initialize(name, config);
|
||||
get { return DefaultName; }
|
||||
}
|
||||
|
||||
public override string GetImageUrlFromMedia(int mediaId, NameValueCollection parameters)
|
||||
public string GetImageUrlFromMedia(int mediaId, NameValueCollection parameters)
|
||||
{
|
||||
string url = string.Empty;
|
||||
var nodeIterator = library.GetMedia(mediaId, false);
|
||||
@@ -27,7 +27,7 @@ namespace Umbraco.Web.Media.ImageUrlProviders
|
||||
return url;
|
||||
}
|
||||
|
||||
public override string GetImageUrlFromFileName(string specifiedSrc, NameValueCollection parameters)
|
||||
public string GetImageUrlFromFileName(string specifiedSrc, NameValueCollection parameters)
|
||||
{
|
||||
string withThumb = addThumbInfo(specifiedSrc, parameters);
|
||||
return addCropInfo(withThumb, parameters);
|
||||
|
||||
@@ -69,5 +69,16 @@ namespace Umbraco.Web
|
||||
{
|
||||
return resolver.ResolveTypes<IThumbnailProvider>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all IImageUrlProvider classes
|
||||
/// </summary>
|
||||
/// <param name="resolver"></param>
|
||||
/// <returns></returns>
|
||||
internal static IEnumerable<Type> ResolveImageUrlProviders(this PluginManager resolver)
|
||||
{
|
||||
return resolver.ResolveTypes<IImageUrlProvider>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using Umbraco.Core.Dictionary;
|
||||
using Umbraco.Core.Dynamics;
|
||||
using Umbraco.Core.PropertyEditors;
|
||||
using Umbraco.Web.Dictionary;
|
||||
using Umbraco.Web.Media;
|
||||
using Umbraco.Web.Media.ThumbnailProviders;
|
||||
using Umbraco.Web.Models;
|
||||
using Umbraco.Web.Mvc;
|
||||
@@ -239,6 +240,9 @@ namespace Umbraco.Web
|
||||
ThumbnailProvidersResolver.Current = new ThumbnailProvidersResolver(
|
||||
PluginManager.Current.ResolveThumbnailProviders());
|
||||
|
||||
ImageUrlProviderResolver.Current = new ImageUrlProviderResolver(
|
||||
PluginManager.Current.ResolveImageUrlProviders());
|
||||
|
||||
CultureDictionaryFactoryResolver.Current = new CultureDictionaryFactoryResolver(
|
||||
new DefaultCultureDictionaryFactory());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user