From 5d2c950073a58d1ff3c90124e0b03554e2b760b7 Mon Sep 17 00:00:00 2001 From: Erik-Jan Westendorp Date: Tue, 30 Nov 2021 04:10:54 +0100 Subject: [PATCH] Rename AddOEmbedProvider() and Obsolete OEmbedProviders() (#11703) * Rename AddOEmbedProvider to AddEmbedProvider * Obsolete OEmbedProviders() Obsolete OEmbedProviders() and proxy it to a new EmbedProviders() * Rename EmbedProviderBase to OEmbedProviderBase Rename EmbedProviderBase to OEmbedProviderBase Add an obsoleted class EmbedProviderBase Add todo's to all implementations * remove unintentionally comment Rem: automatic tests failures not linked to this PR. --- .../UmbracoBuilder.CollectionBuilders.cs | 4 +- .../UmbracoBuilder.Collections.cs | 11 ++- .../Media/EmbedProviders/DailyMotion.cs | 3 +- .../Media/EmbedProviders/EmbedProviderBase.cs | 81 ++---------------- .../Media/EmbedProviders/Flickr.cs | 3 +- .../Media/EmbedProviders/GettyImages.cs | 3 +- .../Media/EmbedProviders/Giphy.cs | 3 +- src/Umbraco.Core/Media/EmbedProviders/Hulu.cs | 3 +- .../Media/EmbedProviders/Issuu.cs | 3 +- .../Media/EmbedProviders/Kickstarter.cs | 3 +- .../EmbedProviders/OEmbedProviderBase.cs | 85 +++++++++++++++++++ .../Media/EmbedProviders/OEmbedResponse.cs | 2 +- .../Media/EmbedProviders/Slideshare.cs | 3 +- .../Media/EmbedProviders/SoundCloud.cs | 3 +- src/Umbraco.Core/Media/EmbedProviders/Ted.cs | 3 +- .../Media/EmbedProviders/Twitter.cs | 3 +- .../Media/EmbedProviders/Vimeo.cs | 3 +- .../Media/EmbedProviders/Youtube.cs | 3 +- 18 files changed, 129 insertions(+), 93 deletions(-) create mode 100644 src/Umbraco.Core/Media/EmbedProviders/OEmbedProviderBase.cs diff --git a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.CollectionBuilders.cs b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.CollectionBuilders.cs index 8b6e6b78d8..5259c2a8a9 100644 --- a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.CollectionBuilders.cs +++ b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.CollectionBuilders.cs @@ -77,10 +77,10 @@ namespace Umbraco.Cms.Core.DependencyInjection /// /// The type of the embed provider. /// The builder. - public static IUmbracoBuilder AddOEmbedProvider(this IUmbracoBuilder builder) + public static IUmbracoBuilder AddEmbedProvider(this IUmbracoBuilder builder) where T : class, IEmbedProvider { - builder.OEmbedProviders().Append(); + builder.EmbedProviders().Append(); return builder; } diff --git a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Collections.cs b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Collections.cs index 1d66e874be..a323d16360 100644 --- a/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Collections.cs +++ b/src/Umbraco.Core/DependencyInjection/UmbracoBuilder.Collections.cs @@ -1,3 +1,4 @@ +using System; using Umbraco.Cms.Core.Actions; using Umbraco.Cms.Core.Cache; using Umbraco.Cms.Core.Composing; @@ -102,7 +103,7 @@ namespace Umbraco.Cms.Core.DependencyInjection builder.ManifestFilters(); builder.MediaUrlGenerators(); // register OEmbed providers - no type scanning - all explicit opt-in of adding types, IEmbedProvider is not IDiscoverable - builder.OEmbedProviders() + builder.EmbedProviders() .Append() .Append() .Append() @@ -265,7 +266,15 @@ namespace Umbraco.Cms.Core.DependencyInjection /// Gets the backoffice OEmbed Providers collection builder. /// /// The builder. + [Obsolete("Use EmbedProviders() instead")] public static EmbedProvidersCollectionBuilder OEmbedProviders(this IUmbracoBuilder builder) + => EmbedProviders(builder); + + /// + /// Gets the backoffice Embed Providers collection builder. + /// + /// The builder. + public static EmbedProvidersCollectionBuilder EmbedProviders(this IUmbracoBuilder builder) => builder.WithCollectionBuilder(); /// diff --git a/src/Umbraco.Core/Media/EmbedProviders/DailyMotion.cs b/src/Umbraco.Core/Media/EmbedProviders/DailyMotion.cs index e5fc553ea8..b79e1a8de2 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/DailyMotion.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/DailyMotion.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO (V10): change base class to OEmbedProviderBase public class DailyMotion : EmbedProviderBase { public override string ApiEndpoint => "https://www.dailymotion.com/services/oembed"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/EmbedProviderBase.cs b/src/Umbraco.Core/Media/EmbedProviders/EmbedProviderBase.cs index bbc690ce97..6d745d3d49 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/EmbedProviderBase.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/EmbedProviderBase.cs @@ -1,85 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Http; -using System.Text; -using System.Xml; +using System; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { - public abstract class EmbedProviderBase : IEmbedProvider + [Obsolete("Use OEmbedProviderBase instead")] + public abstract class EmbedProviderBase : OEmbedProviderBase { - private readonly IJsonSerializer _jsonSerializer; - protected EmbedProviderBase(IJsonSerializer jsonSerializer) + : base(jsonSerializer) { - _jsonSerializer = jsonSerializer; - } - - private static HttpClient _httpClient; - - public abstract string ApiEndpoint { get; } - - public abstract string[] UrlSchemeRegex { get; } - - public abstract Dictionary RequestParams { get; } - - public abstract string GetMarkup(string url, int maxWidth = 0, int maxHeight = 0); - - public virtual string GetEmbedProviderUrl(string url, int maxWidth, int maxHeight) - { - if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute) == false) - throw new ArgumentException("Not a valid URL.", nameof(url)); - - var fullUrl = new StringBuilder(); - - fullUrl.Append(ApiEndpoint); - fullUrl.Append("?url=" + WebUtility.UrlEncode(url)); - - foreach (var param in RequestParams) - fullUrl.Append($"&{param.Key}={param.Value}"); - - if (maxWidth > 0) - fullUrl.Append("&maxwidth=" + maxWidth); - - if (maxHeight > 0) - fullUrl.Append("&maxheight=" + maxHeight); - - return fullUrl.ToString(); - } - - public virtual string DownloadResponse(string url) - { - if (_httpClient == null) - _httpClient = new HttpClient(); - - using (var request = new HttpRequestMessage(HttpMethod.Get, url)) - { - var response = _httpClient.SendAsync(request).Result; - return response.Content.ReadAsStringAsync().Result; - } - } - - public virtual T GetJsonResponse(string url) where T : class - { - var response = DownloadResponse(url); - return _jsonSerializer.Deserialize(response); - } - - public virtual XmlDocument GetXmlResponse(string url) - { - var response = DownloadResponse(url); - var doc = new XmlDocument(); - doc.LoadXml(response); - - return doc; - } - - public virtual string GetXmlProperty(XmlDocument doc, string property) - { - var selectSingleNode = doc.SelectSingleNode(property); - return selectSingleNode != null ? selectSingleNode.InnerText : string.Empty; } } -}; +} diff --git a/src/Umbraco.Core/Media/EmbedProviders/Flickr.cs b/src/Umbraco.Core/Media/EmbedProviders/Flickr.cs index 48d4be06dc..2ea5fd8109 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Flickr.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Flickr.cs @@ -1,9 +1,10 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Net; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Flickr : EmbedProviderBase { public override string ApiEndpoint => "http://www.flickr.com/services/oembed/"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/GettyImages.cs b/src/Umbraco.Core/Media/EmbedProviders/GettyImages.cs index 3dbe44a9e2..0071ee935b 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/GettyImages.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/GettyImages.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class GettyImages : EmbedProviderBase { public override string ApiEndpoint => "http://embed.gettyimages.com/oembed"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Giphy.cs b/src/Umbraco.Core/Media/EmbedProviders/Giphy.cs index ae39b04123..8d99752525 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Giphy.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Giphy.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders @@ -6,6 +6,7 @@ namespace Umbraco.Cms.Core.Media.EmbedProviders /// /// Embed Provider for Giphy.com the popular online GIFs and animated sticker provider. /// + /// TODO(V10) : change base class to OEmbedProviderBase public class Giphy : EmbedProviderBase { public override string ApiEndpoint => "https://giphy.com/services/oembed?url="; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Hulu.cs b/src/Umbraco.Core/Media/EmbedProviders/Hulu.cs index 305d69d497..c88a3d5553 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Hulu.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Hulu.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Hulu : EmbedProviderBase { public override string ApiEndpoint => "http://www.hulu.com/api/oembed.json"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Issuu.cs b/src/Umbraco.Core/Media/EmbedProviders/Issuu.cs index 50ff03d880..89179d40af 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Issuu.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Issuu.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Issuu : EmbedProviderBase { public override string ApiEndpoint => "https://issuu.com/oembed"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Kickstarter.cs b/src/Umbraco.Core/Media/EmbedProviders/Kickstarter.cs index b3527a82a1..1d2c03e897 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Kickstarter.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Kickstarter.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Kickstarter : EmbedProviderBase { public override string ApiEndpoint => "http://www.kickstarter.com/services/oembed"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/OEmbedProviderBase.cs b/src/Umbraco.Core/Media/EmbedProviders/OEmbedProviderBase.cs new file mode 100644 index 0000000000..e7507c3bf2 --- /dev/null +++ b/src/Umbraco.Core/Media/EmbedProviders/OEmbedProviderBase.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Xml; +using Umbraco.Cms.Core.Serialization; + +namespace Umbraco.Cms.Core.Media.EmbedProviders +{ + public abstract class OEmbedProviderBase : IEmbedProvider + { + private readonly IJsonSerializer _jsonSerializer; + + protected OEmbedProviderBase(IJsonSerializer jsonSerializer) + { + _jsonSerializer = jsonSerializer; + } + + private static HttpClient _httpClient; + + public abstract string ApiEndpoint { get; } + + public abstract string[] UrlSchemeRegex { get; } + + public abstract Dictionary RequestParams { get; } + + public abstract string GetMarkup(string url, int maxWidth = 0, int maxHeight = 0); + + public virtual string GetEmbedProviderUrl(string url, int maxWidth, int maxHeight) + { + if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute) == false) + throw new ArgumentException("Not a valid URL.", nameof(url)); + + var fullUrl = new StringBuilder(); + + fullUrl.Append(ApiEndpoint); + fullUrl.Append("?url=" + WebUtility.UrlEncode(url)); + + foreach (var param in RequestParams) + fullUrl.Append($"&{param.Key}={param.Value}"); + + if (maxWidth > 0) + fullUrl.Append("&maxwidth=" + maxWidth); + + if (maxHeight > 0) + fullUrl.Append("&maxheight=" + maxHeight); + + return fullUrl.ToString(); + } + + public virtual string DownloadResponse(string url) + { + if (_httpClient == null) + _httpClient = new HttpClient(); + + using (var request = new HttpRequestMessage(HttpMethod.Get, url)) + { + var response = _httpClient.SendAsync(request).Result; + return response.Content.ReadAsStringAsync().Result; + } + } + + public virtual T GetJsonResponse(string url) where T : class + { + var response = DownloadResponse(url); + return _jsonSerializer.Deserialize(response); + } + + public virtual XmlDocument GetXmlResponse(string url) + { + var response = DownloadResponse(url); + var doc = new XmlDocument(); + doc.LoadXml(response); + + return doc; + } + + public virtual string GetXmlProperty(XmlDocument doc, string property) + { + var selectSingleNode = doc.SelectSingleNode(property); + return selectSingleNode != null ? selectSingleNode.InnerText : string.Empty; + } + } +}; diff --git a/src/Umbraco.Core/Media/EmbedProviders/OEmbedResponse.cs b/src/Umbraco.Core/Media/EmbedProviders/OEmbedResponse.cs index 33710d49d0..80ae4f7a14 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/OEmbedResponse.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/OEmbedResponse.cs @@ -1,4 +1,4 @@ -using System.Net; +using System.Net; using System.Runtime.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders diff --git a/src/Umbraco.Core/Media/EmbedProviders/Slideshare.cs b/src/Umbraco.Core/Media/EmbedProviders/Slideshare.cs index 1517886458..42e500aa5c 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Slideshare.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Slideshare.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Slideshare : EmbedProviderBase { public override string ApiEndpoint => "http://www.slideshare.net/api/oembed/2"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/SoundCloud.cs b/src/Umbraco.Core/Media/EmbedProviders/SoundCloud.cs index 36426b8625..687da98697 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/SoundCloud.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/SoundCloud.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Soundcloud : EmbedProviderBase { public override string ApiEndpoint => "https://soundcloud.com/oembed"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Ted.cs b/src/Umbraco.Core/Media/EmbedProviders/Ted.cs index a50681adf7..511cbf012d 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Ted.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Ted.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Ted : EmbedProviderBase { public override string ApiEndpoint => "http://www.ted.com/talks/oembed.xml"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Twitter.cs b/src/Umbraco.Core/Media/EmbedProviders/Twitter.cs index 1504fb931c..0f90d2d54c 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Twitter.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Twitter.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Twitter : EmbedProviderBase { public override string ApiEndpoint => "http://publish.twitter.com/oembed"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Vimeo.cs b/src/Umbraco.Core/Media/EmbedProviders/Vimeo.cs index e745ba50c0..db324bda12 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Vimeo.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Vimeo.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class Vimeo : EmbedProviderBase { public override string ApiEndpoint => "https://vimeo.com/api/oembed.xml"; diff --git a/src/Umbraco.Core/Media/EmbedProviders/Youtube.cs b/src/Umbraco.Core/Media/EmbedProviders/Youtube.cs index 9a8a28bf00..c85896db08 100644 --- a/src/Umbraco.Core/Media/EmbedProviders/Youtube.cs +++ b/src/Umbraco.Core/Media/EmbedProviders/Youtube.cs @@ -1,8 +1,9 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Umbraco.Cms.Core.Serialization; namespace Umbraco.Cms.Core.Media.EmbedProviders { + // TODO(V10) : change base class to OEmbedProviderBase public class YouTube : EmbedProviderBase { public override string ApiEndpoint => "https://www.youtube.com/oembed";