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();
49 } else if (blob instanceof SQLBlob) {
50 StreamSource source = ((SQLBlob) blob).getBinary()
52 inputFile = ((FileSource) source).getFile();
53 } else if (blob instanceof StreamingBlob) {
54 StreamingBlob streamingBlob = ((StreamingBlob) blob);
55 if (!streamingBlob.isPersistent()) {
56 streamingBlob.persist();
58 StreamSource source = streamingBlob.getStreamSource();
59 inputFile = ((FileSource) source).getFile();
61 if (inputFile == null) {
62 return null; // Add a log message here
64 CmdParameters params = new CmdParameters();
65 File outputFile = File.createTempFile("nuxeoImageTarget", "."
67 String size = ThumbnailConstants.THUMBNAIL_DEFAULT_SIZE;
68 if (parameters != null) {
69 if (parameters.containsKey(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME)) {
70 size = (String) parameters.get(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME);
73 params.addNamedParameter(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME, size);
74 params.addNamedParameter("inputFilePath", inputFile);
75 params.addNamedParameter("outputFilePath", outputFile);
77 ExecResult res = cles.execCommand("toThumbnail", params);
78 if (!res.isSuccessful()) {
81 Blob targetBlob = new FileBlob(outputFile);
82 Framework.trackFile(outputFile, targetBlob);
83 return new SimpleCachableBlobHolder(targetBlob);
84 } catch (Exception e) {
85 logger.trace("Could not create a thumbnail image using the Nuxeo conversion service", e);
86 throw new ConversionException("Thumbnail conversion has failed", e);
91 public void init(ConverterDescriptor descriptor) {