001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Miguel Nixo
018 */
019
020package org.nuxeo.ecm.platform.convert.plugins;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.apache.commons.io.FilenameUtils;
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
039public class Image2PDFConverter extends CommandLineBasedConverter {
040
041    @Override
042    protected BlobHolder buildResult(List<String> cmdOutput, CmdParameters cmdParams) {
043
044        String outputPath = cmdParams.getParameter("outDirPath");
045        File outputDir = new File(outputPath);
046        File[] files = outputDir.listFiles();
047        List<Blob> blobs = new ArrayList<>();
048
049        for (File file : files) {
050            try {
051                Blob blob = Blobs.createBlob(file);
052                blob.setFilename(file.getName());
053                blobs.add(blob);
054            } catch (IOException e) {
055                throw new ConversionException("Cannot create Blob", e);
056            }
057        }
058        return new SimpleCachableBlobHolder(blobs);
059    }
060
061    @Override
062    protected Map<String, Blob> getCmdBlobParameters(BlobHolder blobHolder, Map<String, Serializable> parameters)
063            throws ConversionException {
064
065        Map<String, Blob> cmdBlobParams = new HashMap<>();
066        cmdBlobParams.put("inputFilePath", blobHolder.getBlob());
067        return cmdBlobParams;
068    }
069
070    @Override
071    protected Map<String, String> getCmdStringParameters(BlobHolder blobHolder, Map<String, Serializable> parameters)
072            throws ConversionException {
073
074        Map<String, String> cmdStringParams = new HashMap<>();
075
076        String baseDir = getTmpDirectory(parameters);
077        Path tmpPath = new Path(baseDir).append("image2pdf_" + System.currentTimeMillis());
078
079        File outDir = new File(tmpPath.toString());
080        boolean dirCreated = outDir.mkdir();
081        if (!dirCreated) {
082            throw new ConversionException("Unable to create tmp dir for transformer output", blobHolder);
083        }
084
085        cmdStringParams.put("outDirPath", outDir.getAbsolutePath());
086        cmdStringParams.put("outputFilePath", outDir.getAbsolutePath() + System.getProperty("file.separator")
087                + FilenameUtils.getBaseName(blobHolder.getBlob().getFilename()) + ".pdf");
088        return cmdStringParams;
089    }
090
091}