Skip to content

V14: Handle embedded media providers returning dimensions as string values in their oEmbed JSON response#16514

Merged
Zeegaan merged 1 commit intov14/devfrom
v14/bugfix/oembed-with-string-dimentions
May 31, 2024
Merged

V14: Handle embedded media providers returning dimensions as string values in their oEmbed JSON response#16514
Zeegaan merged 1 commit intov14/devfrom
v14/bugfix/oembed-with-string-dimentions

Conversation

@elit0451
Copy link
Copy Markdown
Contributor

Details

  • Adding a base class of OEmbedResponse, so we can add an implementation handling string values in the JSON response for dimensions (width and height).
    • This wasn't necessary before when using Newtonsoft.Json, but System.Text.Json is more strict in this case.

Test

  • Create a doc type with RTE;
    • Make sure you have "Embed" option included in the toolbar.
  • Create a content node from that document type;
  • Insert an embedded media by clicking on the "Embed" button in the RTE;
  • Verify that https://www.youtube.com/watch?v=3bSHnMZF9xI renders;
  • Configure a new provider by adding the following classes:
using Umbraco.Cms.Core.Media.EmbedProviders;
using Umbraco.Cms.Core.Serialization;

namespace Umbraco.Cms.Web.UI;

public class DeviantArtEmbedProvider : OEmbedProviderBase
{
    public DeviantArtEmbedProvider(IJsonSerializer jsonSerializer)
        : base(jsonSerializer)
    {
    }

    public override string ApiEndpoint => "https://backend.deviantart.com/oembed?url=";

    public override string[] UrlSchemeRegex => new[]
    {
        @"fav\.me/*",
        @"\w+\.deviantart.com\/\w+\/art\/*",
        @"\w+\.deviantart.com\/art\/*",
        @"sta\.sh/*",
        @"\w+\.deviantart.com\/\w+#\/d*"
    };

    public override Dictionary<string, string> RequestParams => new();

    public override string? GetMarkup(string url, int maxWidth = 0, int maxHeight = 0)
    {
        return GeOEmbedDataAsync(url, maxWidth, maxHeight, CancellationToken.None).GetAwaiter().GetResult();
    }

    public override async Task<string?> GeOEmbedDataAsync(string url, int? maxWidth, int? maxHeight, CancellationToken cancellationToken)
    {
        var requestUrl = base.GetEmbedProviderUrl(url, maxWidth, maxHeight);
        OEmbedResponseWithStringDimensions? oembed = await base.GetJsonResponseAsync<OEmbedResponseWithStringDimensions>(requestUrl, cancellationToken);

        return oembed?.GetHtml();
    }
}

using Umbraco.Cms.Core.Composing;

namespace Umbraco.Cms.Web.UI;

public class RegisterEmbedProvidersComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
        => builder.EmbedProviders().Append< DeviantArtEmbedProvider >();
}
  • Verify that http://fav.me/darrsfo renders now as well;

@Zeegaan
Copy link
Copy Markdown
Member

Zeegaan commented May 31, 2024

Works like a charm 🚀

@Zeegaan Zeegaan merged commit 464eaa0 into v14/dev May 31, 2024
@Zeegaan Zeegaan deleted the v14/bugfix/oembed-with-string-dimentions branch May 31, 2024 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants