001/* 002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Laurent Doguin <[email protected]> 018 * Vladimir Pasquier <[email protected]> 019 * 020 */ 021package org.nuxeo.ecm.platform.thumbnail.converter; 022 023import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.THUMBNAIL_DEFAULT_SIZE; 024import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME; 025 026import java.io.IOException; 027import java.io.Serializable; 028import java.util.Map; 029 030import org.nuxeo.ecm.core.api.Blob; 031import org.nuxeo.ecm.core.api.Blobs; 032import org.nuxeo.ecm.core.api.CloseableFile; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 035import org.nuxeo.ecm.core.convert.api.ConversionException; 036import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 037import org.nuxeo.ecm.core.convert.extension.Converter; 038import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 039import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters; 040import org.nuxeo.ecm.platform.commandline.executor.api.CommandAvailability; 041import org.nuxeo.ecm.platform.commandline.executor.api.CommandException; 042import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService; 043import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable; 044import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult; 045import org.nuxeo.runtime.api.Framework; 046 047/** 048 * Converter bean managing the thumbnail conversion for picture documents 049 * 050 * @since 5.7 051 */ 052public class ThumbnailDocumentConverter implements Converter { 053 054 public static final String THUMBNAIL_COMMAND = "toThumbnail"; 055 056 @Override 057 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) { 058 try { 059 // Make sure the toThumbnail command is available 060 CommandLineExecutorService cles = Framework.getService(CommandLineExecutorService.class); 061 CommandAvailability commandAvailability = cles.getCommandAvailability(THUMBNAIL_COMMAND); 062 if (!commandAvailability.isAvailable()) { 063 return null; 064 } 065 // get the input and output of the command 066 Blob blob = blobHolder.getBlob(); 067 if (blob == null) { 068 return new SimpleCachableBlobHolder((Blob) null); 069 } 070 071 Blob targetBlob = Blobs.createBlobWithExtension(".png"); 072 targetBlob.setMimeType("image/png"); 073 try (CloseableFile source = blob.getCloseableFile()) { 074 CmdParameters params = cles.getDefaultCmdParameters(); 075 String size = parameters == null ? THUMBNAIL_DEFAULT_SIZE 076 : (String) parameters.getOrDefault(THUMBNAIL_SIZE_PARAMETER_NAME, THUMBNAIL_DEFAULT_SIZE); 077 params.addNamedParameter(THUMBNAIL_SIZE_PARAMETER_NAME, size); 078 params.addNamedParameter("inputFilePath", source.getFile()); 079 params.addNamedParameter("outputFilePath", targetBlob.getFile()); 080 081 ExecResult res = cles.execCommand(THUMBNAIL_COMMAND, params); 082 if (!res.isSuccessful()) { 083 throw res.getError(); 084 } 085 } 086 return new SimpleCachableBlobHolder(targetBlob); 087 } catch (CommandNotAvailable | IOException | NuxeoException | CommandException e) { 088 throw new ConversionException("Thumbnail conversion failed", e); 089 } 090 } 091 092 @Override 093 public void init(ConverterDescriptor descriptor) { 094 // Nothing to do here 095 } 096}