Something went wrong. Try again.
:rocket: A fully-featured and deeply tested Cloudinary Ghost storage adapter ghost.org/integrations/cloudinary
Something went wrong. Try again.
ghost-storage-cloudinary index.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186'use strict';
require('bluebird');
const StorageBase = require('ghost-storage-base'), cloudinary = require('cloudinary').v2, path = require('path'), request = require('request').defaults({encoding: null}), plugin = require(path.join(__dirname, '/plugins')), debug = require('ghost-ignition').debug('adapter'), common = (() => { // Tries to include GhostError helper try { return require(path.join(__dirname, '../../../lib/common')); } catch (ex) { // Use local mock instead return require(path.join(__dirname, 'errors')); } })();
class CloudinaryAdapter extends StorageBase {
/** * @override */ constructor(options) { super(options);
const config = options || {}, auth = config.auth || config, // Kept to avoid a BCB with forked repo legacy = config.configuration || {}, uploadOptions = config.upload || legacy.file || {}, fetchOptions = config.fetch || legacy.image || {};
this.useDatedFolder = config.useDatedFolder || false; this.rjsOptions = config.rjs || null; this.uploaderOptions = { upload: uploadOptions, fetch: fetchOptions };
debug('useDatedFolder:', this.useDatedFolder); debug('uploaderOptions:', this.uploaderOptions); debug('rjsOptions:', this.rjsOptions);
cloudinary.config(auth); }
/** * @override */ exists(filename) { const pubId = this.toCloudinaryId(filename);
return new Promise((resolve) => cloudinary.uploader.explicit(pubId, {type: 'upload'}, (err) => { if (err) { return resolve(false); } return resolve(true); })); }
/** * @override */ save(image) { // Creates a deep clone of Cloudinary options const uploaderOptions = JSON.parse(JSON.stringify(Object.assign({}, this.uploaderOptions))); Object.assign( uploaderOptions.upload, {public_id: path.parse(this.getSanitizedFileName(image.name)).name} );
// Appends the dated folder if enabled if (this.useDatedFolder) { uploaderOptions.upload.folder = this.getTargetDir(uploaderOptions.upload.folder); }
debug('save:uploadOptions:', uploaderOptions);
// Retinizes images if there is any config provided if (this.rjsOptions) { const rjs = new plugin.RetinaJS(this.uploader, uploaderOptions, this.rjsOptions); return rjs.retinize(image); }
return this.uploader(image.path, uploaderOptions, true); }
/** * Uploads an image with options to Cloudinary * @param {string} imagePath The image path to upload (local or remote) * @param {object} options Cloudinary upload + fetch options * @param {boolean} url If true, will do an extra Cloudinary API call to fetch the uploaded image with fetch options * @return {Promise} The uploader Promise */ uploader(imagePath, options, url) { debug('uploader:imagePath', imagePath); debug('uploader:options', options); debug('uploader:url', url);
return new Promise((resolve, reject) => cloudinary.uploader.upload(imagePath, options.upload, (err, res) => { if (err) { return reject(new common.errors.GhostError({ err: err, message: `Could not upload image ${imagePath}` })); } if (url) { return resolve(cloudinary.url(res.public_id.concat('.', res.format), options.fetch)); } return resolve(); })); }
/** * @override */ serve() { return (req, res, next) => { next(); }; }
/** * @override */ delete(filename) { const pubId = this.toCloudinaryId(filename);
return new Promise((resolve, reject) => cloudinary.uploader.destroy(pubId, (err, res) => { if (err) { return reject(new common.errors.GhostError({ err: err, message: `Could not delete image ${filename}` })); } return resolve(res); })); }
/** * @override */ read(options) { const opts = options || {}; return new Promise((resolve, reject) => request.get(opts.path, (err, res) => { if (err) { return reject(new common.errors.GhostError({ err: err, message: `Could not read image ${opts.path}` })); } return resolve(res.body); })); }
/** * Extracts the a Cloudinary-ready file name for API usage. * If a "folder" upload option is set, it will prepend its * value. * @param {string} filename The wanted image filename * @return {string} Cloudinary-ready image name */ toCloudinaryFile(filename) { const file = path.parse(filename).base; if (typeof this.uploaderOptions.upload.folder !== 'undefined') { return path.join(this.uploaderOptions.upload.folder, file); } return file; }
/** * Returns the Cloudinary public ID off a given filename * @param {string} filename The wanted image filename * @return {string} Cloudinary-ready ID for given image */ toCloudinaryId(filename) { const parsed = path.parse(this.toCloudinaryFile(filename)); return path.join(parsed.dir, parsed.name); }}
module.exports = CloudinaryAdapter;