- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
Latest commit
43 lines (40 loc) · 1.69 KB
/
Copy pathhandler.js
File metadata and controls
43 lines (40 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
constSharp=require('sharp');
constPath=require('path');
constImageFetcher=require('./src/image-fetcher');
constImageProcessor=require('./src/image-processor');
module.exports.processImage=(event,context,callback)=>{
constbucket=decodeURIComponent(event.pathParameters.bucket);
constimageFetcher=newImageFetcher(bucket);
constimageProcessor=newImageProcessor(Sharp);
constfile=decodeURIComponent(event.pathParameters.file);
constquality=event.queryStringParameters&&+event.queryStringParameters.quality||100;
constsize={
width: event.queryStringParameters&&+event.queryStringParameters.width||null,
height: event.queryStringParameters&&+event.queryStringParameters.height||null,
};
constblur=event.queryStringParameters&&+event.queryStringParameters.blur||false;
constwebp=event.queryStringParameters&&event.queryStringParameters.webp||false;
returnimageFetcher
.fetchImage(file)
.then(data=>imageProcessor.resize(data.image,size,quality))
.then(data=>blur ? imageProcessor.blur(data.image,blur) : data)
.then(data=>webp ? imageProcessor.webp(data.image) : data)
.then(data=>{
constcontentType=data.contentType;
constimg=newBuffer(data.image.buffer,'base64');
constfileName=Path.parse(file).base;
callback(null,{
statusCode: 200,
headers: {
'Content-Type': contentType,
'Content-Disposition': 'inline; filename="'+fileName+'"'
},
body: img.toString('base64'),
isBase64Encoded: true,
});
})
.catch(error=>{
console.error('Error:',error);
callback(null,error);
});
};