@@ -11,37 +11,51 @@ import { finished } from 'stream/promises';
11
11
import Jimp from "jimp" ;
12
12
13
13
export const handler = async ( event ) => {
14
- const input = JSON . parse ( event . body ) || event ;
15
- const url = input . url ;
16
- const fileType = input . fileType ;
17
- const id = input . id ;
14
+ let input = { } ;
15
+ let url = '' ;
16
+ let fileType = '' ;
17
+ let id = '' ;
18
+ let out = { "statusCode" : 500 , "body" : { "error" : { "code" : 500 , message : "Failed to start process" } } } ;
19
+ try {
20
+ input = 'body' in event ? JSON . parse ( event . body ) : event ;
21
+ url = input . url ;
22
+ fileType = input . fileType ;
23
+ id = input . id ;
24
+ }
25
+ catch {
26
+ out = formatError ( 'Unable to initialize post processing, perhaps you did not send a valid json body.' )
27
+ }
18
28
try {
19
29
if ( [ 'bmp' , 'png' , 'tiff' , 'webp' , 'tif' , 'psd' , 'svg' , 'jpeg' , 'jpg' , 'gif' , 'heic' , 'heif' , 'avci' , 'avif' , 'icns' , 'ico' , 'j2c' , 'jp2' , 'ktx' , 'pnm' , 'pam' , 'pbm' , 'pfm' , 'pgm' , 'ppm' , 'tga' , 'cur' , 'dds' ] . includes ( fileType ) ) {
20
- return formatResponse ( await getImageInfo ( url ) ) ;
30
+ out = formatResponse ( await getImageInfo ( url , id ) ) ;
21
31
}
22
32
else if ( [ 'pdf' ] . includes ( fileType ) ) {
23
- return formatResponse ( await getPdfInfo ( url ) ) ;
33
+ out = formatResponse ( await getPdfInfo ( url ) ) ;
24
34
}
25
35
else if ( [ 'zip' ] . includes ( fileType ) ) {
26
- return formatResponse ( await getZipInfo ( url ) ) ;
36
+ out = formatResponse ( await getZipInfo ( url ) ) ;
37
+ }
38
+ else {
39
+ out = formatError ( `File type ${ fileType } cannot be processed by this service.` ) ;
27
40
}
28
- return formatError ( `File type ${ fileType } cannot be processed by this service.` ) ;
29
41
}
30
- catch {
31
- return formatError ( `Unable to process url ${ url } of type ${ fileType } for id ${ id } . ` ) ;
42
+ catch ( e ) {
43
+ out = formatError ( `Unable to process url ${ url } of type ${ fileType } for id ${ id } , because ${ JSON . stringify ( e ) } ` ) ;
32
44
}
45
+ return out ;
33
46
} ;
34
47
48
+
35
49
function formatError ( message , code = 500 ) {
36
- return context . succeed ( {
50
+ return {
37
51
statusCode : code ,
38
52
body : {
39
53
error : {
40
54
code,
41
55
message
42
56
}
43
57
}
44
- } )
58
+ }
45
59
}
46
60
47
61
function formatResponse ( body ) {
@@ -53,6 +67,8 @@ function formatResponse(body) {
53
67
54
68
async function getZipInfo ( url ) {
55
69
const filePath = await downloadFile ( url ) ;
70
+ if ( ! fs . existsSync ( filePath ) )
71
+ return formatError ( `Could not download file from ${ url } ` ) ;
56
72
const zip = new AdmZip ( filePath ) ;
57
73
const zipEntries = zip . getEntries ( ) ;
58
74
const out = { files : [ ] , sizeInBytes : getFileSize ( filePath ) } ;
@@ -68,13 +84,15 @@ async function getZipInfo(url) {
68
84
return out ;
69
85
}
70
86
71
- async function getImageInfo ( url ) {
87
+ async function getImageInfo ( url , id ) {
72
88
const filePath = await downloadFile ( url ) ;
89
+ if ( ! fs . existsSync ( filePath ) )
90
+ return formatError ( `Could not download file from ${ url } ` ) ;
73
91
const out = sizeOf ( filePath ) ;
74
92
if ( [ 'png' , 'jpg' , 'jpeg' , 'gif' , 'bmp' , 'tif' , 'tiff' ] . includes ( out . type ) ) {
75
- Jimp . read ( filePath ) . then ( image => {
76
- image . resize ( out . width > out . height ? 300 : Jimp . AUTO , out . width < out . height ? 300 : Jimp . AUTO ) . write ( '/tmp/thumbnail.png' ) ;
77
- } ) ;
93
+ const image = await Jimp . read ( filePath ) ;
94
+ await image . resize ( out . width > out . height ? 300 : Jimp . AUTO , out . width < out . height ? 300 : Jimp . AUTO ) . write ( '/tmp/thumbnail.png' ) ;
95
+ await uploadThumbnail ( '/tmp/thumbnail.png' , ` ${ id } .png` , process . env . AWS_THUMBNAILS_BUCKET ) ;
78
96
out . thumbnail = true ;
79
97
}
80
98
delete out . type ;
0 commit comments