forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
276 lines (239 loc) · 7.48 KB
/
index.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var sharp = require('sharp'),
AWSXRay = require('aws-xray-sdk-core'),
fs = require('fs'),
path = require('path'),
isAnimated = require('animated-gif-detector'),
smartcrop = require('smartcrop-sharp'),
imageminPngquant = require('imagemin-pngquant'),
querystring = require('querystring');
const { config } = require('process');
const localConfigFilename = './lambda-config.json';
const isLocalConfig = process.env.S3_REGION ? false : true;
const localConfig =
isLocalConfig && fs.existsSync( localConfigFilename ) ?
JSON.parse( fs.readFileSync( localConfigFilename ) ) : {};
const cfg = isLocalConfig ? localConfig : process.env;
const enableTracing = cfg.AWS_XRAY_DAEMON_ADDRESS;
const authenticatedRequest = !!cfg.S3_AUTHENTICATED_REQUEST ?
cfg.S3_AUTHENTICATED_REQUEST.toLowerCase() == 'true' : false;
let AWS;
if ( enableTracing ) {
AWS = AWSXRay.captureAWS(require('aws-sdk'));
} else {
AWS = require('aws-sdk');
}
var regions = {};
module.exports = {};
module.exports.s3 = function(config, key, args, callback) {
AWS.config.region = config.region;
var s3config = {};
if (config.endpoint) {
s3config.endpoint = config.endpoint;
}
if (!regions[config.region]) {
regions[config.region] = new AWS.S3(
Object.assign({ region: config.region }, s3config)
);
}
var s3 = regions[config.region];
var isPresigned = !! args['X-Amz-Algorithm'];
if ( authenticatedRequest ) {
request = s3.makeRequest( 'getObject', { Bucket: config.bucket, Key: key } );
} else {
request = s3.makeUnauthenticatedRequest( 'getObject', { Bucket: config.bucket, Key: key } );
// To support forwarding presigned URLs, we hook into the post `build` step to add/forward
// the Amz signing URL query params from the current request.
if ( isPresigned ) {
// All the URL params that should be forwarded from the current request to the S3 file request.
const presignedParams = [
'X-Amz-Algorithm',
'X-Amz-Content-Sha256',
'X-Amz-Credential',
'X-Amz-SignedHeaders',
'X-Amz-Expires',
'X-Amz-Signature',
'X-Amz-Date',
'X-Amz-Security-Token',
];
// Append the presigned URL params to the S3 file request URL.
request.addListener( 'build', function ( req ) {
const urlParams = presignedParams.reduce( ( params, urlParam ) => {
if ( args[ urlParam ] ) {
params[ urlParam ] = args[ urlParam ];
}
return params;
}, {} );
req.httpRequest.path += `?${ querystring.stringify( urlParams ) }`;
});
}
}
request.send( function ( err, data ) {
if ( err ) {
return callback( err );
}
args.key = key;
return module.exports.resizeBuffer( data.Body, args, callback );
} );
return request;
};
const getDimArray = function( dims, zoom ) {
var dimArr = typeof dims === 'string' ? dims.split(',') : dims;
zoom = zoom || 1;
return dimArr.map(function(v) {
return Math.round((Number(v) * zoom)) || null;
});
}
const clamp = function( val, min, max ) {
return Math.min( Math.max( Number( val ), min ), max );
}
// return a default compression value based on a logarithmic scale
// defaultValue = 100, zoom = 2; = 65
// defaultValue = 80, zoom = 2; = 50
// defaultValue = 100, zoom = 1.5; = 86
// defaultValue = 80, zoom = 1.5; = 68
const applyZoomCompression = function ( defaultValue, zoom ) {
const value = Math.round( defaultValue - ( ( Math.log( zoom ) / Math.log( defaultValue / zoom ) ) * ( defaultValue * zoom ) ) );
const min = Math.round( defaultValue / zoom );
return clamp( value, min, defaultValue );
}
module.exports.resizeBuffer = async function(buffer, args, callback) {
try {
const image = sharp(buffer, {failOnError: false}).withMetadata();
// check we can get valid metadata
const metadata = await image.metadata();
// auto rotate based on orientation exif data
image.rotate();
// convert gifs to pngs unless animated
if (
args.key &&
path.extname(args.key).toLowerCase() === '.gif'
) {
if (isAnimated(buffer)) {
return callback(new Error('fallback-to-original'));
} else {
image.png();
}
}
// crop (assumes crop data from original)
if (args.crop) {
var cropValues =
typeof args.crop === 'string'
? args.crop.split(',')
: args.crop;
// convert percentages to px values
cropValues = cropValues.map(function(value, index) {
if (value.indexOf('px') > -1) {
return Number(value.substr(0, value.length - 2));
} else {
return Number(
Number(
metadata[index % 2 ? 'height' : 'width'] *
(value / 100)
).toFixed(0)
);
}
});
image.extract({
left: cropValues[0],
top: cropValues[1],
width: cropValues[2],
height: cropValues[3],
});
}
// get zoom value
const zoom = parseFloat( args.zoom ) || 1;
// resize
if (args.resize) {
// apply smart crop if available
if (args.crop_strategy === 'smart' && ! args.crop) {
const cropResize = getDimArray( args.resize );
const rotatedImage = await image.toBuffer();
const result = await smartcrop.crop(rotatedImage, { width: cropResize[0], height: cropResize[1] });
if (result && result.topCrop) {
image.extract({
left: result.topCrop.x,
top: result.topCrop.y,
width: result.topCrop.width,
height: result.topCrop.height,
});
}
}
// apply the resize
args.resize = getDimArray( args.resize, zoom );
image.resize({
width: args.resize[0],
height: args.resize[1],
withoutEnlargement: true,
position: ( args.crop_strategy !== 'smart' && args.crop_strategy ) || args.gravity || 'centre',
});
} else if (args.fit) {
args.fit = getDimArray( args.fit, zoom );
image.resize({
width: args.fit[0],
height: args.fit[1],
fit: 'inside',
withoutEnlargement: true,
});
} else if (args.lb) {
args.lb = getDimArray( args.lb, zoom );
image.resize({
width: args.lb[0],
height: args.lb[1],
fit: 'contain',
// default to a black background to replicate Photon API behaviour
// when no background colour specified
background: args.background || 'black',
withoutEnlargement: true,
});
} else if (args.w || args.h) {
image.resize({
width: (Number(args.w) * zoom) || null,
height: (Number( args.h ) * zoom) || null,
fit: args.crop ? 'cover' : 'inside',
withoutEnlargement: true,
});
}
// set default quality slightly higher than sharp's default
if ( ! args.quality ) {
args.quality = applyZoomCompression( 82, zoom );
}
// allow override of compression quality
if (args.webp) {
image.webp({
quality: Math.round( clamp( args.quality, 0, 100 ) ),
});
} else if (metadata.format === 'jpeg') {
image.jpeg({
quality: Math.round( clamp( args.quality, 0, 100 ) ),
});
}
// send image
return new Promise((resolve, reject) => {
image.toBuffer(async (err, data, info) => {
if (err) {
reject(err);
}
// Pass PNG images through PNGQuant as Sharp is not good at compressing them.
// See https://github.com/lovell/sharp/issues/478
if ( info.format === 'png' ) {
if ( enableTracing ) {
var mainSegment = AWSXRay.getSegment();
var segment = mainSegment.addNewSubsegment( 'imagemin-pngquant' );
}
data = await imageminPngquant()( data );
if ( enableTracing ) {
segment.close();
}
// Make sure we update the size in the info, to reflect the new
// size after lossless-compression.
info.size = data.length;
}
callback && callback(null, data, info);
resolve({ data, info });
});
});
} catch (err) {
callback && callback(err);
return err;
}
};