1 package org.collectionspace.services.nuxeo.extension.thumbnail;
4 import java.io.Serializable;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
10 import org.nuxeo.ecm.core.api.Blob;
11 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
12 import org.nuxeo.ecm.core.api.impl.blob.FileBlob;
13 //import org.nuxeo.ecm.core.api.impl.blob.StreamingBlob;
14 import org.nuxeo.ecm.core.convert.api.ConversionException;
15 import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
16 import org.nuxeo.ecm.core.convert.extension.Converter;
17 import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
18 //import org.nuxeo.ecm.core.storage.sql.coremodel.SQLBlob;
19 import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
20 import org.nuxeo.ecm.platform.commandline.executor.api.CommandAvailability;
21 import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
22 import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
23 import org.nuxeo.ecm.platform.picture.core.im.IMImageUtils;
25 import org.nuxeo.runtime.api.Framework;
26 //import org.nuxeo.runtime.services.streaming.FileSource;
27 //import org.nuxeo.runtime.services.streaming.StreamSource;
29 public class ThumbnailConverter extends IMImageUtils implements Converter {
30 private static final Log logger = LogFactory.getLog(ThumbnailConverter.class);
33 public BlobHolder convert(BlobHolder blobHolder,
34 Map<String, Serializable> parameters) throws ConversionException {
36 // Make sure the toThumbnail command is available
37 CommandLineExecutorService cles = Framework
38 .getLocalService(CommandLineExecutorService.class);
39 CommandAvailability commandAvailability = cles
40 .getCommandAvailability("toThumbnail");
41 if (!commandAvailability.isAvailable()) {
44 // get the input and output of the command
45 Blob blob = blobHolder.getBlob();
46 File inputFile = null;
47 if (blob instanceof FileBlob) {
48 inputFile = ((FileBlob) blob).getFile();
50 // else if (blob instanceof SQLBlob) {
51 // StreamSource source = ((SQLBlob) blob).getBinary()
52 // .getStreamSource();
53 // inputFile = ((FileSource) source).getFile();
54 // } else if (blob instanceof StreamingBlob) {
55 // StreamingBlob streamingBlob = ((StreamingBlob) blob);
56 // if (!streamingBlob.isPersistent()) {
57 // streamingBlob.persist();
59 // StreamSource source = streamingBlob.getStreamSource();
60 // inputFile = ((FileSource) source).getFile();
63 if (inputFile == null) {
64 logger.error("Blob from blob holder was null.");
65 return null; // Add a log message here
67 CmdParameters params = new CmdParameters();
68 File outputFile = File.createTempFile("nuxeoImageTarget", "."
70 String size = ThumbnailConstants.THUMBNAIL_DEFAULT_SIZE;
71 if (parameters != null) {
72 if (parameters.containsKey(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME)) {
73 size = (String) parameters.get(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME);
76 params.addNamedParameter(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME, size);
77 params.addNamedParameter("inputFilePath", inputFile);
78 params.addNamedParameter("outputFilePath", outputFile);
80 ExecResult res = cles.execCommand("toThumbnail", params);
81 if (!res.isSuccessful()) {
84 Blob targetBlob = new FileBlob(outputFile);
85 Framework.trackFile(outputFile, targetBlob);
86 return new SimpleCachableBlobHolder(targetBlob);
87 } catch (Exception e) {
88 logger.trace("Could not create a thumbnail image using the Nuxeo conversion service", e);
89 throw new ConversionException("Thumbnail conversion has failed", e);
94 public void init(ConverterDescriptor descriptor) {