001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.ecm.platform.picture.magick.utils;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022
023import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
024import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
025import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
026import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
027import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
028import org.nuxeo.ecm.platform.picture.api.ImageInfo;
029import org.nuxeo.ecm.platform.picture.magick.MagickExecutor;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Unit command to extract information from a picture file.
034 *
035 * @author tiry
036 */
037public class ImageIdentifier extends MagickExecutor {
038
039    private static final Log log = LogFactory.getLog(ImageIdentifier.class);
040
041    public static ImageInfo getInfo(String inputFilePath) throws CommandNotAvailable, CommandException {
042
043        ExecResult result = getIdentifyResult(inputFilePath);
044        if (!result.isSuccessful()) {
045            log.debug("identify failed for file: " + inputFilePath);
046            throw result.getError();
047        }
048        String out = result.getOutput().get(result.getOutput().size() > 1 ? result.getOutput().size() - 1 : 0);
049        String[] res = out.split(" ");
050
051        return new ImageInfo(res[1], res[2], res[0], res[3], res[4], inputFilePath);
052    }
053
054    public static ExecResult getIdentifyResult(String inputFilePath) throws CommandNotAvailable {
055        CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
056        CmdParameters params = cles.getDefaultCmdParameters();
057        params.addNamedParameter("inputFilePath", inputFilePath);
058        return cles.execCommand("identify", params);
059    }
060
061}