Copied to clipboard

Flag this post as spam?

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


  • Ben Norman 167 posts 276 karma points
    Sep 16, 2014 @ 04:48
    Ben Norman
    0

    full domain for media

    Is there a setting to get the full domain generated for media that are included in a Rich Text Editor Field? The useDomainPrefixes only affects urls for content and I was hoping it would be the same for the media.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 16, 2014 @ 09:08
    Alex Skrypnyk
    0

    Hi Ben,

    You can try to subscribe on Create Media event and rewrite path to media. List of events :

    http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events

    Thanks

  • Ben Norman 167 posts 276 karma points
    Sep 17, 2014 @ 04:58
    Ben Norman
    1

    I really like that idea about creating an event but I wanted to keep my change to Razor

    Option 1 - use HtmlAgilityPack - I didn't use this one. It put weird little characters in my output.

    @using HtmlAgilityPack
    @{
        var header = Umbraco.Field("headerText").ToString();
        var html = new HtmlDocument();
    html.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8;
        html.LoadHtml(header);
        foreach (HtmlAgilityPack.HtmlNode img in html.DocumentNode.SelectNodes("//img").ToList())
        {
            img.Attributes["src"].Value = String.Format("{0}{1}", Request.Url.GetLeftPart(UriPartial.Authority), img.Attributes["src"].Value);
        }
        using (var ms = new MemoryStream())
        {
            html.Save(ms);
            ms.Position = 0;
            var sr = new StreamReader(ms, true);
            header = sr.ReadToEnd();
        }
    }
    

    Option 2 - Good Ole Replace - I used this one.

    @{
        var header2 = Umbraco.Field("headerText").ToString();
        header2 = header2.Replace("\"/media", String.Format("\"{0}/media", Request.Url.GetLeftPart(UriPartial.Authority)));
    }
    

    Use them like this

    @Html.Raw(header)
    @Html.Raw(header2)
    
  • Ben Norman 167 posts 276 karma points
    Sep 18, 2014 @ 03:10
    Ben Norman
    0

    i ended up working out the encoding issue. I added html.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8; to the code above.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 18, 2014 @ 09:30
    Alex Skrypnyk
    0

    hi Ben,

    So do you use HtmlAgilityPack for finding media items on the page ? Is it the better solution ?

  • Ben Norman 167 posts 276 karma points
    Sep 18, 2014 @ 10:38
    Ben Norman
    100

    Dude, Here is the final Helper that I ended up with. Its pretty cool now. It drops in a base64 version of the image. Not recommended unless you need to do that. Notice the 2 encoding settings which are both needed to sort out the encoding stuff.

    @helper RenderText(string input)
    {
        var output = String.Empty;
        var html = new HtmlAgilityPack.HtmlDocument();
        html.OptionDefaultStreamEncoding = System.Text.Encoding.UTF8;
        html.LoadHtml(input);
        var images = html.DocumentNode.SelectNodes("//img[contains(@src, 'media')]");
        if (images != null)
        {
            foreach (var img in images)
            {
                img.Attributes["src"].Value = "data:image/gif;base64," + Convert.ToBase64String(System.IO.File.ReadAllBytes(Server.MapPath(img.Attributes["src"].Value.Split(new char[] { '?' })[0])));
            }
        }
        using (var ms = new MemoryStream())
        {
            html.Save(ms);
            ms.Position = 0;
            var sr = new StreamReader(ms, System.Text.Encoding.UTF8);
            output = sr.ReadToEnd();
        }
    
        @Html.Raw(output)
    }
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 18, 2014 @ 11:44
    Alex Skrypnyk
    0

    Ben, great helper !

    Thanks

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies