Skip to content

Commit

Permalink
Merge pull request #6 from jsedlak/v10.0-embed-fixes
Browse files Browse the repository at this point in the history
Fixes embed functionality for brightcove media items in experience editor
  • Loading branch information
Oceanswave authored Jul 8, 2021
2 parents 7274d57 + 591601f commit 9962723
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public virtual string GenerateBrightcoveUrl(MediaGenerateMarkupArgs args, bool i
{
var resource = isJs ? "index.min.js" : $"index.html?videoId={args.MediaItem[BrightcovePlayerParameters.MediaId]}";
var url = new UrlString($"//players.brightcove.net/{args.AccountItem[BrightcovePlayerParameters.PublisherId]}/{args.PlayerItem[BrightcovePlayerParameters.PlayerId]}_default/{resource}");

foreach (string arg in args.Properties.Collection)
{
url[arg] = args.Properties.Collection[arg];
}

return url.ToString();
}

Expand Down Expand Up @@ -112,6 +118,7 @@ protected virtual string GenerateJavascriptEmbed(MediaGenerateMarkupArgs args)
<video data-video-id='{args.MediaItem[BrightcovePlayerParameters.MediaId]}'
data-account='{args.AccountItem[BrightcovePlayerParameters.PublisherId]}'
data-player='{args.PlayerItem[BrightcovePlayerParameters.PlayerId]}'
data-item-id='{args.Properties.ItemId}'
data-embed='default'
data-application-id
class='video-js'
Expand Down
24 changes: 20 additions & 4 deletions src/Sitecore.MediaFramework/Commands/EmbedMedia.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Sitecore.MediaFramework.Commands
{
using System;

using System.Text.RegularExpressions;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Layouts;
Expand All @@ -13,6 +13,10 @@
[Serializable]
public class EmbedMedia : Command
{
private const string GuidRegex = @"[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?";
private const string ItemIdGroupName = "itemid";
private static readonly Regex ItemIdRegex = new Regex(@"item(\-)?[I|i]d=(\')?(?'" + ItemIdGroupName + "'" + GuidRegex + ")");

public override void Execute(CommandContext context)
{
Context.ClientPage.Start(this, "Run", context.Parameters);
Expand Down Expand Up @@ -77,14 +81,13 @@ protected void Run(ClientPipelineArgs args)

UrlString url = new UrlString(this.GetParameters(args.Result));

string itemId = url[Constants.PlayerParameters.ItemId];
var itemId = GetItemId(args.Result);

url.Remove(Constants.PlayerParameters.ItemId);

rendering.Datasource = new ID(itemId).ToString();
rendering.Datasource = itemId.ToString();
rendering.Parameters = url.ToString();


parsedLayout.Devices[deviceIndex] = device;
var updatedLayout = parsedLayout.ToXml();
var layout = GetLayout(updatedLayout);
Expand All @@ -94,6 +97,19 @@ protected void Run(ClientPipelineArgs args)
}
}

private ID GetItemId(string embedCode)
{
if (!ItemIdRegex.IsMatch(embedCode))
{
throw new InvalidOperationException("The generated embed code is invalid");
}

var match = ItemIdRegex.Match(embedCode);
var id = ID.Parse(match.Groups[ItemIdGroupName].Value);

return id;
}

protected virtual string GetParameters(string markup)
{
//TODO: Why we need HtmlAgilityPack dependence for simple text parsing?
Expand Down

0 comments on commit 9962723

Please sign in to comment.