Skip to content

Commit 882406d

Browse files
committed
feat: add canBeConvertedToPDF method
1 parent 169d215 commit 882406d

6 files changed

+79
-45
lines changed

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
],
3636
"dependencies": {
3737
"@shelf/aws-lambda-brotli-unpacker": "0.0.2",
38-
"del": "4.1.1"
38+
"@shelf/is-audio-filepath": "0.0.3",
39+
"del": "4.1.1",
40+
"is-image": "3.0.0",
41+
"is-video": "1.0.1"
3942
},
4043
"devDependencies": {
4144
"@babel/cli": "7.4.4",

readme.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ Follow the instructions on how to add a lambda layer in [that repo](https://gith
1818
## Usage
1919

2020
```js
21-
const {convertTo} = require('@shelf/aws-lambda-libreoffice');
21+
const {convertTo, canBeConvertedToPDF} = require('@shelf/aws-lambda-libreoffice');
2222

2323
module.exports.handler = async () => {
2424
// assuming there is a document.docx file inside /tmp dir
2525
// original file will be deleted afterwards
2626

27+
if (!canBeConvertedToPDF('document.docx')) {
28+
return false;
29+
}
30+
2731
return convertTo('document.docx', 'pdf'); // returns /tmp/document.pdf
2832
};
2933
```

src/convert.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {unpack} from '@shelf/aws-lambda-brotli-unpacker';
2+
import {execSync} from 'child_process';
3+
import {cleanupTempFiles} from './cleanup';
4+
import {getConvertedFilePath} from './logs';
5+
6+
export const defaultArgs = [
7+
'--headless',
8+
'--invisible',
9+
'--nodefault',
10+
'--view',
11+
'--nolockcheck',
12+
'--nologo',
13+
'--norestore'
14+
];
15+
16+
const INPUT_PATH = '/opt/lo.tar.br';
17+
const OUTPUT_PATH = '/tmp/instdir/program/soffice';
18+
19+
// see https://github.com/alixaxel/chrome-aws-lambda
20+
export async function getExecutablePath(): Promise<string> {
21+
return unpack({inputPath: INPUT_PATH, outputPath: OUTPUT_PATH});
22+
}
23+
24+
/**
25+
* Converts a file in /tmp to the desired file format
26+
* @param {String} filename Name of the file to convert located in /tmp directory
27+
* @param {String} format File format to convert incoming file to
28+
* @return {Promise<String>} Absolute path to the converted file
29+
*/
30+
export async function convertTo(filename: string, format: string): Promise<string> {
31+
cleanupTempFiles();
32+
33+
const binary = await getExecutablePath();
34+
35+
const logs = execSync(
36+
`cd /tmp && ${binary} ${defaultArgs.join(' ')} --convert-to ${format} --outdir /tmp ${filename}`
37+
);
38+
39+
execSync(`rm /tmp/${filename}`);
40+
cleanupTempFiles();
41+
42+
return getConvertedFilePath(logs.toString('utf8'));
43+
}

src/index.ts

+2-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,2 @@
1-
import {unpack} from '@shelf/aws-lambda-brotli-unpacker';
2-
import {execSync} from 'child_process';
3-
import {cleanupTempFiles} from './cleanup';
4-
import {getConvertedFilePath} from './logs';
5-
6-
export const defaultArgs = [
7-
'--headless',
8-
'--invisible',
9-
'--nodefault',
10-
'--view',
11-
'--nolockcheck',
12-
'--nologo',
13-
'--norestore'
14-
];
15-
16-
const INPUT_PATH = '/opt/lo.tar.br';
17-
const OUTPUT_PATH = '/tmp/instdir/program/soffice';
18-
19-
// see https://github.com/alixaxel/chrome-aws-lambda
20-
export async function getExecutablePath(): Promise<string> {
21-
return unpack({inputPath: INPUT_PATH, outputPath: OUTPUT_PATH});
22-
}
23-
24-
/**
25-
* Converts a file in /tmp to the desired file format
26-
* @param {String} filename Name of the file to convert located in /tmp directory
27-
* @param {String} format File format to convert incoming file to
28-
* @return {Promise<String>} Absolute path to the converted file
29-
*/
30-
export async function convertTo(filename: string, format: string): Promise<string> {
31-
cleanupTempFiles();
32-
33-
const binary = await getExecutablePath();
34-
35-
const logs = execSync(
36-
`cd /tmp && ${binary} ${defaultArgs.join(' ')} --convert-to ${format} --outdir /tmp ${filename}`
37-
);
38-
39-
execSync(`rm /tmp/${filename}`);
40-
cleanupTempFiles();
41-
42-
return getConvertedFilePath(logs.toString('utf8'));
43-
}
1+
export * from './convert';
2+
export * from './validations';

src/validations.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {canBeConvertedToPDF} from './validations';
2+
3+
describe('canBeConvertedToPDF', () => {
4+
it.each`
5+
filename | expected
6+
${'image.jpg'} | ${false}
7+
${'image.mp4'} | ${false}
8+
${'image.mp3'} | ${false}
9+
${'image.wav'} | ${false}
10+
${'image.chm'} | ${false}
11+
${'image.docx'} | ${true}
12+
${'image.pdf'} | ${true}
13+
`('should return $expected for filename $filename', ({filename, expected}) => {
14+
expect(canBeConvertedToPDF(filename)).toEqual(expected);
15+
});
16+
});

src/validations.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import isAudio from '@shelf/is-audio-filepath';
2+
import isVideo from 'is-video';
3+
import isImage from 'is-image';
4+
5+
export function canBeConvertedToPDF(filename: string): boolean {
6+
return (
7+
!isImage(filename) && !isVideo(filename) && !isAudio(filename) && !filename.endsWith('.chm')
8+
);
9+
}

0 commit comments

Comments
 (0)