Try our conversational search powered by Generative AI!

Issues with Optimizely 12.20.0 Module Upgrade - Event Handling and External Hosted Images

Vote:
 

I recently upgraded my Optimizely module from version 12.0.2 to 12.20.0, which is responsible for image uploads via a third-party API. While it was working fine with Optimizely 12.0.2, I encountered a few issues with the publish event handler code after the upgrade.

_contentEvents.PublishedContent += PublishedContent;

This event handler fires not only during the publishing event but also during the creation of a new page. Is there a way to ensure that this event is triggered only when the publishing process is completed? It's worth mentioning that this issue was present in the previous version as well.

The purpose of the method called by the published event is to collect data within the page, added by our module, and post this collected data to another API endpoint. To achieve this, the data collection function utilizes the HTML Agility Pack to read the nodes in the page and filter them based on CSS classes and other attributes. Here's the code for the method in

public static List<MediaUsage> GetInformationFromXhtmlProperties(IContent content, IContent pageContent)
{
    List<MediaUsage> mediaUsages = new List<MediaUsage>();

    var xhtmlProperties = content.GetType().GetProperties().Where(prop => prop.PropertyType == typeof(XhtmlString));
    foreach (var xhtmlProperty in xhtmlProperties)
    {
        var xhtmlString = xhtmlProperty.GetValue(content, null) as XhtmlString;
        if (xhtmlString == null) continue;

        HtmlDocument html = new HtmlDocument();
        html.LoadHtml(xhtmlString.ToString());

        HtmlNodeCollection htmlNodeCollection = html.DocumentNode.SelectNodes("//img");
        if (htmlNodeCollection == null) continue;

        var imageNodes = htmlNodeCollection.Where(node => node.Attributes.Select(attr => attr.Name == "class" && attr.Value.Contains("qbankmedia")).Any());

        foreach (var imageNode in imageNodes)
        {
            var srcAttribute = imageNode.Attributes["src"];
            if (srcAttribute != null)
            {
                IQBankEpiMedia qbankMedia = UrlResolver.Current.Route(new UrlBuilder(srcAttribute.Value)) as IQBankEpiMedia;
                if (qbankMedia != null)
                {
                    var usageInformation = CreateMediaUsage(pageContent, qbankMedia);
                    mediaUsages.Add(usageInformation);
                }
            }
        }
    }
    return mediaUsages;
}

Now, when I call the above method with an externally hosted image URL (an image downloaded from a CDN), the following line of code returns null:

IQBankEpiMedia qbankMedia = UrlResolver.Current.Route(new UrlBuilder(srcAttribute.Value)) as IQBankEpiMedia;

However, if the src attribute contains an internal URL (within the image folder on the website), it returns the correct media file. Do you have any idea why this might be happening?

#304217
Jun 28, 2023 5:33
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.