/* globals exports, require */ //jshint strict: false //jshint esversion: 6 "use strict"; const crc32 = require("fast-crc32c"); const gcs = require("@google-cloud/storage")(); const PubSub = require("@google-cloud/pubsub"); const imagemagick = require("imagemagick-stream"); exports.thumbnail = (event, context) => { const fileName = event.name; const bucketName = event.bucket; const size = "64x64" const bucket = gcs.bucket(bucketName); const topicName = "REPLACE_WITH_YOUR_TOPIC ID"; const pubsub = new PubSub(); if ( fileName.search("64x64_thumbnail") == -1 ){ // doesn't have a thumbnail, get the filename extension var filename_split = fileName.split('.'); var filename_ext = filename_split[filename_split.length - 1]; var filename_without_ext = fileName.substring(0, fileName.length - filename_ext.length ); if (filename_ext.toLowerCase() == 'png' || filename_ext.toLowerCase() == 'jpg'){ // only support png and jpg at this point console.log(`Processing Original: gs://${bucketName}/${fileName}`); const gcsObject = bucket.file(fileName); let newFilename = filename_without_ext + size + '_thumbnail.' + filename_ext; let gcsNewObject = bucket.file(newFilename); let srcStream = gcsObject.createReadStream(); let dstStream = gcsNewObject.createWriteStream(); let resize = imagemagick().resize(size).quality(90); srcStream.pipe(resize).pipe(dstStream); return new Promise((resolve, reject) => { dstStream .on("error", (err) => { console.log(`Error: ${err}`); reject(err); }) .on("finish", () => { console.log(`Success: ${fileName} → ${newFilename}`); // set the content-type gcsNewObject.setMetadata( { contentType: 'image/'+ filename_ext.toLowerCase() }, function(err, apiResponse) {}); pubsub .topic(topicName) .publisher() .publish(Buffer.from(newFilename)) .then(messageId => { console.log(`Message ${messageId} published.`); }) .catch(err => { console.error('ERROR:', err); }); }); }); } else { console.log(`gs://${bucketName}/${fileName} is not an image I can handle`); } } else { console.log(`gs://${bucketName}/${fileName} already has a thumbnail`); } }; { "name": "thumbnails", "version": "1.0.0", "description": "Create Thumbnail of uploaded image", "scripts": { "start": "node index.js" }, "dependencies": { "@google-cloud/storage": "1.5.1", "@google-cloud/pubsub": "^0.18.0", "fast-crc32c": "1.0.4", "imagemagick-stream": "4.1.1" }, "devDependencies": {}, "engines": { "node": ">=4.3.2" } }
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.