Uploading images and PDF files into Tridion is easy, but sometimes we want to restrict the size allowed for these uploads. For example, it’s not cool to upload a 5MB image to the homepage of our website. In this article I will present an approach using the Event System to give a warning message to users when the filesize is too big. The filesize is configured in the Publication Metadata and also a default one is set in the code.
1. Capture the Save Event for a Component
2. If it is a Multimedia Component and an ‘Interesting Mime Type’ then continue
3. Try to get the Allowed Filesize for the items from the Publication Metadata. If not found, then use the defaults defined in the MaxFilesizeDef.cs file
4. If the file is too big, then throw a new WrongFileSizeException. For example, “Sorry, file exceeds the allowed size for PDF files. The allowed size is 10MB.”
The Code: https://github.com/rcurlette/MMFileSizeValidator#tridionimagesizechecker
Setting up the Publication Metadata Configuration
Create a new Metadata Schema or add the following fields to your existing Metadata Schema. This is not mandatory, but if you want to change the default filesize values, then it is recommended to do this. It also allows you to set very high values (or low values) for 1 Publication.
1. Create a new Schema, Configuration.
– maxImageSize, Text
– maxPdfSize, Text
– maxMovieSize, Text
*These fields must be named like this
2. Create a Metadata Schema with 1 ComponentLink field, ‘Configuration’, linking to the Configuration Schema we created above in step 1
3. Add Configuration Schema to Publication and fill in values.
Configuring the Default Filesizes
The valdiation is enabled for all Publications. If the Metadata is set, it uses those values. However, if no Metadata configuration setting is found on the Publication then the defaults from the code are used. Here are the defaults:
- Image: long _maxImageSize = 4194304; (4 MB)
- PDF: long _maxPdfSize = 20971520; (20 MB)
- Movie: long _maxMovieSize = 41943040; (40 MB)
Configuring the Cache Settings and Configuration Manager
The Configuration Manager class from Mihai comes with a nice XML configuration. See below for it. The filename I have is EventSystemComponentSave.dll.config. This must be the same as your Event System DLL.
<!-- Amount of minutes to keep a loaded Configuration dictionary in the cache -->
<add key="SystemComponentCacheMinutes" value="1"/>
<!-- Where to read the System Component from (Publication, Current, TcmUri or WebDavUrl). Multiple values are comma separated -->
<add key="SystemComponentLocation" value="Publication"/>
<!-- Which metadata field contains a Component Link to the SystemComponent. Multiple fields separated by commas -->
<add key="SystemComponentField" value="Configuration"/>
<!-- Search for System Component recursively (going up into the parent Folder/SG)? -->
<add key="SystemComponentRecursive" value="false"/>
<!-- When merging several System Components, should it override values for the same key? -->
<add key="SystemComponentOverride" value="false"/>
This is the same to install any Event System DLL on your Tridion 2009 / 2011 instance.
1. Compile the assembly (Add References to the Tridion\bin\client\Tridion.ContentManager.dll and Tridion\bin\client\Tridion.Common.dll )
2. Copy EventSystemComponentSave.dll to the Tridion\bin folder (or another folder of your choice).
3. Copy the EventSystemComponentSave.dll.config to the Tridion\bin folder (or another folder of your choice).
4. Register it in the Tridion.ContentManager.config
<add assemblyFileName="C:\Program Files (x86)\Tridion\bin\EventSystemComponentSave.dll" />
5. Open the Services.msc and restart the ‘Tridion Content Manager Service Host’ service.
6. Test by trying to upload a large Multimedia file
Credits and Thanks
I used Dominic Cronin’s excellent ‘Image Size Checker’ (height/width) Event System code for a starting point. https://code.google.com/p/tridion-practice/source/browse/#git%2FImageSizeChecker
I used Mihai’s great ConfigurationManager code to store and retrieve the Publication Metadata settings. https://code.google.com/p/yet-another-tridion-blog/source/browse/trunk/Yet+Another+Event+System/
Adding content validation rules to the Event System is easy and also provides a better QA for the quality of content being uploaded to our website. In this example we saw how we can easily restrict the filesizes of Multimedia items being uploaded into Tridion. We also saw how easy it is to use a Configuration schema in our code and use the Configuration Manager to cache these settings. I hope this article gives you some new ideas and helps you keep the file sizes under reasonable limits in your Tridion system.