Sitecore media library paths

5.8k views Asked by At

Using Sitecore 6.5, when images are rendered on a web page, a URL such as the one below is used

~/media/OSS/Images/MyImage

But if you add an image from the library in a content editor a path such as below is used

~/media/1CFDDC34C94E460FAA2B1518DCA22360.PNG

This makes sense as it's trying to use a meaningful path when rendered for the web.

We would like to use the first media image path to add images in the content editor in HTML view rather than the default second method. This is because we are actually taking some html files and automatically adding them in to Sitecore via a script and we can change the image paths to a location in the media library if the first image format is used by using a convention so the images should appear in the newly created items. We have now idea about a media library image ID.

The first format does appear to work as images are rendered in the content editor design editor and when the page is rendered but Sitecore marks these as broken links in the Content Editor. Are any ideas on whether we are safe to use this format?

2

There are 2 answers

0
Derek Hunziker On

You may want to avoid hard coding paths to media in the rich text field. The second "dynamic link" is an important feature of Sitecore in that it keeps a connection between the media and item in the Links database. This safeguards you if you ever delete or move the media.

Since it sounds like you are importing content from an external source and you already have a means of detecting the image paths, I would recommend (if possible) that you upload the images programmatically and insert the dynamic links.

Below is a function that you can call for uploading to the Media Library and getting back the media item:

Example usage:

var file = AddFile("/assets/images/my-image.jpg", "/sitecore/media library/images/example", "my-image");

The code:

private MediaItem AddFile(string relativeUrl, string sitecorePath, string mediaItemName)
{
    var extension = Path.GetExtension(relativeUrl);

    var localFilename = @"c:\temp\" + mediaItemName + extension;
    using (var client = new WebClient())
    {
        client.DownloadFile("http://yourdomain.com" + relativeUrl, localFilename);
    }

    // Create the options
    var options = new MediaCreatorOptions
        {
            FileBased = false,
            IncludeExtensionInItemName = false,
            KeepExisting = false,
            Versioned = false,
            Destination = sitecorePath + "/" + mediaItemName,
            Database = Factory.GetDatabase("master")
        };

    // Now create the file
    var creator = new MediaCreator();
    var mediaItem = creator.CreateFromFile(localFilename, options);
    return mediaItem;
}

As for generating the dynamic link to the media, I actually haven't found a Sitecore method to do this, so I resorted to the following code:

var extension = !String.IsNullOrEmpty(Settings.Media.RequestExtension)
                                ? Settings.Media.RequestExtension
                                : ((MediaItem)item).Extension;

var dynamicMediaUrl = String.Format(
                         "{0}{1}.{2}", 
                         MediaManager.MediaLinkPrefix, 
                         item.ID.ToShortID(), 
                         extension);
0
jammykam On

No it will not cause any rendering issue apart from the broken links notification as you noted. Also when you select an image in the editor and select to edit the media folder will be at the root rather than at the image itself. But as Derek has noted, the use of dynamic links is an important feature to make sure your links do not break if something is moved or deleted.

I would add to his answer that since you are adding the text via a script you can detect images in the text using HtmlAgilityPack (already used in Sitecore) or FizzlerEx (more similar to jQuery syntax), use the code he provided to upload the images to the media library, grab the GUID and replace the src. Something along the lines of:

string content = "<whatever your html to go in the rich text field>";

HtmlDocument doc = new HtmlDocument();
doc.Load(content);

foreach(HtmlNode img in doc.DocumentElement.SelectNodes("//img[starts-with(@src, '/media/')]")
{
   HtmlAttribute attr = img["src"];
   Item scMediaItem = UploadLocalMedia(attr.Value);
   attr.Value = GetDynamicMediaUrl(scMediaItem);
}