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