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 <ldoguin@nuxeo.com>
018 * Vladimir Pasquier <vpasquier@nuxeo.com>
019 *
020 */
021package org.nuxeo.ecm.platform.thumbnail.converter;
022
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.Blobs;
029import org.nuxeo.ecm.core.api.CloseableFile;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.convert.api.ConversionException;
033import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
034import org.nuxeo.ecm.core.convert.extension.Converter;
035import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
036import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
037import org.nuxeo.ecm.platform.commandline.executor.api.CommandAvailability;
038import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
039import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
040import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
041import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
042import org.nuxeo.runtime.api.Framework;
043
044/**
045 * Converter bean managing the thumbnail conversion for picture documents
046 *
047 * @since 5.7
048 */
049public class ThumbnailDocumentConverter implements Converter {
050
051    public static final String THUMBNAIL_CONVERTER_NAME = "toThumbnail";
052
053    public static final String THUMBNAIL_SIZE_PARAMETER_NAME = "size";
054
055    public static final String THUMBNAIL_DEFAULT_SIZE = "100x100";
056
057    public static final String THUMBNAIL_COMMAND = "toThumbnail";
058
059    @Override
060    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
061        try {
062            // Make sure the toThumbnail command is available
063            CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
064            CommandAvailability commandAvailability = cles.getCommandAvailability(THUMBNAIL_COMMAND);
065            if (!commandAvailability.isAvailable()) {
066                return null;
067            }
068            // get the input and output of the command
069            Blob blob = blobHolder.getBlob();
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;
076                if (parameters != null && parameters.containsKey(THUMBNAIL_SIZE_PARAMETER_NAME)) {
077                    size = (String) parameters.get(THUMBNAIL_SIZE_PARAMETER_NAME);
078                } else {
079                    size = THUMBNAIL_DEFAULT_SIZE;
080                }
081                params.addNamedParameter(THUMBNAIL_SIZE_PARAMETER_NAME, size);
082                params.addNamedParameter("inputFilePath", source.getFile());
083                params.addNamedParameter("outputFilePath", targetBlob.getFile());
084
085                ExecResult res = cles.execCommand(THUMBNAIL_COMMAND, params);
086                if (!res.isSuccessful()) {
087                    throw res.getError();
088                }
089            }
090            return new SimpleCachableBlobHolder(targetBlob);
091        } catch (CommandNotAvailable | IOException | NuxeoException | CommandException e) {
092            throw new ConversionException("Thumbnail conversion failed", e);
093        }
094    }
095
096    @Override
097    public void init(ConverterDescriptor descriptor) {
098    }
099}