forked from spiritedmedia/tachyon-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tachyon.js
executable file
·438 lines (400 loc) · 14.5 KB
/
tachyon.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
const AWS = require('aws-sdk'),
fs = require('fs'),
isAnimated = require('animated-gif-detector'),
process = require('process');
// We need to setup AWS configuration before instantiating an S3 object
let configPath = '';
let configPaths = [
'./local-config.json', // Settings for local version
'./config.json' // Settings for AWS Lambda
];
for (var i = 0; i < configPaths.length; i++) {
var path = configPaths[i];
if (fs.existsSync(path)) {
configPath = path;
break;
}
}
let config = {};
if (configPath) {
config = JSON.parse(fs.readFileSync(configPath));
AWS.config.loadFromPath(configPath);
}
const S3 = new AWS.S3({
signatureVersion: 'v4',
}),
Sharp = require('sharp'),
Helpers = require('./helpers');
/**
* For use in referncing functions from this file
*
* @type {object}
*/
var self = module.exports = {};
/**
* Parse a URL and setup different options that can be passed along our Promise chain
*
* @param {string} url The URL to parse
* @return {Promise|object} Returns a promise that resolves to an object with various data about the image
*/
module.exports.setup = function (url) {
return new Promise(function (resolve, reject) {
// Remove preceeding slash
url = url.replace(/^\/+/g, '');
const originalURL = url;
// Make sure one of the following query strings is provided, otherwise there is nothing to process
const allowedParams = [
'w', // Reisze to a certain width
'h', // Resize to a certain height
'resize', // Center cropped to the exact size
'fit', // Alias of resize
'quality', // Adjust JPG quality
'crop', // Extract a region of the image
'webp', // Output in webp format
'rotate', // Rotate image 0, 90, 180, 270 degrees
'flip', // Flip the image vertically
'flop', // Flip the image horizontally
'negative', // Convert image to opposite colors
'grayscale', // Convert image to black and white
'greyscale', // Alias of grayscale
'lb', // Letterbox an image at a certain size
'background' // Background color of letterbox
];
let parts = originalURL.split('?');
let path = parts[0];
let querystring = parts[1];
let params = Helpers.queryStringToJSON(querystring);
params = Helpers.filterQueryParams(params, allowedParams);
let newQuerystring = Helpers.JSONToQueryString(params);
newQuerystring = Helpers.sanitizeQueryString(newQuerystring);
// Transform the path for how we store resized versions on S3
let newPath = 'resized/' + path;
let newURL = newPath + '_' + newQuerystring;
let returnObj = {
originalURL: originalURL,
path: path,
querystring: params,
S3Path: newPath,
S3Querystring: newQuerystring,
S3Key: newURL,
};
// Make sure we're dealing with a supported file extension
const originalExtension = path.split('.').pop().toLowerCase();
const allowedExtensions = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
if (allowedExtensions.indexOf(originalExtension) === -1) {
returnObj.code = 'invalid-extension';
returnObj.reason = originalExtension + ' is not an allowed file extension!';
return reject(returnObj);
}
let matchingKeys = Helpers.getIntersectingValues(Object.keys(params), allowedParams);
if (matchingKeys.length === 0) {
returnObj.code = 'invalid-query-string';
returnObj.reason = 'No valid query strings were used: ' + Object.keys(params);
return reject(returnObj);
}
return resolve(returnObj);
}); // end Promise
};
/**
* Check if the image is already cached on S3 based on the query string
*
* @param {object} data Data about the image from setup()
* @return {Promise|object} Returns a promise that resolves to an object with various data about the image
*/
module.exports.checkIfCached = function (data) {
console.log('checkIfCached');
return new Promise(function (resolve, reject) {
if (!('S3Key' in data)) {
data.code = 'no-s3-key';
data.reason = 'No S3 Key was passed to checkIfCached';
return reject(data);
}
self.getFromS3(data.S3Key)
.then(function (s3Obj) {
data.obj = s3Obj;
data.code = 'found-on-s3';
// Sweet! We're done. End the Promise chain...
return reject(data)
})
.catch(function (err) {
// Nothing in cache so keep going down the Promise chain...
return resolve(data);
});
}); // end Promise
};
/**
* Get the original, querystring-less image from S3
*
* @param {object} data Data about the image from setup()
* @return {Promise|object} Returns a promise that resolves to an object with various data about the image
*/
module.exports.getOriginal = function (data) {
console.log('getOriginal');
return new Promise(function (resolve, reject) {
if (!('path' in data)) {
data.code = 'no-path';
data.reason = 'No path was passed to getOriginal';
return reject(data);
}
self.getFromS3(data.path)
.then(function (s3Obj) {
data.obj = s3Obj;
return resolve(data);
})
.catch(function (err) {
// The original image wasn't found on S3
data.code = 'original-not-found-from-s3';
return reject(data);
});
}); // end Promise
};
/**
* Save the processed image back to S3 in a new /resized/ directory
*
* @param {object} data Data about the image from setup()
* @return {Promise|object} Returns a promise that resolves to an object with various data about the image
*/
module.exports.cacheProcessedImage = function (data) {
console.log('cacheProcessedImage');
return new Promise(function (resolve, reject) {
let args = {
key: data.S3Key,
body: data.obj.Body,
ContentType: data.obj.ContentType
};
self.putToS3(args)
.then(function (resp) {
data.code = 'cached-processed-image';
data.reason = 'The processed image was saved to S3';
resolve(data);
})
.catch(function (err) {
data.code = 'processed-image-not-cached';
data.reason = 'The processed image couldn\'t be cached on S3';
console.log(err);
return reject(data);
});
}); // end Promise
};
/**
* Helper for making a S3 read request
*
* @param {string} key Path to the image in the S3 bucket
* @return {Promise|object} Returns a promise that resolves to data about the S3 file
*/
module.exports.getFromS3 = function (key) {
console.log('Get S3 Key: ', key);
return S3
.getObject({
Bucket: config.bucket,
Key: key
})
.promise();
};
/**
* helper for making a S3 write request
*
* @param {object} args Object with the body of the file to be saved and the key of where the file should live in the S3 bucket
* @return {Promise|object} Returns a promise that resolves to data about the S3 file
*/
module.exports.putToS3 = function (args) {
console.log('Put S3 Key: ', args.key);
let expiry = new Date();
expiry.setFullYear(expiry.getFullYear() + 1);
let params = {
Bucket: config.bucket,
Key: args.key,
Body: args.body,
CacheControl: 'max-age=31536000',
Expires: expiry,
ACL: 'public-read'
};
if ('ContentType' in args) {
params.ContentType = args.ContentType;
}
return S3
.putObject(params)
.promise();
};
/**
* Process an image based on the query strings passed to the URL
*
* @param {object} data Data about the image from setup()
* @return {Promise|object} Returns a promise that resolves to an object with various data about the image
*/
module.exports.processImage = function (data) {
console.log('processImage');
let buffer = data.obj.Body;
let args = data.querystring;
return new Promise(function (resolve, reject) {
var image = Sharp(buffer).withMetadata();
image.metadata(function (err, metadata) {
// Auto rotate based on orientation exif data
let rotation = null;
if (args.rotate) {
rotation = Number(args.rotate);
if ([0, 90, 180, 270].indexOf(rotation) == -1) {
rotation = null;
}
}
image.rotate(rotation);
// Set content type
switch (metadata.format) {
case 'jpg':
case 'jpeg':
data.obj.ContentType = 'image/jpeg';
break;
case 'png':
data.obj.ContentType = 'image/png';
break;
case 'gif':
data.obj.ContentType = 'image/gif';
break;
case 'webp':
data.obj.ContentType = 'image/webp';
break;
}
// Convert gifs to pngs unless animated
if (metadata.format === 'gif') {
if (isAnimated(buffer)) {
data.code = 'animated-gif';
data.reason = 'The image being processed is an animated gif (unprocessable)';
data.obj.Body = '';
return reject(data);
} else {
image.png();
data.obj.ContentType = 'image/png';
}
}
// Normalize dimensions
if (args.w) {
args.w = Math.min(args.w, metadata.width);
}
if (args.h) {
args.h = Math.min(args.h, metadata.height);
}
// Reverse the colors
if (args.negative) {
image.negate();
}
// Flip image vertically
if (args.flip) {
image.flip();
}
// Flip the image horizontally
if (args.flop) {
image.flop();
}
// Convert to black and white
if (args.grayscale || args.greyscale) {
image.grayscale();
}
// Crop (assumes crop data from original)
if (args.crop) {
var cropValues = args.crop;
if (typeof args.crop === 'string') {
cropValues = args.crop.split(',')
}
// Convert percantages 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],
});
}
// Resize
if (args.resize) {
if (typeof args.resize === 'string') {
args.resize = args.resize.split(',');
}
image.resize.apply(
image,
args.resize.map(function (v) {
return Number(v) || null;
})
);
} else if (args.fit) {
if (typeof args.fit === 'string') {
args.fit = args.fit.split(',');
}
image.resize.apply(
image,
args.fit.map(function (v) {
return Number(v) || null;
})
);
image.max();
} else if (args.lb) {
if (typeof args.lb === 'string') {
args.lb = args.lb.split(',');
}
image.resize.apply(
image,
args.lb.map(function (v) {
return Number(v) || null;
})
);
// Default to a black background to replicate Photon API behaviour
// when no background color specified
if (!args.background) {
args.background = 'black';
}
image.background(args.background);
image.embed();
} else if (args.w || args.h) {
image.resize(
Number(args.w) || null,
Number(args.h) || null
);
if (!args.crop) {
image.max();
}
}
// Allow override of compression quality
if (args.webp) {
image.webp({
quality: args.quality
? Math.min(
Math.max(Number(args.quality), 0)
, 100)
: 80,
});
data.obj.ContentType = 'image/webp';
} else if (metadata.format === 'jpeg' && args.quality) {
image.jpeg({
quality: Math.min(
Math.max(Number(args.quality), 0),
100
),
});
}
// Save the image out
image.toBuffer(function (err, img) {
if (err) {
data.code = 'error-processing-image';
data.reason = err;
return reject(data);
}
data.code = 'processed-image';
data.obj.Body = img;
if (typeof img === 'Buffer') {
data.obj.ContentLength = Buffer.byteLength(img);
}
return resolve(data);
});
});
}); // end Promise
};