File Titles, Size and Thumbnail MetaData in CQ5

| No Comments | No TrackBacks

Recently, I noticed a quirk in obtaining the file title in CQ5. To be clear, I am only obtaining file metadata here if the file type (dc:format property is a PDF, or 'application/pdf'). The file title (dc:title property in the jcr:content node), is stored as a String. However, when actual metadata is stored with the actual document, it is stored as a String array (String[]). 

Basically, what I have done is to obtain the dc:title metadata property, but if this is empty, then I simply display the file name in place of the title on the front end. What was happening is that, even though a file title was transfered from the document when it was uploaded to the DAM, it could not obtain the dc:title property so it simply displayed my fallback, which is the name of the file. 

The fix is to simply obtain the Asset and determine if it is an Object[] instead of a simple String, and if it is an instance of an Object array, simply get the first item in the array and store that as the title. An example of how you may do this is below:

First, get the Resource from the path, and adapt this to an Asset.

Resource resource = resourceResolver.getResource("/a/path/to/a/file/in/the/dam");
Asset asset = resource.adaptTo(Asset.class);

Next, get the metadata property that contains the title.

String fileName = asset.getMetadataValue("dc:title");

Finally, assess that the fileName is empty or null. (You can use the StringUtils class in the package org.apache.commons.lang3 or your own method.) If it is empty or null, determine if it is an instance of an Object array, and if it is, get the first item in the array as below.

if (fileName instanceof Object[]) {
  Object[] titleArray = (Object[]) asset.getMetadata("dc:title");
  fileName = (titleArray.length > 0) ? titleArray[0].toString() : "";
} else {
  fileName = asset.getName();
}

File size and thumbnails can also be obtained. To get the size of a file, simply adapt it to a Property. The path to the jcr:data node will need to be obtained, similar to the below. Note that one can use the JcrConstants class from the package com.day.cq.commons.jcr in order to get various property names in the Java content repository. The dam creates the following structure to store the uploaded PDF: PDF>jcr:content>renditions>original>jcr:data

Property property = asset.adaptTo(Node.class).getNode(JcrConstants.JCR_CONTENT + "/renditions/original/" + JcrConstants.JCR_CONTENT).getProperty(JcrConstants.JCR_DATA);

To get the size of the file from the property, simply:

fileSize = property.getBinary().getSize();

This file size can then be adapted to display user-friendly units and text, such as "100 MB". 

Thumbnails can be obtained in a similar way, except we do not need to adapt these to Property objects; the path is what we need in order to display the image. Different renditions of the PDF thumbnail are created from the first page in the document when it has been uploaded, and a thumbnail can also be uploaded and overwrite the renditions created at upload time. Decide which rendition will work for you, and simply obtain the path. The dam creates the following structure to store the uploaded PDF's thumbnails: PDF>jcr:content>renditions>.

String thumbnailPath = asset.adaptTo(Node.class).getNode(JcrConstants.JCR_CONTENT + "/renditions/" + "you/rendition/to/use").getPath();

No TrackBacks

TrackBack URL: http://jenikya.com/cgi-bin/mt5/mt-tb.cgi/1145

Leave a comment

Archives

OpenID accepted here Learn more about OpenID