Skip to content

Commit

Permalink
Merge pull request #135 from tadasauciunas/master
Browse files Browse the repository at this point in the history
Possibility to use LibreOffice extensions
  • Loading branch information
randomhash authored Mar 7, 2020
2 parents 381cec8 + 8f86fe5 commit ab4dde6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
40 changes: 39 additions & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {execSync} from 'child_process';
import {basename} from 'path';
import {unpack} from './unpack';
import {cleanupTempFiles} from './cleanup';
import {getConvertedFilePath} from './logs';
Expand All @@ -16,17 +17,38 @@ export const defaultArgs = [

const INPUT_PATH = '/opt/lo.tar.br';
const OUTPUT_PATH = '/tmp/instdir/program/soffice.bin';
const UNOPKG_OUTPUT_PATH = '/tmp/instdir/program/unopkg.bin';

type ExtensionOptions = {
extensions: string[];
shouldThrowOnExtensionFail?: boolean;
};

/**
* Converts a file in /tmp to the desired file format
* @param {String} filename Name of the file to convert located in /tmp directory
* @param {String} format File format to convert incoming file to
* @param {ExtensionOptions} options LibreOffice extensions to be enabled during file conversion
* @return {Promise<String>} Absolute path to the converted file
*/
export async function convertTo(filename: string, format: string): Promise<string> {
export async function convertTo(
filename: string,
format: string,
options?: ExtensionOptions
): Promise<string> {
let logs;
cleanupTempFiles();
await unpack({inputPath: INPUT_PATH});

if (options && options.extensions && options.extensions.length) {
const {extensions, shouldThrowOnExtensionFail = true} = options;
const enabledExtensions = execSync(`${UNOPKG_OUTPUT_PATH} list --shared`).toString();

extensions.forEach(extension => {
enableExtension(enabledExtensions, extension, shouldThrowOnExtensionFail);
});
}

const cmd = `cd /tmp && ${OUTPUT_PATH} ${defaultArgs.join(
' '
)} --convert-to ${format} --outdir /tmp /tmp/${filename}`;
Expand All @@ -42,3 +64,19 @@ export async function convertTo(filename: string, format: string): Promise<strin

return getConvertedFilePath(logs.toString('utf8'));
}

function enableExtension(
enabledExtensions: string,
extension: string,
shouldThrowOnExtensionFail: boolean
): void {
if (!enabledExtensions.includes(basename(extension))) {
try {
execSync(`${UNOPKG_OUTPUT_PATH} add --shared ${extension}`);
} catch (e) {
if (shouldThrowOnExtensionFail) {
throw e;
}
}
}
}
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ const {convertTo} = require('./lib');
module.exports.handler = async () => {
writeFileSync('/tmp/test.txt', Buffer.from('Hello World!'));

return convertTo('test.txt', `"pdf:writer_pdf_Export"`);
const extensionOptions = {extensions: ['test_extension.oxt']};

return convertTo('test.txt', `"pdf:writer_pdf_Export"`, extensionOptions);
};
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cd ../libreoffice-lambda-layer

rm -rf layer
unzip layers.zip -d layer
unzip layer.tar.br.zip -d layer

cd layer
brotli -d lo.tar.br
Expand All @@ -15,4 +15,4 @@ yarn build
docker run --rm \
-v "$PWD":/var/task \
-v "$PWD"/../libreoffice-lambda-layer/layer:/opt \
lambci/lambda:nodejs12.x test.handler
lambci/lambda:nodejs12.x test.handler
Binary file added test_extension.oxt
Binary file not shown.

0 comments on commit ab4dde6

Please sign in to comment.