umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.4k stars 2.66k forks source link

/{localLink:umb://media/f73925e0b1bc4b2daa99eef696d6eb36} rendering as blank on frontend #16216

Open PhilMakower opened 4 months ago

PhilMakower commented 4 months ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.0.3

Bug summary

I have migrated content from v7 to v13. At least one of the pages has a link to a PDF in the media section.

On the v7 site, the href is just "/media/2732/ds2178ra.pdf" On the migrated (v13) site, the href is "/{localLink:umb://media/f73925e0b1bc4b2daa99eef696d6eb36}" as above. On the v13 site, in Umbraco we can click the link then click the link button to get the sidebar. This correctly shows the link as "/media/2732/ds2178ra.pdf" (this file exists on the filesystem)

Om the v13 site, the razor template includes @Model.BodyText where BodyText is the RTE property (html string) The media href is blank. There is also an href "/{localLink:1080}" in the backend which renders correctly as "/products/" so the issue seems specific to media.

I have tried stopping the, deleting the DATA directory and restarting so it has to re-create the caches. I have tried the "Published Status" buttons in Umbraco so see if that fixes the error, no joy.

Specifics

I have written an extension RteString() to fetch the BodyText from the ExternalIndex, which has the link as above.

public static string? RteString(this IExamineManager examineManager, string Name, int Id)
{
    // https://docs.umbraco.com/umbraco-cms/reference/searching/examine/examine-manager
    if (examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex? index))
    {
        string query = "+(__IndexType:content -template:0) +id:" + Id.ToString();
        ISearchResults searchResults = index.Searcher.CreateQuery().NativeQuery(query.ToString()).Execute();
        if (searchResults.Any())
        {
            foreach (ISearchResult result in searchResults)
            {
                if (result.Id is null)
                {
                    continue;
                    // NOT REACHED
                }
                IEnumerable<string> Values = result.GetValues("__Raw_" + Name);
                if (Values.Any())
                {
                    return Values.First();
                    // NOT REACHED
                }
                else
                {
                    //return null;
                    // NOT REACHED
                }
            }
            //return null;
            // NOT REACHED
        }
        else
        {
            //return null;
            // NOT REACHED
        }
    }
    else
    {
        //return null;
        // NOT REACHED
    }
    return null;
}

I have writen an extension EnsureInternalLinks() to turnt he links into actual URLs. Note that for MEDIA, I get the IMedia item from the media service, and the URL is just in the "umbracoFIle" property.

public static string EnsureInternalLinks(this IMediaService mediaService, IContentService contentService, UmbracoHelper Umbraco, string text)
{
    foreach ((int? intId, GuidUdi? udi, string tagValue) in FindLocalLinkIds(text))
    {
        if (udi is not null)
        {
            //GuidUdi MyUdi = udi;
            Guid MyGuid = udi.Guid;
            // umb://media/f73925e0b1bc4b2daa99eef696d6eb36 => f73925e0b1bc4b2daa99eef696d6eb36
            string newLink = "#";
            if (udi.EntityType == Constants.UdiEntityType.Document)
            {
                IContent? icontent = contentService.GetById(MyGuid);
                IPublishedContent? content = icontent is null ? null : Umbraco.Content(icontent.Id);
                newLink = content?.Url() ?? "";
            }
            else if (udi.EntityType == Constants.UdiEntityType.Media)
            {
                IMedia? imedia = mediaService.GetById(MyGuid);
                //IPublishedContent? media = imedia is null ? null : Umbraco.Media(imedia.Id);
                //newLink = media?.Url() ?? "#Media:" + imedia?.Id;
                string? src = imedia?.GetValue<string>(Constants.Conventions.Media.File);
                //dynamic umbracoFile = json is null ? null : JsonConvert.DeserializeObject(json);
                //newLink = umbracoFile?.src ?? "#Media:" + imedia?.Id;
                newLink = src ?? "";
            }
            text = text.Replace(tagValue, "href=\"" + newLink);

        }
        else if (intId.HasValue)
        {
            IPublishedContent? content = Umbraco.Content((int)intId);
            string newLink = content?.Url() ?? "";
            text = text.Replace(tagValue, "href=\"" + newLink);
        }
    }

    return text;
}

internal static readonly Regex LocalLinkPattern = new(
    @"href=""[/]?(?:\{|\%7B)localLink:([a-zA-Z0-9-://]+)(?:\}|\%7D)",
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

private static IEnumerable<(int? intId, GuidUdi? udi, string tagValue)> FindLocalLinkIds(string text)
{
    // Parse internal links
    MatchCollection tags = LocalLinkPattern.Matches(text);
    foreach (Match tag in tags)
    {
        if (tag.Groups.Count > 0)
        {
            string id = tag.Groups[1].Value; // .Remove(tag.Groups[1].Value.Length - 1, 1);

            // The id could be an int or a UDI
            if (UdiParser.TryParse(id, out Umbraco.Cms.Core.Udi? udi))
            {
                var guidUdi = udi as GuidUdi;
                if (guidUdi is not null)
                {
                    yield return (null, guidUdi, tag.Value);
                }
            }

            if (int.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intId))
            {
                yield return (intId, null, tag.Value);
            }
        }
    }
}

then in templates

@inject IExamineManager examineManager
@inject IMediaService mediaService
@inject IContentService contentService
@inject HtmlUrlParser HtmlUrlParser
@inject HtmlImageSourceParser HtmlImageSourceParser
@{
string BodyText = examineManager.RteString("bodyText", Model.Id) ?? "";
  string html = mediaService.EnsureInternalLinks(contentService, Umbraco, BodyText);
  html = HtmlUrlParser.EnsureUrls(html);
  html = HtmlImageSourceParser.EnsureImageSources(html);
}
@Html.Raw(html)

Steps to reproduce

Add media link to content in RTE editor in Umbraco View page Newly created link shows OK using @Model.BodyText and custom Migrated link shows BLANK using @Model.BodyText but shows OK using @Html.Raw() on custom string created as above (it's as if the migrated media file in not "published" somehow? Though then it wouldn't be in the external index!)

Expected result / actual result

expect the output to include the HREF to the media item But the HREF is BLANK

github-actions[bot] commented 4 months ago

Hi there @PhilMakower!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

PhilMakower commented 4 months ago

The media item in question, as well as many others, are in a folder in the RECYCLE BIN of the Media Libarary! In v7 the media items show on the front end anyway (since the content has direct links to the files) In v13 the links have been migrated to locallinks. Presumably Umbraco "knows" not to link to Media items in the Recycle Bin! I guess one workaround is to find all the locallinks in the content to Media items in the Recycle Bin and move each out of the Recycle Bin.

PhilMakower commented 4 months ago

Or maybe customise the published content cache to allow links to media items even if they are in the Recycle Bin?