diff --git a/src/Umbraco.Core/Configuration/ProviderFeatureSection.cs b/src/Umbraco.Core/Configuration/ProviderFeatureSection.cs new file mode 100644 index 0000000000..59d50f664f --- /dev/null +++ b/src/Umbraco.Core/Configuration/ProviderFeatureSection.cs @@ -0,0 +1,37 @@ +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; } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/GenericProviderCollection.cs b/src/Umbraco.Core/GenericProviderCollection.cs new file mode 100644 index 0000000000..82bd6b47be --- /dev/null +++ b/src/Umbraco.Core/GenericProviderCollection.cs @@ -0,0 +1,31 @@ +using System; +using System.Configuration.Provider; + +namespace Umbraco.Core +{ + public class GenericProviderCollection : 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]; + } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Media/ImageUrlProviderBase.cs b/src/Umbraco.Core/Media/ImageUrlProviderBase.cs new file mode 100644 index 0000000000..df0b13e339 --- /dev/null +++ b/src/Umbraco.Core/Media/ImageUrlProviderBase.cs @@ -0,0 +1,10 @@ +using System.Configuration.Provider; + +namespace Umbraco.Core.Media +{ + public abstract class ImageUrlProviderBase : ProviderBase + { + public abstract string GetImageUrlFromMedia(int mediaId); + public abstract string GetImageUrlFromFileName(string specifiedSrc); + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/ObjectResolution/IHttpContextFactory.cs b/src/Umbraco.Core/ObjectResolution/IHttpContextFactory.cs new file mode 100644 index 0000000000..3095c78516 --- /dev/null +++ b/src/Umbraco.Core/ObjectResolution/IHttpContextFactory.cs @@ -0,0 +1,9 @@ +using System.Web; + +namespace Umbraco.Core.ObjectResolution +{ + public interface IHttpContextFactory + { + HttpContextBase Context { get; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/ObjectResolution/WebHttpContextFactory.cs b/src/Umbraco.Core/ObjectResolution/WebHttpContextFactory.cs new file mode 100644 index 0000000000..d8f6d4131f --- /dev/null +++ b/src/Umbraco.Core/ObjectResolution/WebHttpContextFactory.cs @@ -0,0 +1,12 @@ +using System.Web; + +namespace Umbraco.Core.ObjectResolution +{ + public class WebHttpContextFactory : IHttpContextFactory + { + public HttpContextBase Context + { + get { return new HttpContextWrapper(HttpContext.Current); } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/ProviderFeature.cs b/src/Umbraco.Core/ProviderFeature.cs new file mode 100644 index 0000000000..9fab0b7db6 --- /dev/null +++ b/src/Umbraco.Core/ProviderFeature.cs @@ -0,0 +1,42 @@ +using System; +using System.Configuration; +using System.Configuration.Provider; +using System.Web.Configuration; +using Umbraco.Core.Configuration; + +namespace Umbraco.Core +{ + public abstract class ProviderFeature where TProvider : ProviderBase + { + private static bool _initialized; + private static object _lock = new object(); + + public static TProvider Provider { get; private set; } + + public static GenericProviderCollection 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(); + + ProvidersHelper.InstantiateProviders(section.Providers, Providers, typeof (TProvider)); + + Provider = Providers[section.DefaultProvider]; + + _initialized = true; + } + } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 7509049668..f13206e9e7 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -60,6 +60,7 @@ + diff --git a/src/Umbraco.Web.UI/config/ImageUrl.Release.config b/src/Umbraco.Web.UI/config/ImageUrl.Release.config new file mode 100644 index 0000000000..288001b6aa --- /dev/null +++ b/src/Umbraco.Web.UI/config/ImageUrl.Release.config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/Umbraco.Web.UI/config/ImageUrl.config b/src/Umbraco.Web.UI/config/ImageUrl.config new file mode 100644 index 0000000000..288001b6aa --- /dev/null +++ b/src/Umbraco.Web.UI/config/ImageUrl.config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/Umbraco.Web/Media/ImageUrl.cs b/src/Umbraco.Web/Media/ImageUrl.cs new file mode 100644 index 0000000000..f7a7bae465 --- /dev/null +++ b/src/Umbraco.Web/Media/ImageUrl.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections; +using System.Configuration; +using System.Globalization; +using System.Web; +using System.Web.Configuration; +using Umbraco.Core; +using Umbraco.Core.Configuration; +using Umbraco.Core.Media; +using Umbraco.Core.ObjectResolution; +using umbraco; + +namespace Umbraco.Web.Media +{ + public class ImageUrl : ProviderFeature + { + + private static IHttpContextFactory _contextFactory; + + static ImageUrl() + { + _contextFactory = new WebHttpContextFactory(); + Initialize("ImageUrl"); + } + + public static string GetImageUrl(string specifiedSrc, string field, string provider, int? nodeId = null) + { + string url; + ImageUrlProviderBase p = GetProvider(provider); + + string fieldValue = string.Empty; + if (!string.IsNullOrEmpty(field)) + { + if(nodeId.HasValue) + { + var contentFromCache = GetContentFromCache(nodeId.GetValueOrDefault(), field); + if (contentFromCache != null) + { + fieldValue = contentFromCache.ToString(); + } + else + { + page 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; + } + int mediaId; + if (int.TryParse(fieldValue, out mediaId)) + { + //Fetch media + url = p.GetImageUrlFromMedia(mediaId); + } + else + { + //assume file path + url = p.GetImageUrlFromFileName(fieldValue); + } + + } + else + { + url = p.GetImageUrlFromFileName(specifiedSrc); + } + return url; + } + + private static ImageUrlProviderBase GetProvider(string provider) + { + return string.IsNullOrEmpty(provider) ? + Provider : + Providers[provider]; + } + + private static object GetContentFromCache(int nodeIdInt, string field) + { + object content = _contextFactory.Context.Cache[String.Format("contentItem{0}_{1}", nodeIdInt.ToString(CultureInfo.InvariantCulture), field)]; + return content; + } + + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Media/ImageUrlProviders/ImageUrlProvider.cs b/src/Umbraco.Web/Media/ImageUrlProviders/ImageUrlProvider.cs new file mode 100644 index 0000000000..ba1dbe0a64 --- /dev/null +++ b/src/Umbraco.Web/Media/ImageUrlProviders/ImageUrlProvider.cs @@ -0,0 +1,43 @@ +using System.Xml.XPath; +using Umbraco.Core.Media; +using umbraco; + +namespace Umbraco.Web.Media.ImageUrlProviders +{ + public class ImageUrlProvider : ImageUrlProviderBase + { + public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) + { + //Get default values from the provider config here? + base.Initialize(name, config); + } + + public override string GetImageUrlFromMedia(int mediaId) + { + string url = string.Empty; + var nodeIterator = library.GetMedia(mediaId, false); + if (nodeIterator.Current != null) + { + var filename = getProperty(nodeIterator, "umbracoFile"); + var fileExtension = getProperty(nodeIterator, "umbracoExtension"); + url = filename; + } + + return url; + } + + private static string getProperty(XPathNodeIterator nodeIterator, string fileProp) + { + string xpath = UmbracoSettings.UseLegacyXmlSchema + ? string.Format(".//data[@alias = '{0}']", fileProp) + : string.Format(".//{0}", fileProp); + var file = nodeIterator.Current.SelectSingleNode(xpath).InnerXml; + return file; + } + + public override string GetImageUrlFromFileName(string specifiedSrc) + { + return specifiedSrc + "?wheee=1234"; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index c6c74be2ee..58410154a9 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -354,6 +354,7 @@ ASPXCodeBehind + diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/Image.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/Image.cs new file mode 100644 index 0000000000..bc96b080d7 --- /dev/null +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/Image.cs @@ -0,0 +1,24 @@ +using System.Web.UI; +using System.Web.UI.HtmlControls; +using Umbraco.Core.Media; +using Umbraco.Web.Media; + +namespace umbraco.presentation.templateControls +{ + public class Image : HtmlImage + { + public string NodeId { get; set; } + public string Field { get; set; } + public string Provider { get; set; } + + protected override void Render(HtmlTextWriter writer) + { + int id; + bool hasid = int.TryParse(NodeId, out id); + int? nodeId = hasid ? id : (int?) null; + + Src = ImageUrl.GetImageUrl(Src, Field, Provider, nodeId); + base.Render(writer); + } + } +} \ No newline at end of file