001/* 002 * Copyright (c) 2006-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Laurent Doguin <ldoguin@nuxeo.com> 011 * Vladimir Pasquier <vpasquier@nuxeo.com> 012 * 013 */ 014package org.nuxeo.ecm.platform.thumbnail.converter; 015 016import java.io.IOException; 017import java.io.Serializable; 018import java.util.Map; 019 020import org.nuxeo.ecm.core.api.Blob; 021import org.nuxeo.ecm.core.api.Blobs; 022import org.nuxeo.ecm.core.api.CloseableFile; 023import org.nuxeo.ecm.core.api.NuxeoException; 024import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 025import org.nuxeo.ecm.core.convert.api.ConversionException; 026import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 027import org.nuxeo.ecm.core.convert.extension.Converter; 028import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 029import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters; 030import org.nuxeo.ecm.platform.commandline.executor.api.CommandAvailability; 031import org.nuxeo.ecm.platform.commandline.executor.api.CommandException; 032import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService; 033import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable; 034import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * Converter bean managing the thumbnail conversion for picture documents 039 * 040 * @since 5.7 041 */ 042public class ThumbnailDocumentConverter implements Converter { 043 044 public static final String THUMBNAIL_CONVERTER_NAME = "toThumbnail"; 045 046 public static final String THUMBNAIL_SIZE_PARAMETER_NAME = "size"; 047 048 public static final String THUMBNAIL_DEFAULT_SIZE = "100x100"; 049 050 public static final String THUMBNAIL_COMMAND = "toThumbnail"; 051 052 @Override 053 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 054 try { 055 // Make sure the toThumbnail command is available 056 CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class); 057 CommandAvailability commandAvailability = cles.getCommandAvailability(THUMBNAIL_COMMAND); 058 if (!commandAvailability.isAvailable()) { 059 return null; 060 } 061 // get the input and output of the command 062 Blob blob = blobHolder.getBlob(); 063 064 Blob targetBlob = Blobs.createBlobWithExtension(".png"); 065 targetBlob.setMimeType("image/png"); 066 try (CloseableFile source = blob.getCloseableFile()) { 067 CmdParameters params = cles.getDefaultCmdParameters(); 068 String size; 069 if (parameters != null && parameters.containsKey(THUMBNAIL_SIZE_PARAMETER_NAME)) { 070 size = (String) parameters.get(THUMBNAIL_SIZE_PARAMETER_NAME); 071 } else { 072 size = THUMBNAIL_DEFAULT_SIZE; 073 } 074 params.addNamedParameter(THUMBNAIL_SIZE_PARAMETER_NAME, size); 075 params.addNamedParameter("inputFilePath", source.getFile()); 076 params.addNamedParameter("outputFilePath", targetBlob.getFile()); 077 078 ExecResult res = cles.execCommand(THUMBNAIL_COMMAND, params); 079 if (!res.isSuccessful()) { 080 throw res.getError(); 081 } 082 } 083 return new SimpleCachableBlobHolder(targetBlob); 084 } catch (CommandNotAvailable | IOException | NuxeoException | CommandException e) { 085 throw new ConversionException("Thumbnail conversion failed", e); 086 } 087 } 088 089 @Override 090 public void init(ConverterDescriptor descriptor) { 091 } 092}