Created
June 5, 2017 09:32
-
-
Save quirijnslings/7e447f70bb4323e7d00a441f7c369b3e to your computer and use it in GitHub Desktop.
Example implementating of IBinaryPathProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DD4T.Templates.Base.Utils; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using DD4T.Templates.Base.Builder; | |
using Tridion.ContentManager.Templating; | |
using Tridion.ContentManager; | |
using Tridion.ContentManager.ContentManagement; | |
using Tridion.ContentManager.Templating.Assembly; | |
using DD4T.Templates.Base.Providers; | |
using Tridion.ContentManager.ContentManagement.Fields; | |
using System.Text.RegularExpressions; | |
namespace Acme.SDL.Templates | |
{ | |
/// <summary> | |
/// This is a test to demonstrate the capabilities of the BinaryPathProvider | |
/// The test is successful if: | |
/// - PDFs are published without a TCM URI and into a special structure group configured in metadata on the folder | |
/// - PNGs are published into the regular directory, using the title of the multimedia component as a file name | |
/// - All other file types are published regularly | |
/// </summary> | |
public class FlexibleBinaryPathProvider : BaseBinaryPathProvider | |
{ | |
private new static TemplatingLogger log = TemplatingLogger.GetLogger(typeof(FlexibleBinaryPathProvider)); | |
public FlexibleBinaryPathProvider(Engine engine, Package package) : base(engine, package) | |
{ | |
} | |
public override bool GetStripTcmUrisFromBinaryUrls(Component component) | |
{ | |
if (component.ComponentType != ComponentType.Multimedia) | |
{ | |
throw new TemplatingException($"Unexpected component type: {component.ComponentType} - while it should have been {ComponentType.Multimedia}"); | |
} | |
if (component.BinaryContent.MultimediaType.FileExtensions.Any(a => a.Replace(".", "").ToLower().Contains("pdf"))) | |
{ | |
// this is a PDF, returning TRUE | |
return true; | |
} | |
// this is not a PDF, returning false | |
return false; | |
} | |
public override TcmUri GetTargetStructureGroupUri(string componentUri) | |
{ | |
log.Debug($"Called GetTargetStructureGroupUri with {componentUri}"); | |
Component c = (Component) Engine.GetObject(componentUri); | |
Folder containingFolder = c.OrganizationalItem as Folder; | |
if (containingFolder.Metadata == null || containingFolder.MetadataSchema == null) | |
{ | |
log.Debug("no folder metadata found, calling base method"); | |
return base.GetTargetStructureGroupUri(componentUri); // if no configuration is found, fall back on the default logic | |
} | |
ItemFields metadataFields = new ItemFields(containingFolder.Metadata, containingFolder.MetadataSchema); | |
ItemField targetSGField = metadataFields.Where(a => a.Name == "TargetSG").FirstOrDefault(); | |
if (targetSGField == null) | |
{ | |
return base.GetTargetStructureGroupUri(componentUri); // if no configuration is found, fall back on the default logic | |
} | |
try | |
{ | |
TcmUri targetSGUri = new TcmUri(((TextField)targetSGField).Value); | |
return targetSGUri; | |
} | |
catch | |
{ | |
log.Debug($"{((TextField)targetSGField).Value} is not a valid TcmUri"); | |
return base.GetTargetStructureGroupUri(componentUri); // if no configuration is found, fall back on the default logic | |
} | |
} | |
public override string GetFilename(Component mmComp, string variantId) | |
{ | |
log.Debug($"Called GetFilename for {mmComp.Title}"); | |
if (! mmComp.BinaryContent.MultimediaType.FileExtensions.Any(ext => ext == "png")) | |
{ | |
return base.GetFilename(mmComp, variantId); | |
} | |
Regex re = new Regex("\\W"); | |
return re.Replace(mmComp.Title, "-") + "." + mmComp.BinaryContent.MultimediaType.FileExtensions | |
.Select(a => a.ToLower()).FirstOrDefault(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Quirijn, i am trying to implement this to my project, i was able to upload he new dll with my code, and i can see that my new class is being
BinaryPublisher: Found class to override the binary path provider: Templates.CustomDD4T.FlexibleBinaryPathProvider BinaryPublisher: Found type Templates.CustomDD4T.FlexibleBinaryPathProvider BaseBinaryPathProvider: stripTcmUrisFromBinaryUrls = True FlexibleBinaryPathProvider: Called GetStripTcmUrisFromBinaryUrls Constructor!! BinaryPublisher: Instantiated class Templates.CustomDD4T.FlexibleBinaryPathProvider
I added a log entry in the constructor and in all overridden methods and only the one in the constructor is executed, none of the overridden methods. do you have an idea why this could be happening?
Thank You in advance.
Regards,