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 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.platform.picture.magick.utils;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
026import org.nuxeo.ecm.platform.commandline.executor.api.CommandException;
027import org.nuxeo.ecm.platform.commandline.executor.api.CommandLineExecutorService;
028import org.nuxeo.ecm.platform.commandline.executor.api.CommandNotAvailable;
029import org.nuxeo.ecm.platform.commandline.executor.api.ExecResult;
030import org.nuxeo.ecm.platform.picture.api.ImageInfo;
031import org.nuxeo.ecm.platform.picture.magick.MagickExecutor;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * Unit command to extract information from a picture file.
036 *
037 * @author tiry
038 */
039public class ImageIdentifier extends MagickExecutor {
040
041    private static final Log log = LogFactory.getLog(ImageIdentifier.class);
042
043    public static ImageInfo getInfo(String inputFilePath) throws CommandNotAvailable, CommandException {
044
045        ExecResult result = getIdentifyResult(inputFilePath);
046        if (!result.isSuccessful()) {
047            log.debug("identify failed for file: " + inputFilePath);
048            throw result.getError();
049        }
050        String out = result.getOutput().get(result.getOutput().size() > 1 ? result.getOutput().size() - 1 : 0);
051        String[] res = out.split(" ");
052
053        return new ImageInfo(res[1], res[2], res[0], res[3], res[4], inputFilePath);
054    }
055
056    public static ExecResult getIdentifyResult(String inputFilePath) throws CommandNotAvailable {
057        CommandLineExecutorService cles = Framework.getLocalService(CommandLineExecutorService.class);
058        CmdParameters params = cles.getDefaultCmdParameters();
059        params.addNamedParameter("inputFilePath", inputFilePath);
060        return cles.execCommand("identify", params);
061    }
062
063}