001/*
002 * (C) Copyright 2018 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.convert.plugins;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.Serializable;
025import java.net.URI;
026import java.net.URISyntaxException;
027import java.nio.file.Files;
028import java.nio.file.Path;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.io.FileUtils;
033import org.apache.logging.log4j.LogManager;
034import org.apache.logging.log4j.Logger;
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.ecm.core.convert.api.ConversionException;
037import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * LibreOffice converter to be used when using the {@code soffice} command line.
042 * <p>
043 * It fills the {@code -env:userInstallation} argument with a temporary folder to correctly isolate the {@code soffice}
044 * processes so multiple instances can be run simultaneously.
045 *
046 * @since 10.10
047 */
048public class LibreOfficeConverter extends CommandLineConverter {
049
050    public static final String USER_INSTALLATION_PATH_KEY = "userInstallation";
051
052    private static final Logger log = LogManager.getLogger(LibreOfficeConverter.class);
053
054    @Override
055    protected Map<String, String> getCmdStringParameters(BlobHolder blobHolder, Map<String, Serializable> parameters) {
056        Map<String, String> cmdStringParameters = super.getCmdStringParameters(blobHolder, parameters);
057
058        // create a temporary folder for the user installation env
059        try {
060            Path tempDirectoryPath = Framework.createTempDirectory(null);
061            // the -env:userInstallation expects an URI (file:///tmp/foo)
062            cmdStringParameters.put(USER_INSTALLATION_PATH_KEY, tempDirectoryPath.toUri().toString());
063        } catch (IOException e) {
064            throw new ConversionException(blobHolder, e);
065        }
066
067        return cmdStringParameters;
068    }
069
070    @Override
071    protected BlobHolder buildResult(List<String> cmdOutput, CmdParameters cmdParams) {
072        try {
073            return super.buildResult(cmdOutput, cmdParams);
074        } finally {
075            // delete the temp folder
076            String userInstallationPath = cmdParams.getParameter(USER_INSTALLATION_PATH_KEY);
077            if (userInstallationPath != null) {
078                deleteTempDirectory(userInstallationPath);
079            }
080        }
081    }
082
083    private void deleteTempDirectory(String tempFileURI) {
084        try {
085            // tempFileURI is an URI (file:///tmp/foo)
086            URI uri = new URI(tempFileURI);
087            File file = new File(uri);
088            if (!Files.exists(file.toPath())) {
089                return;
090            }
091            FileUtils.deleteDirectory(file);
092        } catch (IOException | URISyntaxException e) {
093            log.error(e);
094            log.debug(e, e);
095        }
096    }
097}