Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon 81 posts 184 karma points
    Dec 01, 2016 @ 14:56
    Simon
    0

    Override RTE media picker urls

    Hi,

    Is there some way to override the RTE media picker to use the CDN domain rather than the relative Url that it uses at the moment?

    A standard media picker control uses the proper CDN url e.g. http//cdn.example.com/media/234/image.jpg but using an image picked via the RTE still serves images from the local machine not the CDN url.

    Ideas?

    Thanks

    Simon

  • Ali Sheikh Taheri 470 posts 1647 karma points c-trib
    Dec 01, 2016 @ 20:45
    Ali Sheikh Taheri
    0

    Hi Simon,

    You can use ParseContent method and pass the value of the RTE.

    Have a look at the function in the GitHub repository here: https://github.com/AliSheikhTaheri/AST-Amazon-S3/blob/master/AST%20Amazon%20S3/Helper/ContentHelper.cs#L7

    example in a cshtml

    var bodyText = Model.Content.GetPropertyValue<string>("bodyText");
    @ContentHelper.ParseContent(bodyText)
    

    Hope the above code helps.

    Cheers

    Ali

  • Simon 81 posts 184 karma points
    Dec 04, 2016 @ 16:42
    Simon
    0

    Hi Ali,

    Thanks for coming back - but if I do that I seem to get the raw HTML rendered on the page - e.g.

    <p><span class="FullSizeImage"><img id="__mcenew" src="/media/1087/dreamstime_l_28166563.jpg" alt="" data-id="1225"></span></p> <h3>Do take your shoes off</h3> <p>When entering temples, private homes, or even some shops and offices, take your shoes off. You’ll know when it’s appropriate to do so because you’ll see lines and piles of shoes outside the front door. Feet are considered the dirtiest part of the body in Thailand, and shoes even more so. Therefore, it’s considered a big no-no to point at anything with your feet, put your feet up on the table, or touch anyone with your feet.</p>
    

    Also, you'll see from the above that the media url is still relative and doesn't seem to have picked up the CDN domain.

    Ideas?

    Thanks

    Simon

  • Ali Sheikh Taheri 470 posts 1647 karma points c-trib
    Dec 07, 2016 @ 11:24
    Ali Sheikh Taheri
    0

    Hi Simon,

    In order to fix the raw html use this:

    @Html.Raw(ContentHelper.ParseContent(bodyText))
    

    To get CDN domain, do you have useCDN and cdnDomain setup in your web.config?

    Cheers

    Ali

  • Simon 81 posts 184 karma points
    Dec 07, 2016 @ 12:23
    Simon
    0

    Hi Ali,

    Sorry - still doesn't work. Media Urls added to the RTE using the media picker on the RTE editor are still relative and not using the CDN. Web.config settings

    <add key="useCDN" value="true" />
    <add key="cdnDomain" value="http://cdn.example.co.uk" />
    

    Template code:

    @{
        var bodyText = Model.Content.GetPropertyValue<string>("content");
        @Html.Raw(ContentHelper.ParseContent(bodyText))
    }
    

    Image url in the content

    http://www.example.co.uk/media/1087/dreamstime_l_28166563.jpg
    

    Not the CDN.

    Any more ideas?

    Thanks

    Simon

  • Simon 81 posts 184 karma points
    Dec 07, 2016 @ 12:28
    Simon
    0

    Ali,

    Found the problem - you're using this regex to find the images to parse

    <img(?<attr1>.*?)src=""(?<url>/media/.*?)""(?<attr2>.*?)/>
    

    However, the image tag from the RTE media picker is this:

    <img id="__mcenew" src="/media/1087/dreamstime_l_28166563.jpg" alt="" rel="1225" data-id="1225" />
    

    Which doesn't match your Regex.

  • Simon 81 posts 184 karma points
    Dec 15, 2016 @ 18:08
    Simon
    100

    Hi Ali,

    In the spirit of sharing, I've refactored your ParseMediaLinks function to update the main regexes. There's a couple of things that I found - anyway, here you go:

    private static string ParseMediaLinks(string input)
            {
                var output = input;
    
                var matchesImages = Regex.Matches(input, "<img(.*?)src=\"(?<url>/media/.*?)\"(.*?)(/?)>", RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
    
                foreach (Match match in matchesImages)
                {
                    string url = match.Groups["url"].Value;
    
                    var outputLink = string.Format("{0}{1}", GlobalHelper.GetCdnDomain(), url);
                    output = output.Replace(match.Groups["url"].Value, outputLink);
                }
    
                var matchesLinks = Regex.Matches(input, "<a(.*?)href=\"(?<url>/media/.*?)\"(.*?)>(?<content>.*?)</a>");
                foreach (Match match in matchesLinks)
                {
                    string url = match.Groups["url"].Value, content = match.Groups["content"].Value;
    
                    if (IsPlainText(content))
                    {
                        var outputLink = string.Format("{0}{1}", GlobalHelper.GetCdnDomain(), url);
                        output = output.Replace(match.Groups["url"].Value, outputLink);
                    }
                }
    
                return output;
            }
    

    Thanks

    Simon

  • Arunabha Das 37 posts 150 karma points
    Jun 20, 2017 @ 12:32
    Arunabha Das
    0

    Hi , How will I get the latest dll or code.

    The package is is throwing error.

  • Mark Zawodny 9 posts 72 karma points
    Aug 31, 2017 @ 18:21
    Mark Zawodny
    0

    Note that even with Simon's update (which works great by the way), that regex still relies on using "/media/" as part of the URL. If you define a different virtualRoot in FileSystemProviders.config, it won't find the URL to replace it properly. I changed mine manually to follow my naming convention, but it would be helpful to incorporate the virtualRoot element into the regex in a future version.

Please Sign in or register to post replies

Write your reply to:

Draft