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