Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. var async = require("async");
  2. var AWS = require("aws-sdk");
  3. var gm = require("gm").subClass({imageMagick: false});
  4. var fs = require("fs");
  5. var mktemp = require("mktemp");
  6.  
  7. var THUMB_KEY_PREFIX = "thumbnails/",
  8. THUMB_WIDTH = 1920,
  9. THUMB_HEIGHT = 1080,
  10. ALLOWED_FILETYPES = ['png', 'jpg', 'jpeg', 'bmp', 'tiff', 'pdf', 'gif'];
  11.  
  12. var utils = {
  13. decodeKey: function(key) {
  14. return decodeURIComponent(key).replace(/+/g, ' ');
  15. }
  16. };
  17.  
  18.  
  19. var s3 = new AWS.S3();
  20.  
  21.  
  22. exports.handler = function(event, context) {
  23. var bucket = event.Records[0].s3.bucket.name,
  24. srcKey = utils.decodeKey(event.Records[0].s3.object.key),
  25. dstKey = THUMB_KEY_PREFIX + srcKey.replace(/.w+$/, ".jpg"),
  26. fileType = srcKey.match(/.w+$/);
  27.  
  28. if(srcKey.indexOf(THUMB_KEY_PREFIX) === 0) {
  29. return;
  30. }
  31.  
  32. if (fileType === null) {
  33. console.error("Invalid filetype found for key: " + srcKey);
  34. return;
  35. }
  36.  
  37. fileType = fileType[0].substr(1);
  38.  
  39. if (ALLOWED_FILETYPES.indexOf(fileType) === -1) {
  40. console.error("Filetype " + fileType + " not valid for thumbnail, exiting");
  41. return;
  42. }
  43.  
  44. async.waterfall([
  45.  
  46. function download(next) {
  47. //Download the image from S3
  48. s3.getObject({
  49. Bucket: bucket,
  50. Key: srcKey
  51. }, next);
  52. },
  53.  
  54. function createThumbnail(response, next) {
  55. var temp_file, image;
  56. var responseBody = new Buffer(response.Body);
  57.  
  58. if(fileType === "pdf") {
  59. temp_file = mktemp.createFileSync("/tmp/XXXXXXXXXX.pdf")
  60. fs.writeFileSync(temp_file, responseBody);
  61. image = gm(temp_file + "[0]");
  62. } else if (fileType === 'gif') {
  63. temp_file = mktemp.createFileSync("/tmp/XXXXXXXXXX.gif")
  64. fs.writeFileSync(temp_file, responseBody);
  65. image = gm(temp_file + "[0]");
  66. } else {
  67. image = gm(response.Body);
  68. }
  69.  
  70. image.size(function(err, size) {
  71. /*
  72. * scalingFactor should be calculated to fit either the width or the height
  73. * within 150x150 optimally, keeping the aspect ratio. Additionally, if the image
  74. * is smaller than 150px in both dimensions, keep the original image size and just
  75. * convert to png for the thumbnail's display
  76. */
  77.  
  78. // var scalingFactor = Math.min(1, THUMB_WIDTH / size.width, THUMB_HEIGHT / size.height),
  79. // width = scalingFactor * size.width,
  80. // height = scalingFactor * size.height;
  81.  
  82. this.resize(THUMB_WIDTH, THUMB_HEIGHT)
  83. .toBuffer("jpg", function(err, buffer) {
  84. if(temp_file) {
  85. fs.unlinkSync(temp_file);
  86. }
  87.  
  88. if (err) {
  89. next(err);
  90. } else {
  91. next(null, response.contentType, buffer);
  92. }
  93. });
  94. });
  95. },
  96.  
  97. function uploadThumbnail(contentType, data, next) {
  98. s3.putObject({
  99. Bucket: bucket,
  100. Key: dstKey,
  101. Body: data,
  102. ContentType: "image/jpeg",
  103. ACL: 'public-read',
  104. Metadata: {
  105. thumbnail: 'TRUE'
  106. }
  107. }, next);
  108. }
  109.  
  110. ],
  111. function(err) {
  112. if (err) {
  113. console.error(
  114. "Unable to generate thumbnail for '" + bucket + "/" + srcKey + "'" +
  115. " due to error: " + err
  116. );
  117. } else {
  118. console.log("Created thumbnail for '" + bucket + "/" + srcKey + "'");
  119. }
  120.  
  121. context.done();
  122. });
  123. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement