I'm always scared when it comes to messing with Sitecore mechanism. But the code below should do the trick.
It replaces the original ProtectedImageLinkRenderer
processor which protects all the media links generated from field renderers (including rich text field).
The _ignoredExtensions
field can be configured via xml config like in the example and contains a list of extensions that should be ignored.
GetProtectedUrl
method checks first if the extension in the generated url is on the list of ignored extensions and if it is, returns unprotected url.
Of course it only works if you have Media.RequestExtension
setting set to empty value and links are generated with proper extensions - otherwise all the links are generated with ashx
extension by default and it would not be possible to deduce the extension.
using System;
using System.Collections.Generic;
using System.Xml;
using Sitecore.Pipelines.RenderField;
using Sitecore.Xml;
namespace SitecorePlayground.Pipelines.renderField
{
public class CustomProtectedImageLinkRenderer : ProtectedImageLinkRenderer
{
private readonly List<string> _ignoredExtensions = new List<string>();
protected override string GetProtectedUrl(string url)
{
var extension = url.Substring(Math.Max(0, url.LastIndexOf('.'))).TrimStart('.');
int questionMarkIndex;
if ((questionMarkIndex = extension.IndexOf('?')) > -1)
{
extension = extension.Substring(0, questionMarkIndex);
}
if (_ignoredExtensions.Contains(extension.ToLower()))
return url;
return base.GetProtectedUrl(url);
}
public virtual void AddIgnoredExtension(XmlNode configNode)
{
string extension = XmlUtil.GetValue(configNode);
if (!string.IsNullOrWhiteSpace(extension))
{
_ignoredExtensions.Add(extension.ToLower());
}
}
}
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<renderField>
<processor
patch:instead="processor[@type='Sitecore.Pipelines.RenderField.ProtectedImageLinkRenderer, Sitecore.Kernel']"
type="SitecorePlayground.Pipelines.renderField.CustomProtectedImageLinkRenderer, SitecorePlayground">
<IgnoredExtensions hint="raw:AddIgnoredExtension">
<wordDocument>docx</wordDocument>
<pdfDocument>pdf</pdfDocument>
</IgnoredExtensions>
</processor>
</renderField>
</pipelines>
</sitecore>
</configuration>