var cloudinary = require('cloudinary'),
dotenv = require('dotenv');
...
// ### CloudinaryUrl Helper//
// *Usage example:*
// `{{cloudinaryUrl heroImage width=1200 height=500 crop='fill' gravity='north'}}`
//
// Returns an src-string for a cloudinary image
_helpers.cloudinaryUrl = function(context, options) {
dotenv.load();
// if we dont pass in a context and just kwargs
// then `this` refers to our default scope block and kwargs
// are stored in context.hash
if (!options && context.hasOwnProperty('hash')) {
// strategy is to place context kwargs into options
options = context;
// bind our default inherited scope into context
context = this;
}
// safe guard to ensure context is never null
context = context === null ? undefined : context;
if (context) {
if (context.public_id) {
var imageName = context.public_id.concat('.',context.format);
// use a regex to strip out the cloudinary username
var cloudinaryRegex = /cloudinary\:\/\/([0-9]+)\:(.+)\@(.+)/;
var cloudConfigHash = process.env.CLOUDINARY_URL.match(cloudinaryRegex);
return cloudinary.url(imageName, options.hash);
}
}
};