Language specific MediaProvider breaking icons at Media Library

UPDATE: Kamruz Jaman has contributed with a comment for a simpler, less intrusive and more generalist solution:  Inside the GetMediaUrl method of your custom MediaProvider, check for the “Thumbnail” property of your MediaUrlOptions parameter. If it is true, make your code fall back to the base, such as:

if (mediaOptions.Thumbnail)
return base.GetMediaUrl(mediaItem, mediaOptions);

During an upgrade from Sitecore 6.5 to Sitecore 8.1 one issue were noticed at the Media Library: when a Media Item is uploaded, the icon appears broken.

Broken Icon

A custom MediaProvider is used at this build to modify the way Sitecore generates Media Item urls. All it does is to add the language at beginning of the media URL so we can have language-specific media items. This is important to allow the same Media Item (etc: an image or PDF) to be served in multiple languages.

Root cause

When a media item is uploaded to the Media Library, Sitecore will fill the “Icon” field with the URL of the media item itself. Because of the custom MediaProvider, language is being added to the beginning of the generated URL. Apparently that was not a problem in Sitecore 6.5, but things seems to have changed at Sitecore 8.

Technical explanation by Sitecore

A Support Ticket #476256 were created to allow further investigation from Sitecore on this issue. According to the technical team:

“The behavior you reported is expected as tree node icons are rendered by Sitecore.Shell.Applications.ContentManager.Sidebars.Tree.RenderIcon(Item item) method which does not use language definition, embedded to URL, to resolve item language. So when the Icon field contains URL with language definition embedded, it breaks the method functionality and leads to incorrect resolution of the item’s icon.

When you have versioned media item, the icon is rendered for specific language automatically even when language is not specified deliberately. For instance, if you have media item with two language versions and different images for each language, and use -/media/ItemGUID.ashx?h=16&thn=1&w=16 string in the Icon field, language specific icon will be rendered when you switch the language in the Content Editor.”

This explains the causes but doesn’t fix the issue: how can we skip or bypass the custom MediaProvider, avoiding the language to be added to the URL when the “Icon” field is filled?

Solution

In order to fix that we will need to extend and override the Sitecore.Resources.Media.Media class, responsible for the upload and update of metadata when a Media Item is uploaded to the Media Library, then modify all Media Types to use your class instead of the original.

Step 1 – Extend the Media class

This is going to be responsible for removing the language from the URL when a Media Item is uploaded. In bold you see the code responsible for that, rest is basically native code to ensure compliance with default Sitecore behavior.

using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Data.Proxies;
using Sitecore.Diagnostics;
using Sitecore.Resources.Media;
using Sitecore.SecurityModel;

namespace CustomMediaProvider
{
  public class Media : Sitecore.Resources.Media.Media
  {
    public override Sitecore.Resources.Media.Media Clone()
    {
      return new Media();
    }

    public override void UpdateMetaData(MediaStream mediaStream)
    {
      Assert.ArgumentNotNull(mediaStream, "mediaStream");
      var innerItem = MediaData.MediaItem.InnerItem;
      if (!innerItem.Paths.IsMediaItem) return;
      using (new EditContext(innerItem, SecurityCheck.Disable))
      {
        innerItem["extension"] = mediaStream.Extension;
        innerItem["mime type"] = mediaStream.MimeType;
        innerItem["size"] = mediaStream.Length.ToString();
        if (Settings.Media.AutoSetAlt)
        {
          innerItem["Alt"] = innerItem.Name;
        }
        var shellOptions = MediaUrlOptions.GetShellOptions();
        shellOptions.Thumbnail = true;
        shellOptions.Height = 0x10;
        shellOptions.Width = 0x10;
        var realItem = ProxyManager.GetRealItem(innerItem, true);
        // CODE ADDED TO REMOVE LANGUAGE FROM URL WHILE UPLOADING
        var iconUrl = MediaManager.GetMediaUrl(realItem, shellOptions);
        var prefix = "/" + Context.Language.Name + "/";
        if (iconUrl.StartsWith(prefix)) iconUrl = iconUrl.Replace(prefix, "");
        innerItem.Appearance.Icon = iconUrl;
      }
    }
  }
}

Step 2 – Register the Media class to all Media Types

This is the painful part: we will need to register our class to every Media Type we want to cover. There is no easy way for that other than adding a node for each Media Type, paying attention to the correct way for patching that specific node (some nodes may have a Media class already defined and thus needs correct patching, others don’t).

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
 <mediaLibrary>
 <!-- This is our Custom MediaProvider causing all the trouble -->
 <mediaProvider>
 <patch:attribute name="type">CustomMediaProvider.MediaProvider,CustomMediaProvider</patch:attribute>
 </mediaProvider>
 <!-- And here goes our custom Media class being attached to several Media Types -->
 <mediaTypes>
 <mediaType name="Any" extensions="*">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.Media, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="AVI video" extensions="avi">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="Windows Bitmap image" extensions="bmp, dib">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.ImageMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="Flash" extensions="swf">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.SwfMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="Flash video" extensions="flv">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="GIF image" extensions="gif">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.ImageMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="HTML" extensions="htm,html,stm">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="JPEG image" extensions="jpg, jpeg, jpe, jfif">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.ImageThumbnailGenerator, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="MP3" extensions="mp3">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.Mp3Media, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="MP4 video" extensions="mp4">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="PDF file" extensions="pdf">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="PNG image" extensions="png">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.ImageMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="SVG image" extensions="svg">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.SvgMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="QuickTime movie" extensions="mov, qt">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="TIFF image" extensions="tiff, tif">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.ImageMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="Zip file" extensions="zip">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider" patch:instead="*[@type='Sitecore.Resources.Media.ZipMedia, Sitecore.Kernel']"/>
 </prototypes>
 </mediaType>
 <mediaType name="Video for Adobe Flash Player" extensions="f4v">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="Windows Media video" extensions="WMV">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="Word document" extensions="doc">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="Word 2007 document" extensions="docx">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="PPTX File" extensions="pptx">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="PPT File" extensions="ppt">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="XLSX File" extensions="xlsx">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 <mediaType name="XLS File" extensions="xls">
 <prototypes>
 <media type="CustomMediaProvider.Media, CustomMediaProvider"/>
 </prototypes>
 </mediaType>
 </mediaTypes>
 </mediaLibrary>
 </sitecore>
</configuration>
Publicado em Bug fixing, Development, Support Ticket, Upgrades
2 comentários sobre “Language specific MediaProvider breaking icons at Media Library
  1. Kamruz Jaman says:

    Another option would have been to check if the request was for a thumbnail (the ?thn=1 part of the stored Icon url) and if so ignore your custom code to generate the Media URLs. This is what I ended up doing for some code I wrote:

    https://github.com/jammykam/Sitecore-CloudMediaLibrary/blob/master/src/FS.MediaLibrary.CloudStorage/Media/MediaProvider.cs#L22

    • Great point Kamruz, I’ve just tested you approach and it also made the trick.

      In fact your approach is actually better as it is less intrusive and works in more cases, such as when the Attachment field renders.

      I am updating the article with a message at the top suggesting your solution as a better response. Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

  Am Not Spammer

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>