File tree 6 files changed +79
-45
lines changed
6 files changed +79
-45
lines changed Original file line number Diff line number Diff line change 35
35
],
36
36
"dependencies" : {
37
37
"@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"
39
42
},
40
43
"devDependencies" : {
41
44
"@babel/cli" : " 7.4.4" ,
Original file line number Diff line number Diff line change @@ -18,12 +18,16 @@ Follow the instructions on how to add a lambda layer in [that repo](https://gith
18
18
## Usage
19
19
20
20
``` js
21
- const {convertTo } = require (' @shelf/aws-lambda-libreoffice' );
21
+ const {convertTo , canBeConvertedToPDF } = require (' @shelf/aws-lambda-libreoffice' );
22
22
23
23
module .exports .handler = async () => {
24
24
// assuming there is a document.docx file inside /tmp dir
25
25
// original file will be deleted afterwards
26
26
27
+ if (! canBeConvertedToPDF (' document.docx' )) {
28
+ return false ;
29
+ }
30
+
27
31
return convertTo (' document.docx' , ' pdf' ); // returns /tmp/document.pdf
28
32
};
29
33
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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' ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments