Vulcan/packages/vulcan-cloudinary/lib/server/cloudinary.js

90 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-09-12 11:59:00 +09:00
import cloudinary from 'cloudinary';
import { Utils, getSetting, registerSetting } from 'meteor/vulcan:core';
registerSetting('cloudinary', null, 'Cloudinary settings');
2016-06-06 11:42:49 +09:00
2018-02-01 15:03:55 -06:00
export const Cloudinary = cloudinary.v2;
2016-07-11 11:47:00 +09:00
const uploadSync = Meteor.wrapAsync(Cloudinary.uploader.upload);
const cloudinarySettings = getSetting('cloudinary');
2015-11-21 12:24:08 +09:00
Cloudinary.config({
cloud_name: cloudinarySettings.cloudName,
api_key: cloudinarySettings.apiKey,
api_secret: cloudinarySettings.apiSecret,
2016-10-01 14:45:26 +02:00
secure: true,
2015-11-21 12:24:08 +09:00
});
2017-09-29 15:53:12 +09:00
export const CloudinaryUtils = {
2016-07-11 11:47:00 +09:00
// send an image URL to Cloudinary and get a cloudinary result object in return
uploadImage(imageUrl) {
try {
2016-12-12 11:34:28 +09:00
var result = uploadSync(Utils.addHttp(imageUrl));
2016-07-11 11:47:00 +09:00
const data = {
cloudinaryId: result.public_id,
result: result,
urls: CloudinaryUtils.getUrls(result.public_id)
};
return data;
} catch (error) {
console.log("// Cloudinary upload failed for URL: "+imageUrl); // eslint-disable-line
console.log(error); // eslint-disable-line
2016-07-11 11:47:00 +09:00
}
},
// generate signed URL for each format based off public_id
getUrls(cloudinaryId) {
return cloudinarySettings.formats.map(format => {
2016-07-11 11:47:00 +09:00
const url = Cloudinary.url(cloudinaryId, {
width: format.width,
height: format.height,
2016-07-11 11:47:00 +09:00
crop: 'fill',
sign_url: true,
2018-09-12 11:59:00 +09:00
fetch_format: 'auto',
quality: 'auto'
2016-07-11 11:47:00 +09:00
});
return {
name: format.name,
url: url
};
});
}
2016-05-31 10:53:23 +09:00
};
2015-11-21 12:24:08 +09:00
// methods
2017-09-13 12:13:04 +02:00
// Meteor.methods({
// testCloudinaryUpload: function (thumbnailUrl) {
// if (Users.isAdmin(Meteor.user())) {
// thumbnailUrl = typeof thumbnailUrl === "undefined" ? "http://www.telescopeapp.org/images/logo.png" : thumbnailUrl;
// const data = CloudinaryUtils.uploadImage(thumbnailUrl);
// console.log(data); // eslint-disable-line
// }
// },
// cachePostThumbnails: function (limit = 20) {
// if (Users.isAdmin(Meteor.user())) {
// console.log(`// caching ${limit} thumbnails…`)
// var postsWithUncachedThumbnails = Posts.find({
// thumbnailUrl: { $exists: true },
// originalThumbnailUrl: { $exists: false }
// }, {sort: {createdAt: -1}, limit: limit});
// postsWithUncachedThumbnails.forEach(Meteor.bindEnvironment((post, index) => {
// Meteor.setTimeout(function () {
// console.log(`// ${index}. Caching thumbnail for post “${post.title}” (_id: ${post._id})`); // eslint-disable-line
// const data = CloudinaryUtils.uploadImage(post.thumbnailUrl);
// Posts.update(post._id, {$set:{
// cloudinaryId: data.cloudinaryId,
// cloudinaryUrls: data.urls
// }});
// }, index * 1000);
// }));
// }
// }
2017-09-29 15:53:12 +09:00
// });