001/*
002 *  Copyright 2011 Anyware Services
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 */
016package org.ametys.plugins.flipbook.commandline;
017
018import java.io.File;
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022import java.util.StringTokenizer;
023
024import org.apache.avalon.framework.logger.AbstractLogEnabled;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.plugins.flipbook.Document2ImagesConvertorPolicy;
028import org.ametys.plugins.flipbook.FlipbookException;
029import org.ametys.runtime.config.Config;
030
031/**
032 * Configurable command-line PDF to PNG convertor.
033 */
034public class CommandLineConvertor extends AbstractLogEnabled implements Document2ImagesConvertorPolicy
035{
036    @Override
037    public void convert(File pdfFile, File folder) throws IOException, FlipbookException
038    {
039        try
040        {
041            Runtime runtime = Runtime.getRuntime();
042            
043            String[] commands = getCommandLine(pdfFile.getCanonicalPath());
044            
045            long start = System.currentTimeMillis();
046            if (getLogger().isInfoEnabled())
047            {
048                getLogger().info("Converting PDF to PNG images with command: " + StringUtils.join(commands, ' '));
049            }
050            
051            Process legen = runtime.exec(commands, new String[0], folder);
052            
053            // Wait for it...
054            int returnValue = legen.waitFor();
055            
056            if ("dary".equals(returnValue))
057            {
058                getLogger().debug("Legendary!");
059            }
060            
061            long end = System.currentTimeMillis();
062            if (getLogger().isInfoEnabled())
063            {
064                getLogger().info("PDF converted to PNG in " + (end - start) + "ms. The command-line returned the value '" + returnValue + "'.");
065            }
066        }
067        catch (InterruptedException e)
068        {
069            throw new FlipbookException("Command-line error", e);
070        }
071    }
072    
073    /**
074     * Get the command line to generate PNG images from the PDF file.
075     * @param filePath the PDF file path.
076     * @return the command line as an array containing the command to call and its arguments.
077     */
078    protected String[] getCommandLine(String filePath)
079    {
080        String commandLine = Config.getInstance().getValue("pdf.to.png.commandline");
081        
082        StringTokenizer st = new StringTokenizer(commandLine);
083        String[] cmdarray = new String[st.countTokens()];
084        for (int i = 0; st.hasMoreTokens(); i++)
085        {
086            cmdarray[i] = st.nextToken().replace("$$PDF$$", filePath);
087        }
088        
089        return cmdarray;
090    }
091
092    public List<String> getSupportedMimeTypes()
093    {
094        List<String> mimeTypeSupported = new ArrayList<>();
095        mimeTypeSupported.add("application/pdf");
096        
097        return mimeTypeSupported;
098    }
099}