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-12-12 15:00:56 +09:00
|
|
|
import { Callbacks, Utils, getSetting } from 'meteor/nova:core';
|
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-12-12 15:00:56 +09:00
|
|
|
cloud_name: getSetting("cloudinaryCloudName"),
|
|
|
|
api_key: getSetting("cloudinaryAPIKey"),
|
|
|
|
api_secret: getSetting("cloudinaryAPISecret"),
|
2016-10-01 14:45:26 +02:00
|
|
|
secure: true,
|
2015-11-21 12:24:08 +09:00
|
|
|
});
|
|
|
|
|
2016-07-11 11:47:00 +09:00
|
|
|
const CloudinaryUtils = {
|
2016-11-26 02:46:55 +08:00
|
|
|
|
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) {
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log("// Cloudinary upload failed for URL: "+imageUrl); // eslint-disable-line
|
|
|
|
console.log(error.stack); // eslint-disable-line
|
2016-07-11 11:47:00 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// generate signed URL for each format based off public_id
|
|
|
|
getUrls(cloudinaryId) {
|
2016-12-12 15:00:56 +09:00
|
|
|
return getSetting("cloudinaryFormats").map(format => {
|
2016-07-11 11:47:00 +09:00
|
|
|
const url = Cloudinary.url(cloudinaryId, {
|
2016-11-26 02:46:55 +08:00
|
|
|
width: format.width,
|
|
|
|
height: format.height,
|
2016-07-11 11:47:00 +09:00
|
|
|
crop: 'fill',
|
|
|
|
sign_url: true,
|
|
|
|
fetch_format: "auto",
|
|
|
|
quality: "auto"
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
name: format.name,
|
|
|
|
url: url
|
|
|
|
};
|
|
|
|
});
|
2015-12-11 16:55:20 +09:00
|
|
|
}
|
2016-05-31 10:53:23 +09:00
|
|
|
};
|
2015-11-21 12:24:08 +09:00
|
|
|
|
2015-12-11 16:55:20 +09:00
|
|
|
// methods
|
2015-11-21 12:24:08 +09:00
|
|
|
Meteor.methods({
|
2015-12-11 16:55:20 +09:00
|
|
|
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);
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log(data); // eslint-disable-line
|
2015-11-21 12:24:08 +09:00
|
|
|
}
|
2015-12-11 16:55:20 +09:00
|
|
|
},
|
2016-06-06 11:42:49 +09:00
|
|
|
cachePostThumbnails: function (limit = 20) {
|
2015-12-11 16:55:20 +09:00
|
|
|
|
2016-07-21 14:51:58 +09:00
|
|
|
if (Users.isAdmin(Meteor.user())) {
|
2015-12-11 16:55:20 +09:00
|
|
|
|
|
|
|
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) => {
|
2015-12-11 16:55:20 +09:00
|
|
|
|
2016-06-06 11:42:49 +09:00
|
|
|
Meteor.setTimeout(function () {
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log(`// ${index}. Caching thumbnail for post “${post.title}” (_id: ${post._id})`); // eslint-disable-line
|
2015-12-11 16:55:20 +09:00
|
|
|
|
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
|
|
|
}});
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-06-06 11:42:49 +09:00
|
|
|
}, index * 1000);
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2015-12-11 16:55:20 +09:00
|
|
|
}));
|
|
|
|
}
|
2015-11-21 12:24:08 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// post submit callback
|
|
|
|
function cachePostThumbnailOnSubmit (post) {
|
2016-12-12 15:00:56 +09:00
|
|
|
if (getSetting("cloudinaryAPIKey")) {
|
2015-11-21 12:26:19 +09:00
|
|
|
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:26:19 +09:00
|
|
|
}
|
2015-11-21 12:24:08 +09:00
|
|
|
}
|
|
|
|
}
|
2016-12-12 10:24:34 +09:00
|
|
|
Callbacks.add("posts.new.async", cachePostThumbnailOnSubmit);
|
2015-11-21 12:24:08 +09:00
|
|
|
|
|
|
|
// post edit callback
|
|
|
|
function cachePostThumbnailOnEdit (newPost, oldPost) {
|
2016-12-12 15:00:56 +09:00
|
|
|
if (getSetting("cloudinaryAPIKey")) {
|
2015-11-21 12:26:19 +09:00
|
|
|
if (newPost.thumbnailUrl && newPost.thumbnailUrl !== oldPost.thumbnailUrl) {
|
2016-11-26 02:46:55 +08:00
|
|
|
|
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:26:19 +09:00
|
|
|
}
|
2015-11-21 12:24:08 +09:00
|
|
|
}
|
|
|
|
}
|
2016-12-12 10:24:34 +09:00
|
|
|
Callbacks.add("posts.edit.async", cachePostThumbnailOnEdit);
|
2016-07-11 11:47:00 +09:00
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
export default CloudinaryUtils;
|