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

120 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-06-06 11:42:49 +09:00
import cloudinary from "cloudinary";
2016-06-23 11:40:35 +09:00
import Posts from "meteor/nova:posts";
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users';
2016-06-06 11:42:49 +09:00
const Cloudinary = cloudinary.v2;
2016-07-11 11:47:00 +09:00
const uploadSync = Meteor.wrapAsync(Cloudinary.uploader.upload);
2015-11-21 12:24:08 +09:00
Cloudinary.config({
2016-05-31 10:53:23 +09:00
cloud_name: Telescope.settings.get("cloudinaryCloudName"),
api_key: Telescope.settings.get("cloudinaryAPIKey"),
api_secret: Telescope.settings.get("cloudinaryAPISecret")
2015-11-21 12:24:08 +09:00
});
2016-07-11 11:47:00 +09:00
const CloudinaryUtils = {
// send an image URL to Cloudinary and get a cloudinary result object in return
uploadImage(imageUrl) {
try {
var result = uploadSync(Telescope.utils.addHttp(imageUrl));
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);
console.log(error.stack);
}
},
// generate signed URL for each format based off public_id
getUrls(cloudinaryId) {
return Telescope.settings.get("cloudinaryFormats").map(format => {
const url = Cloudinary.url(cloudinaryId, {
width: format.width,
height: format.height,
crop: 'fill',
sign_url: true,
fetch_format: "auto",
quality: "auto"
});
return {
name: format.name,
url: url
};
});
}
2016-05-31 10:53:23 +09:00
};
2015-11-21 12:24:08 +09:00
// methods
2015-11-21 12:24:08 +09:00
Meteor.methods({
testCloudinaryUpload: function (thumbnailUrl) {
2016-07-21 14:51:58 +09:00
if (Users.isAdmin(Meteor.user())) {
2016-05-31 10:53:23 +09:00
thumbnailUrl = typeof thumbnailUrl === "undefined" ? "http://www.telescopeapp.org/images/logo.png" : thumbnailUrl;
2016-07-11 11:47:00 +09:00
const data = CloudinaryUtils.uploadImage(thumbnailUrl);
console.log(data);
2015-11-21 12:24:08 +09:00
}
},
2016-06-06 11:42:49 +09:00
cachePostThumbnails: function (limit = 20) {
2016-07-21 14:51:58 +09:00
if (Users.isAdmin(Meteor.user())) {
var postsWithUncachedThumbnails = Posts.find({
thumbnailUrl: { $exists: true },
originalThumbnailUrl: { $exists: false }
}, {sort: {createdAt: -1}, limit: limit});
2016-06-06 11:42:49 +09:00
postsWithUncachedThumbnails.forEach(Meteor.bindEnvironment((post, index) => {
2016-06-06 11:42:49 +09:00
Meteor.setTimeout(function () {
console.log(`// ${index}. Caching thumbnail for post “${post.title}” (_id: ${post._id})`);
2016-07-11 11:47:00 +09:00
const data = CloudinaryUtils.uploadImage(post.thumbnailUrl);
2016-06-06 11:42:49 +09:00
Posts.update(post._id, {$set:{
2016-07-11 11:47:00 +09:00
cloudinaryId: data.cloudinaryId,
cloudinaryUrls: data.urls
2016-06-06 11:42:49 +09:00
}});
}, index * 1000);
}));
}
2015-11-21 12:24:08 +09:00
}
});
// post submit callback
function cachePostThumbnailOnSubmit (post) {
2016-05-31 10:53:23 +09:00
if (Telescope.settings.get("cloudinaryAPIKey")) {
if (post.thumbnailUrl) {
2016-07-11 11:47:00 +09:00
const data = CloudinaryUtils.uploadImage(post.thumbnailUrl);
Posts.update(post._id, {$set:{
cloudinaryId: data.cloudinaryId,
cloudinaryUrls: data.urls
}});
}
2015-11-21 12:24:08 +09:00
}
}
Telescope.callbacks.add("posts.new.async", cachePostThumbnailOnSubmit);
2015-11-21 12:24:08 +09:00
// post edit callback
function cachePostThumbnailOnEdit (newPost, oldPost) {
2016-05-31 10:53:23 +09:00
if (Telescope.settings.get("cloudinaryAPIKey")) {
if (newPost.thumbnailUrl && newPost.thumbnailUrl !== oldPost.thumbnailUrl) {
2016-07-11 11:47:00 +09:00
const data = CloudinaryUtils.uploadImage(newPost.thumbnailUrl);
Posts.update(newPost._id, {$set:{
cloudinaryId: data.cloudinaryId,
cloudinaryUrls: data.urls
}});
}
2015-11-21 12:24:08 +09:00
}
}
Telescope.callbacks.add("posts.edit.async", cachePostThumbnailOnEdit);
2016-07-11 11:47:00 +09:00
export default CloudinaryUtils;