forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-handler.js
More file actions
46 lines (39 loc) · 961 Bytes
/
Copy pathlambda-handler.js
File metadata and controls
46 lines (39 loc) · 961 Bytes
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
44
45
46
var tachyon = require('./index'),
proxyFile = require('./proxy-file');
exports.handler = function( event, context, callback ) {
var region = process.env.S3_REGION;
var bucket = process.env.S3_BUCKET;
var key = decodeURI( event.pathParameters.proxy );
var config = {
region: region,
bucket: bucket
};
var args = event.queryStringParameters || {};
if ( typeof args.webp === 'undefined' && event.headers ) {
args.webp = !!event.headers['X-WebP'];
}
return tachyon.s3( config, key, args, function(
err,
data,
info
) {
if ( err ) {
if ( err.message === 'return-original-file' ) {
return proxyFile( region, bucket, key, callback );
}
return context.fail( err );
}
var resp = {
statusCode: 200,
headers: {
'Content-Type': 'image/' + info.format,
},
body: new Buffer(data).toString('base64'),
isBase64Encoded: true
};
callback( null, resp );
data = null;
info = null;
err = null;
});
};