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.
This commit is contained in:
Erik-Jan Westendorp
2021-11-30 04:10:54 +01:00
committed by GitHub
parent 4cd0544c65
commit 5d2c950073
18 changed files with 129 additions and 93 deletions

View File

@@ -77,10 +77,10 @@ namespace Umbraco.Cms.Core.DependencyInjection
/// </summary>
/// <typeparam name="T">The type of the embed provider.</typeparam>
/// <param name="builder">The builder.</param>
public static IUmbracoBuilder AddOEmbedProvider<T>(this IUmbracoBuilder builder)
public static IUmbracoBuilder AddEmbedProvider<T>(this IUmbracoBuilder builder)
where T : class, IEmbedProvider
{
builder.OEmbedProviders().Append<T>();
builder.EmbedProviders().Append<T>();
return builder;
}

View File

@@ -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<YouTube>()
.Append<Twitter>()
.Append<Vimeo>()
@@ -265,7 +266,15 @@ namespace Umbraco.Cms.Core.DependencyInjection
/// Gets the backoffice OEmbed Providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
[Obsolete("Use EmbedProviders() instead")]
public static EmbedProvidersCollectionBuilder OEmbedProviders(this IUmbracoBuilder builder)
=> EmbedProviders(builder);
/// <summary>
/// Gets the backoffice Embed Providers collection builder.
/// </summary>
/// <param name="builder">The builder.</param>
public static EmbedProvidersCollectionBuilder EmbedProviders(this IUmbracoBuilder builder)
=> builder.WithCollectionBuilder<EmbedProvidersCollectionBuilder>();
/// <summary>

View File

@@ -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";

View File

@@ -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<string, string> 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<T>(string url) where T : class
{
var response = DownloadResponse(url);
return _jsonSerializer.Deserialize<T>(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;
}
}
};
}

View File

@@ -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/";

View File

@@ -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";

View File

@@ -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
/// <summary>
/// Embed Provider for Giphy.com the popular online GIFs and animated sticker provider.
/// </summary>
/// TODO(V10) : change base class to OEmbedProviderBase
public class Giphy : EmbedProviderBase
{
public override string ApiEndpoint => "https://giphy.com/services/oembed?url=";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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<string, string> 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<T>(string url) where T : class
{
var response = DownloadResponse(url);
return _jsonSerializer.Deserialize<T>(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;
}
}
};

View File

@@ -1,4 +1,4 @@
using System.Net;
using System.Net;
using System.Runtime.Serialization;
namespace Umbraco.Cms.Core.Media.EmbedProviders

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";