001/*
002 * (C) Copyright 2002-2007 Nuxeo SAS (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.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 */
018
019package org.nuxeo.ecm.platform.convert.plugins;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.nuxeo.common.utils.Path;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.Blobs;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.ecm.core.convert.api.ConversionException;
034import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
035import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
036
037/**
038 * Pdf2Image converter based on imageMagick's convert command-line executable.
039 *
040 * @author ldoguin
041 */
042public class PDF2ImageConverter extends CommandLineBasedConverter {
043
044    @Override
045    protected BlobHolder buildResult(List<String> cmdOutput, CmdParameters cmdParams) {
046
047        String outputPath = cmdParams.getParameter("outDirPath");
048        File outputDir = new File(outputPath);
049        File[] files = outputDir.listFiles();
050        List<Blob> blobs = new ArrayList<Blob>();
051
052        for (File file : files) {
053            try {
054                Blob blob = Blobs.createBlob(file);
055                blob.setFilename(file.getName());
056                blobs.add(blob);
057            } catch (IOException e) {
058                throw new ConversionException("Cannot create Blob", e);
059            }
060        }
061        return new SimpleCachableBlobHolder(blobs);
062    }
063
064    @Override
065    protected Map<String, Blob> getCmdBlobParameters(BlobHolder blobHolder, Map<String, Serializable> parameters)
066            throws ConversionException {
067
068        Map<String, Blob> cmdBlobParams = new HashMap<String, Blob>();
069        cmdBlobParams.put("sourceFilePath", blobHolder.getBlob());
070        return cmdBlobParams;
071    }
072
073    @Override
074    protected Map<String, String> getCmdStringParameters(BlobHolder blobHolder, Map<String, Serializable> parameters)
075            throws ConversionException {
076
077        Map<String, String> cmdStringParams = new HashMap<String, String>();
078
079        String baseDir = getTmpDirectory(parameters);
080        Path tmpPath = new Path(baseDir).append("pdf2image_" + System.currentTimeMillis());
081
082        File outDir = new File(tmpPath.toString());
083        boolean dirCreated = outDir.mkdir();
084        if (!dirCreated) {
085            throw new ConversionException("Unable to create tmp dir for transformer output");
086        }
087
088        cmdStringParams.put("outDirPath", outDir.getAbsolutePath());
089        cmdStringParams.put("targetFilePath", outDir.getAbsolutePath() + System.getProperty("file.separator")
090                + parameters.get("targetFilePath").toString());
091        return cmdStringParams;
092    }
093
094}