mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-10-08 14:51:49 +00:00
Updated image transformers to have formalised parameters, support for resize image transformations, get and update thumbnail implementation
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@8779 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -25,33 +25,52 @@
|
||||
package org.alfresco.repo.thumbnail;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ContentData;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.thumbnail.AcceptOptions;
|
||||
import org.alfresco.service.cmr.thumbnail.GenerateOptions;
|
||||
import org.alfresco.service.cmr.repository.TransformationOptions;
|
||||
import org.alfresco.service.cmr.thumbnail.CreateOptions;
|
||||
import org.alfresco.service.cmr.thumbnail.ThumbnailException;
|
||||
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
|
||||
import org.alfresco.service.cmr.thumbnail.GenerateOptions.ParentAssociationDetails;
|
||||
import org.alfresco.service.cmr.thumbnail.CreateOptions.ParentAssociationDetails;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.service.namespace.RegexQNamePattern;
|
||||
import org.alfresco.util.GUID;
|
||||
import org.alfresco.util.ParameterCheck;
|
||||
|
||||
/**
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ThumbnailServiceImpl implements ThumbnailService
|
||||
{
|
||||
/** Error messages */
|
||||
private static final String ERR_NO_CREATE = "Thumbnail could not be created as required transformation is not supported.";
|
||||
private static final String ERR_DUPLICATE_NAME = "Thumbnail could not be created because of a duplicate name";
|
||||
private static final String ERR_NO_PARENT = "Thumbnail has no parent so update cannot take place.";
|
||||
private static final String ERR_TOO_PARENT = "Thumbnail has more than one source content node. This is invalid so update cannot take place.";
|
||||
private static final String ERR_TRANS_CLASS = "Unable to create transformation options from saved class. Update of thumbnail cannnot take place.";
|
||||
|
||||
/** Node service */
|
||||
private NodeService nodeService;
|
||||
|
||||
/** Content service */
|
||||
private ContentService contentService;
|
||||
|
||||
/** Mimetype map */
|
||||
private MimetypeMap mimetypeMap;
|
||||
|
||||
/**
|
||||
* Set the node service
|
||||
*
|
||||
@@ -72,13 +91,34 @@ public class ThumbnailServiceImpl implements ThumbnailService
|
||||
this.contentService = contentService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the mimetype map
|
||||
*
|
||||
* @param mimetypeMap the mimetype map
|
||||
*/
|
||||
public void setMimetypeMap(MimetypeMap mimetypeMap)
|
||||
{
|
||||
this.mimetypeMap = mimetypeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.thumbnail.ThumbnailService#createThumbnail(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, org.alfresco.service.cmr.thumbnail.GenerateOptions)
|
||||
*/
|
||||
public NodeRef createThumbnail(NodeRef node, QName contentProperty, GenerateOptions createOptions)
|
||||
public NodeRef createThumbnail(NodeRef node, QName contentProperty, CreateOptions createOptions)
|
||||
{
|
||||
// Parameter check
|
||||
ParameterCheck.mandatory("node", node);
|
||||
ParameterCheck.mandatory("contentProperty", contentProperty);
|
||||
|
||||
NodeRef thumbnail = null;
|
||||
|
||||
// Check for duplicate names
|
||||
if (createOptions.getThumbnailName() != null && getThumbnailByName(node, contentProperty, createOptions.getThumbnailName()) != null)
|
||||
{
|
||||
// We can't continue because there is already an thumnail with the given name for that content property
|
||||
throw new ThumbnailException(ERR_DUPLICATE_NAME);
|
||||
}
|
||||
|
||||
// Apply the thumbnailed aspect to the node if it doesn't already have it
|
||||
if (this.nodeService.hasAspect(node, ContentModel.ASPECT_THUMBNAILED) == false)
|
||||
{
|
||||
@@ -87,13 +127,25 @@ public class ThumbnailServiceImpl implements ThumbnailService
|
||||
|
||||
// Get the name of the thumbnail and add to properties map
|
||||
String thumbnailName = createOptions.getThumbnailName();
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(2);
|
||||
if (thumbnailName == null)
|
||||
{
|
||||
thumbnailName = GUID.generate();
|
||||
}
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
|
||||
properties.put(ContentModel.PROP_NAME, thumbnailName);
|
||||
// TODO .. somehow we need to store the details of the thumbnail on the node so we can regen the thumbnail later ..
|
||||
else
|
||||
{
|
||||
String thumbnailFileName = generateThumbnailFileName(thumbnailName, createOptions.getDestinationMimetype());
|
||||
properties.put(ContentModel.PROP_NAME, thumbnailFileName);
|
||||
}
|
||||
|
||||
// Add the name of the content property
|
||||
properties.put(ContentModel.PROP_CONTENT_PROPERTY_NAME, contentProperty);
|
||||
|
||||
// Add the class name of the transformation options
|
||||
if (createOptions.getTransformationOptions() != null)
|
||||
{
|
||||
properties.put(ContentModel.PROP_TRANSFORMATION_OPTIONS_CLASS, createOptions.getTransformationOptions().getClass().getName());
|
||||
}
|
||||
|
||||
// See if parent association details have been specified for the thumbnail
|
||||
ParentAssociationDetails assocDetails = createOptions.getParentAssociationDetails();
|
||||
@@ -125,18 +177,41 @@ public class ThumbnailServiceImpl implements ThumbnailService
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, thumbnailName));
|
||||
}
|
||||
|
||||
// TODO .. do the work of actually creating the thumbnail content ...
|
||||
// Get the content reader and writer for content nodes
|
||||
ContentReader reader = this.contentService.getReader(node, contentProperty);
|
||||
ContentWriter writer = this.contentService.getWriter(thumbnail, ContentModel.PROP_CONTENT, true);
|
||||
writer.setMimetype(createOptions.getDestinationMimetype());
|
||||
writer.setEncoding(reader.getEncoding());
|
||||
|
||||
// Catch the failure to create the thumbnail
|
||||
if (this.contentService.isTransformable(reader, writer, createOptions.getTransformationOptions()) == false)
|
||||
{
|
||||
// Throw exception indicating that the thumbnail could not be created
|
||||
throw new ThumbnailException(ERR_NO_CREATE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Do the thumnail transformation
|
||||
this.contentService.transform(reader, writer, createOptions.getTransformationOptions());
|
||||
|
||||
// Store the transformation options on the thumbnail
|
||||
createOptions.getTransformationOptions().saveToNode(thumbnail, this.nodeService);
|
||||
}
|
||||
|
||||
// Return the created thumbnail
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.thumbnail.ThumbnailService#getThumbnails(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, org.alfresco.service.cmr.thumbnail.AcceptOptions)
|
||||
* Generates the thumbnail name from the name and destination mimertype
|
||||
*
|
||||
* @param thumbnailName the thumbnail name
|
||||
* @param destinationMimetype the destination name
|
||||
* @return String the thumbnail file name
|
||||
*/
|
||||
public List<NodeRef> getThumbnails(NodeRef node, QName contentProperty, AcceptOptions acceptOptions)
|
||||
private String generateThumbnailFileName(String thumbnailName, String destinationMimetype)
|
||||
{
|
||||
return null;
|
||||
return thumbnailName + "." + this.mimetypeMap.getExtension(destinationMimetype);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,6 +219,179 @@ public class ThumbnailServiceImpl implements ThumbnailService
|
||||
*/
|
||||
public void updateThumbnail(NodeRef thumbnail)
|
||||
{
|
||||
// First check that we are dealing with a thumbnail
|
||||
if (ContentModel.TYPE_THUMBNAIL.equals(this.nodeService.getType(thumbnail)) == true)
|
||||
{
|
||||
// Get the transformation options
|
||||
TransformationOptions options = null;
|
||||
String transformationOptionsClassName = (String)this.nodeService.getProperty(thumbnail, ContentModel.PROP_TRANSFORMATION_OPTIONS_CLASS);
|
||||
if (transformationOptionsClassName == null)
|
||||
{
|
||||
options = new TransformationOptions();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create an options object of the type specified on the thumbnail
|
||||
try
|
||||
{
|
||||
Class transformationClass = Class.forName(transformationOptionsClassName);
|
||||
options = (TransformationOptions)transformationClass.newInstance();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new ThumbnailException(ERR_TRANS_CLASS);
|
||||
}
|
||||
|
||||
// Populate the options from the node
|
||||
options.populateFromNode(thumbnail, this.nodeService);
|
||||
}
|
||||
|
||||
// Get the node that is the source of the thumbnail
|
||||
NodeRef node = null;
|
||||
List<ChildAssociationRef> parents = this.nodeService.getParentAssocs(thumbnail, ContentModel.ASSOC_THUMBNAILS, RegexQNamePattern.MATCH_ALL);
|
||||
if (parents.size() == 0)
|
||||
{
|
||||
throw new ThumbnailException(ERR_NO_PARENT);
|
||||
}
|
||||
else if (parents.size() != 1)
|
||||
{
|
||||
throw new ThumbnailException(ERR_TOO_PARENT);
|
||||
}
|
||||
else
|
||||
{
|
||||
node = parents.get(0).getParentRef();
|
||||
}
|
||||
|
||||
// Get the content property
|
||||
QName contentProperty = (QName)this.nodeService.getProperty(thumbnail, ContentModel.PROP_CONTENT_PROPERTY_NAME);
|
||||
|
||||
// Get the reader and writer
|
||||
ContentReader reader = this.contentService.getReader(node, contentProperty);
|
||||
ContentWriter writer = this.contentService.getWriter(node, ContentModel.PROP_CONTENT, true);
|
||||
|
||||
// Set the basic detail of the transformation options
|
||||
options.setSourceNodeRef(node);
|
||||
options.setSourceContentProperty(contentProperty);
|
||||
options.setTargetNodeRef(thumbnail);
|
||||
options.setTargetContentProperty(ContentModel.PROP_CONTENT);
|
||||
|
||||
// Catch the failure to create the thumbnail
|
||||
if (this.contentService.isTransformable(reader, writer, options) == false)
|
||||
{
|
||||
// Throw exception indicating that the thumbnail could not be created
|
||||
throw new ThumbnailException(ERR_NO_CREATE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Do the thumnail transformation
|
||||
this.contentService.transform(reader, writer, options);
|
||||
}
|
||||
}
|
||||
// TODO else should we throw an exception?
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.thumbnail.ThumbnailService#getThumbnailByName(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.lang.String)
|
||||
*/
|
||||
public NodeRef getThumbnailByName(NodeRef node, QName contentProperty, String thumbnailName)
|
||||
{
|
||||
NodeRef thumbnail = null;
|
||||
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// Since there is not an easy alternative and for clarity the node service is being used to retrieve the thumbnails.
|
||||
// If retrieval performance becomes an issue then this code can be replaced
|
||||
//
|
||||
|
||||
// Check that the node has the thumbnailed aspect applied
|
||||
if (nodeService.hasAspect(node, ContentModel.ASPECT_THUMBNAILED) == true)
|
||||
{
|
||||
// Get all the thumnails that match the thumbnail name
|
||||
List<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(node, ContentModel.ASSOC_THUMBNAILS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, thumbnailName));
|
||||
for (ChildAssociationRef assoc : assocs)
|
||||
{
|
||||
// Check the child to see if it matches the content property we are concerned about.
|
||||
// We can assume there will only ever be one per content property since createThumbnail enforces this.
|
||||
NodeRef child = assoc.getChildRef();
|
||||
if (contentProperty.equals(this.nodeService.getProperty(child, ContentModel.PROP_CONTENT_PROPERTY_NAME)) == true)
|
||||
{
|
||||
thumbnail = child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return thumbnail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.thumbnail.ThumbnailService#getThumbnails(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.lang.String, org.alfresco.service.cmr.repository.TransformationOptions)
|
||||
*/
|
||||
public List<NodeRef> getThumbnails(NodeRef node, QName contentProperty, String mimetype, TransformationOptions options)
|
||||
{
|
||||
List<NodeRef> thumbnails = new ArrayList<NodeRef>(5);
|
||||
|
||||
//
|
||||
// NOTE:
|
||||
//
|
||||
// Since there is not an easy alternative and for clarity the node service is being used to retrieve the thumbnails.
|
||||
// If retrieval performance becomes an issue then this code can be replaced
|
||||
//
|
||||
|
||||
// Check that the node has the thumbnailed aspect applied
|
||||
if (nodeService.hasAspect(node, ContentModel.ASPECT_THUMBNAILED) == true)
|
||||
{
|
||||
// Get all the thumnails that match the thumbnail name
|
||||
List<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(node, ContentModel.ASSOC_THUMBNAILS, RegexQNamePattern.MATCH_ALL);
|
||||
for (ChildAssociationRef assoc : assocs)
|
||||
{
|
||||
// Check the child to see if it matches the content property we are concerned about.
|
||||
// We can assume there will only ever be one per content property since createThumbnail enforces this.
|
||||
NodeRef child = assoc.getChildRef();
|
||||
if (contentProperty.equals(this.nodeService.getProperty(child, ContentModel.PROP_CONTENT_PROPERTY_NAME)) == true &&
|
||||
matchMimetypeOptions(child, mimetype, options) == true)
|
||||
{
|
||||
thumbnails.add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return thumbnails;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the thumbnail meta-data matches the given mimetype and options
|
||||
*
|
||||
* @param thumbnail thumbnail node reference
|
||||
* @param mimetype mimetype
|
||||
* @param options transformation options
|
||||
* @return boolean true if the mimetype and options match the thumbnail metadata, false otherwise
|
||||
*/
|
||||
private boolean matchMimetypeOptions(NodeRef thumbnail, String mimetype, TransformationOptions options)
|
||||
{
|
||||
boolean result = true;
|
||||
|
||||
// Check the mimetype
|
||||
String thumbnailMimetype = ((ContentData)this.nodeService.getProperty(thumbnail, ContentModel.PROP_CONTENT)).getMimetype();
|
||||
if (mimetype.equals(thumbnailMimetype) == true)
|
||||
{
|
||||
// TODO continue to check options ...
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.service.cmr.thumbnail.ThumbnailService#getThumbnails(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.lang.String)
|
||||
*/
|
||||
public List<NodeRef> getThumbnails(NodeRef node, QName contentProperty, String mimetype)
|
||||
{
|
||||
return getThumbnails(node, contentProperty, mimetype, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -24,37 +24,309 @@
|
||||
*/
|
||||
package org.alfresco.repo.thumbnail;
|
||||
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
|
||||
import org.alfresco.repo.content.transform.magick.ImageResizeOptions;
|
||||
import org.alfresco.repo.content.transform.magick.ImageTransformationOptions;
|
||||
import org.alfresco.service.cmr.repository.ChildAssociationRef;
|
||||
import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.TransformationOptions;
|
||||
import org.alfresco.service.cmr.thumbnail.CreateOptions;
|
||||
import org.alfresco.service.cmr.thumbnail.ThumbnailException;
|
||||
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.alfresco.service.namespace.RegexQNamePattern;
|
||||
import org.alfresco.util.BaseAlfrescoSpringTest;
|
||||
|
||||
/**
|
||||
* Thumbnail service implementation unit test
|
||||
*
|
||||
* @author Roy Wetherall
|
||||
*/
|
||||
public class ThumbnailServiceImplTest extends BaseSpringTest
|
||||
public class ThumbnailServiceImplTest extends BaseAlfrescoSpringTest
|
||||
{
|
||||
private NodeService nodeService;
|
||||
private ContentService contentService;
|
||||
private ThumbnailService thumbnailService;
|
||||
private ThumbnailService thumbnailService;
|
||||
private MimetypeMap mimetypeMap;
|
||||
private NodeRef folder;
|
||||
|
||||
/**
|
||||
* Called during the transaction setup
|
||||
*/
|
||||
protected void onSetUpInTransaction() throws Exception
|
||||
{
|
||||
// Get required service implementations
|
||||
this.nodeService = (NodeService)this.applicationContext.getBean("NodeService");
|
||||
this.contentService = (ContentService)this.applicationContext.getBean("ContentService");
|
||||
this.thumbnailService = (ThumbnailService)this.applicationContext.getBean("ThumbnailService");
|
||||
super.onSetUpInTransaction();
|
||||
|
||||
// Get the required services
|
||||
this.thumbnailService = (ThumbnailService)this.applicationContext.getBean("ThumbnailService");
|
||||
this.mimetypeMap = (MimetypeMap)this.applicationContext.getBean("mimetypeService");
|
||||
|
||||
// Create a folder and some content
|
||||
Map<QName, Serializable> folderProps = new HashMap<QName, Serializable>(1);
|
||||
folderProps.put(ContentModel.PROP_NAME, "testFolder");
|
||||
this.folder = this.nodeService.createNode(
|
||||
this.rootNodeRef,
|
||||
ContentModel.ASSOC_CHILDREN,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testFolder"),
|
||||
ContentModel.TYPE_FOLDER).getChildRef();
|
||||
}
|
||||
|
||||
public void testCreateThumbnail() throws Exception
|
||||
public void testCreateThumbnailFromImage() throws Exception
|
||||
{
|
||||
NodeRef jpgOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_JPEG);
|
||||
NodeRef gifOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_GIF);
|
||||
|
||||
// ===== small: 64x64, marked as thumbnail ====
|
||||
|
||||
ImageResizeOptions imageResizeOptions = new ImageResizeOptions();
|
||||
imageResizeOptions.setWidth(64);
|
||||
imageResizeOptions.setHeight(64);
|
||||
imageResizeOptions.setResizeToThumbnail(true);
|
||||
ImageTransformationOptions imageTransformationOptions = new ImageTransformationOptions();
|
||||
imageTransformationOptions.setResizeOptions(imageResizeOptions);
|
||||
CreateOptions createOptions = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions,
|
||||
"small");
|
||||
NodeRef thumbnail1 = this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions);
|
||||
assertNotNull(thumbnail1);
|
||||
checkThumbnailed(jpgOrig, "small");
|
||||
checkThumbnail(thumbnail1, imageTransformationOptions);
|
||||
outputThumbnailTempContentLocation(thumbnail1, "jpg", "small - 64x64, marked as thumbnail");
|
||||
|
||||
// ===== small2: 64x64, aspect not maintained ====
|
||||
|
||||
ImageResizeOptions imageResizeOptions2 = new ImageResizeOptions();
|
||||
imageResizeOptions2.setWidth(64);
|
||||
imageResizeOptions2.setHeight(64);
|
||||
imageResizeOptions2.setMaintainAspectRatio(false);
|
||||
ImageTransformationOptions imageTransformationOptions2 = new ImageTransformationOptions();
|
||||
imageTransformationOptions2.setResizeOptions(imageResizeOptions2);
|
||||
CreateOptions createOptions2 = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions2,
|
||||
"small2");
|
||||
NodeRef thumbnail2 = this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions2);
|
||||
checkThumbnailed(jpgOrig, "small2");
|
||||
checkThumbnail(thumbnail2, imageTransformationOptions2);
|
||||
outputThumbnailTempContentLocation(thumbnail2, "jpg", "small2 - 64x64, aspect not maintained");
|
||||
|
||||
// ===== half: 50%x50 =====
|
||||
|
||||
ImageResizeOptions imageResizeOptions3 = new ImageResizeOptions();
|
||||
imageResizeOptions3.setWidth(50);
|
||||
imageResizeOptions3.setHeight(50);
|
||||
imageResizeOptions3.setPercentResize(true);
|
||||
ImageTransformationOptions imageTransformationOptions3 = new ImageTransformationOptions();
|
||||
imageTransformationOptions3.setResizeOptions(imageResizeOptions3);
|
||||
CreateOptions createOptions3 = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions3,
|
||||
"half");
|
||||
NodeRef thumbnail3 = this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions3);
|
||||
checkThumbnailed(jpgOrig, "half");
|
||||
checkThumbnail(thumbnail3, imageTransformationOptions3);
|
||||
outputThumbnailTempContentLocation(thumbnail3, "jpg", "half - 50%x50%");
|
||||
|
||||
|
||||
// ===== half2: 50%x50 from gif =====
|
||||
|
||||
ImageResizeOptions imageResizeOptions4 = new ImageResizeOptions();
|
||||
imageResizeOptions4.setWidth(50);
|
||||
imageResizeOptions4.setHeight(50);
|
||||
imageResizeOptions4.setPercentResize(true);
|
||||
ImageTransformationOptions imageTransformationOptions4 = new ImageTransformationOptions();
|
||||
imageTransformationOptions4.setResizeOptions(imageResizeOptions4);
|
||||
CreateOptions createOptions4 = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions4,
|
||||
"half2");
|
||||
NodeRef thumbnail4 = this.thumbnailService.createThumbnail(gifOrig, ContentModel.PROP_CONTENT, createOptions4);
|
||||
checkThumbnailed(gifOrig, "half2");
|
||||
checkThumbnail(thumbnail4, imageTransformationOptions4);
|
||||
outputThumbnailTempContentLocation(thumbnail4, "jpg", "half2 - 50%x50%, from gif");
|
||||
|
||||
}
|
||||
|
||||
public void testDuplicationNames()
|
||||
throws Exception
|
||||
{
|
||||
NodeRef jpgOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_JPEG);
|
||||
ImageResizeOptions imageResizeOptions = new ImageResizeOptions();
|
||||
imageResizeOptions.setWidth(64);
|
||||
imageResizeOptions.setHeight(64);
|
||||
imageResizeOptions.setResizeToThumbnail(true);
|
||||
ImageTransformationOptions imageTransformationOptions = new ImageTransformationOptions();
|
||||
imageTransformationOptions.setResizeOptions(imageResizeOptions);
|
||||
CreateOptions createOptions = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions,
|
||||
"small");
|
||||
NodeRef thumbnail1 = this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions);
|
||||
assertNotNull(thumbnail1);
|
||||
checkThumbnailed(jpgOrig, "small");
|
||||
checkThumbnail(thumbnail1, imageTransformationOptions);
|
||||
|
||||
try
|
||||
{
|
||||
this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions);
|
||||
fail("A duplicate exception should have been raised");
|
||||
}
|
||||
catch (ThumbnailException exception)
|
||||
{
|
||||
// OK since this should have been thrown
|
||||
}
|
||||
}
|
||||
|
||||
public void testThumbnailUpdate()
|
||||
throws Exception
|
||||
{
|
||||
// First create a thumbnail
|
||||
NodeRef jpgOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_JPEG);
|
||||
ImageResizeOptions imageResizeOptions = new ImageResizeOptions();
|
||||
imageResizeOptions.setWidth(64);
|
||||
imageResizeOptions.setHeight(64);
|
||||
imageResizeOptions.setResizeToThumbnail(true);
|
||||
ImageTransformationOptions imageTransformationOptions = new ImageTransformationOptions();
|
||||
imageTransformationOptions.setResizeOptions(imageResizeOptions);
|
||||
CreateOptions createOptions = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions,
|
||||
"small");
|
||||
NodeRef thumbnail1 = this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions);
|
||||
|
||||
// Update the thumbnail
|
||||
this.thumbnailService.updateThumbnail(thumbnail1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void testGetThumbnailByName()
|
||||
throws Exception
|
||||
{
|
||||
NodeRef jpgOrig = createOrigionalContent(this.folder, MimetypeMap.MIMETYPE_IMAGE_JPEG);
|
||||
|
||||
// Check for missing thumbnail
|
||||
NodeRef result1 = this.thumbnailService.getThumbnailByName(jpgOrig, ContentModel.PROP_CONTENT, "small");
|
||||
assertNull("The thumbnail 'small' should have been missing", result1);
|
||||
|
||||
// Create the thumbnail
|
||||
ImageResizeOptions imageResizeOptions = new ImageResizeOptions();
|
||||
imageResizeOptions.setWidth(64);
|
||||
imageResizeOptions.setHeight(64);
|
||||
imageResizeOptions.setResizeToThumbnail(true);
|
||||
ImageTransformationOptions imageTransformationOptions = new ImageTransformationOptions();
|
||||
imageTransformationOptions.setResizeOptions(imageResizeOptions);
|
||||
CreateOptions createOptions = new CreateOptions(
|
||||
MimetypeMap.MIMETYPE_IMAGE_JPEG,
|
||||
imageTransformationOptions,
|
||||
"small");
|
||||
this.thumbnailService.createThumbnail(jpgOrig, ContentModel.PROP_CONTENT, createOptions);
|
||||
|
||||
// Try and retrieve the thumbnail
|
||||
NodeRef result2 = this.thumbnailService.getThumbnailByName(jpgOrig, ContentModel.PROP_CONTENT, "small");
|
||||
assertNotNull(result2);
|
||||
checkThumbnail(result2, imageTransformationOptions);
|
||||
|
||||
// Check for an other thumbnail that doesn't exist
|
||||
NodeRef result3 = this.thumbnailService.getThumbnailByName(jpgOrig, ContentModel.PROP_CONTENT, "anotherone");
|
||||
assertNull("The thumbnail 'anotherone' should have been missing", result3);
|
||||
}
|
||||
|
||||
// TODO test getThumbnails
|
||||
|
||||
private void checkThumbnailed(NodeRef thumbnailed, String assocName)
|
||||
{
|
||||
assertTrue("Thumbnailed aspect should have been applied", this.nodeService.hasAspect(thumbnailed, ContentModel.ASPECT_THUMBNAILED));
|
||||
List<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(thumbnailed, RegexQNamePattern.MATCH_ALL, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, assocName));
|
||||
assertNotNull(assocs);
|
||||
assertEquals(1, assocs.size());
|
||||
}
|
||||
|
||||
private void checkThumbnail(NodeRef thumbnail, TransformationOptions transformationOptions)
|
||||
{
|
||||
// Check the thumbnail is of the correct type
|
||||
assertEquals(ContentModel.TYPE_THUMBNAIL, this.nodeService.getType(thumbnail));
|
||||
|
||||
// Check the meta data on the thumnail
|
||||
assertEquals(ContentModel.PROP_CONTENT, this.nodeService.getProperty(thumbnail, ContentModel.PROP_CONTENT_PROPERTY_NAME));
|
||||
assertEquals(transformationOptions.getClass().getName(), this.nodeService.getProperty(thumbnail, ContentModel.PROP_TRANSFORMATION_OPTIONS_CLASS));
|
||||
|
||||
if (transformationOptions instanceof ImageTransformationOptions)
|
||||
{
|
||||
ImageTransformationOptions imageTransformationOptions = (ImageTransformationOptions)transformationOptions;
|
||||
assertTrue(this.nodeService.hasAspect(thumbnail, ImageTransformationOptions.ASPECT_IMAGE_TRANSFORATION_OPTIONS));
|
||||
assertEquals(
|
||||
imageTransformationOptions.getCommandOptions(),
|
||||
this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_COMMAND_OPTIONS));
|
||||
ImageResizeOptions resizeOptions = imageTransformationOptions.getResizeOptions();
|
||||
if (resizeOptions != null)
|
||||
{
|
||||
assertTrue((Boolean)this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE));
|
||||
assertEquals(
|
||||
imageTransformationOptions.getResizeOptions().getHeight(),
|
||||
this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE_HEIGHT));
|
||||
assertEquals(
|
||||
imageTransformationOptions.getResizeOptions().getWidth(),
|
||||
this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE_WIDTH));
|
||||
assertEquals(
|
||||
imageTransformationOptions.getResizeOptions().isMaintainAspectRatio(),
|
||||
this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE_MAINTAIN_ASPECT_RATIO));
|
||||
assertEquals(
|
||||
imageTransformationOptions.getResizeOptions().isPercentResize(),
|
||||
this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE_PERCENT));
|
||||
assertEquals(
|
||||
imageTransformationOptions.getResizeOptions().isResizeToThumbnail(),
|
||||
this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE_TO_THUMBNAIL));
|
||||
}
|
||||
else
|
||||
{
|
||||
assertFalse((Boolean)this.nodeService.getProperty(thumbnail, ImageTransformationOptions.PROP_RESIZE));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void outputThumbnailTempContentLocation(NodeRef thumbnail, String ext, String message)
|
||||
throws IOException
|
||||
{
|
||||
File tempFile = File.createTempFile("thumbnailServiceImpTest", "." + ext);
|
||||
ContentReader reader = this.contentService.getReader(thumbnail, ContentModel.PROP_CONTENT);
|
||||
reader.getContent(tempFile);
|
||||
System.out.println(message + ": " + tempFile.getPath());
|
||||
}
|
||||
|
||||
private NodeRef createOrigionalContent(NodeRef folder, String mimetype)
|
||||
throws IOException
|
||||
{
|
||||
String ext = this.mimetypeMap.getExtension(mimetype);
|
||||
File origFile = AbstractContentTransformerTest.loadQuickTestFile(ext);
|
||||
|
||||
Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
|
||||
props.put(ContentModel.PROP_NAME, "origional." + ext);
|
||||
NodeRef node = this.nodeService.createNode(
|
||||
folder,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "origional." + ext),
|
||||
ContentModel.TYPE_CONTENT,
|
||||
props).getChildRef();
|
||||
|
||||
ContentWriter writer = this.contentService.getWriter(node, ContentModel.PROP_CONTENT, true);
|
||||
writer.setMimetype(mimetype);
|
||||
writer.setEncoding("UTF-8");
|
||||
writer.putContent(origFile);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user