Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. // dependencies
  2. var async = require('async');
  3. var path = require('path');
  4. var AWS = require('aws-sdk');
  5. var gm = require('gm')
  6. .subClass({ imageMagick: true }); // Enable ImageMagick integration.
  7. var util = require('util');
  8.  
  9. // constants
  10. var MAX_WIDTH = 640;
  11. var MAX_HEIGHT = 100;
  12.  
  13. // get reference to S3 client
  14. var s3 = new AWS.S3();
  15.  
  16. exports.handler = function(event, context) {
  17. // Read options from the event.
  18. console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
  19. var srcBucket = event.Records[0].s3.bucket.name;
  20. // Object key may have spaces or unicode non-ASCII characters.
  21. var srcKey =
  22. decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
  23. var dstBucket = srcBucket;
  24. var dstKey = srcKey.replace('originals', 'resized');
  25. var extension = path.extname(dstKey);
  26. var filename = path.basename(dstKey, extension);
  27. var directory = path.dirname(dstKey);
  28. dstKey = directory + '/' + filename + extension;
  29.  
  30. // Sanity check: validate that source and destination are different buckets.
  31. // if (srcBucket == dstBucket) {
  32. // console.error("Destination bucket must not match source bucket.");
  33. // return;
  34. // }
  35. console.log('Dumping resized file to: ' + dstKey);
  36.  
  37. // Infer the image type.
  38. var typeMatch = srcKey.match(/\.([^.]*)$/);
  39. if (!typeMatch) {
  40. console.error('unable to infer image type for key ' + srcKey);
  41. return;
  42. }
  43. var imageType = typeMatch[1];
  44. if (imageType != "jpg" && imageType != "png") {
  45. console.log('skipping non-image ' + srcKey);
  46. return;
  47. }
  48.  
  49. // Download the image from S3, transform, and upload to a different S3 bucket.
  50. async.waterfall([
  51. function download(next) {
  52. // Download the image from S3 into a buffer.
  53. s3.getObject({
  54. Bucket: srcBucket,
  55. Key: srcKey
  56. },
  57. next);
  58. },
  59. function transform(response, next) {
  60. gm(response.Body).size(function(err, size) {
  61. // Infer the scaling factor to avoid stretching the image unnaturally.
  62. var scalingFactor = Math.min(
  63. MAX_WIDTH / size.width,
  64. MAX_HEIGHT / size.height
  65. );
  66. var width = scalingFactor * size.width;
  67. var height = scalingFactor * size.height;
  68. // var height = scalingFactor * size.height;
  69.  
  70. // Transform the image buffer in memory.
  71. this.resize(width, height)
  72. .toBuffer(imageType, function(err, buffer) {
  73. if (err) {
  74. next(err);
  75. } else {
  76. next(null, response.ContentType, buffer);
  77. }
  78. });
  79. });
  80. },
  81. function upload(contentType, data, next) {
  82. // Stream the transformed image to a different S3 bucket.
  83. s3.putObject({
  84. Bucket: dstBucket,
  85. Key: dstKey,
  86. Body: data,
  87. ContentType: contentType
  88. },
  89. next);
  90. }
  91. ], function (err) {
  92. if (err) {
  93. console.error(
  94. 'Unable to resize ' + srcBucket + '/' + srcKey +
  95. ' and upload to ' + dstBucket + '/' + dstKey +
  96. ' due to an error: ' + err
  97. );
  98. } else {
  99. console.log(
  100. 'Successfully resized ' + srcBucket + '/' + srcKey +
  101. ' and uploaded to ' + dstBucket + '/' + dstKey
  102. );
  103. }
  104.  
  105. context.done();
  106. }
  107. );
  108. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement