Skip to content
Permalink
Browse files

Refactor cloudinary integration

  • Loading branch information
marcomontalbano committed Jan 10, 2020
1 parent aa5d343 commit a19bced18a3fd93f971e4d055a1b229111bb6368
@@ -1,7 +1,6 @@
NODE_ENV=development
CLOUDINARY_CLOUD_NAME=
FACEBOOK_ACCESS_TOKEN=
IMGBB_API_KEY=
CLOUDINARY_URL=
GA_TRACKING_ID=
SITE_ID=
NETLIFY_ACCESS_TOKEN=
GA_TRACKING_ID=
FACEBOOK_ACCESS_TOKEN=
@@ -4,6 +4,5 @@
functions = "dist/server"

[template.environment]
NODE_ENV = "Node Environment"
CLOUDINARY_CLOUD_NAME = "Cloudinary Cloud Name"
CLOUDINARY_URL = "Cloudinary URL"
GA_TRACKING_ID = "Google Analytics Tracking ID"

Some generated files are not rendered by default. Learn more.

@@ -59,6 +59,7 @@
},
"dependencies": {
"clipboard": "^2.0.4",
"cloudinary": "~1.18.1",
"dotenv": "~8.2.0",
"hint.css": "^2.6.0",
"nprogress": "^0.2.0",
Binary file not shown.
@@ -1,7 +1,4 @@
import fetch from './proxiedFetch';
import * as imgbb from './imgbb';

const { CLOUDINARY_CLOUD_NAME, IMGBB_API_KEY } = process.env;
import cloudinary from './cloudinary';

export default class VideoProvider {
static get regex() {}
@@ -35,33 +32,19 @@ export default class VideoProvider {
return new Promise();
}

getThumbnail_asCloudinaryUrl() {
return this.getThumbnail_asVideoUrl().then(url => this.getThumbnail_validateUrl(url));
}

getThumbnail_asImgbbUrl() {
return this.getThumbnail_asCloudinaryUrl().then(cloudinaryUrl => imgbb.create(cloudinaryUrl));
}

getThumbnail() {
return this.getThumbnail_asImgbbUrl()
.then(url => {
this.log('getThumbnail', url);

return fetch(url).then(response => response.buffer()).then(buffer => ({ buffer, url }));
})
}

getThumbnail_validateUrl(url) {
if (this.constructor.useCloudinary && CLOUDINARY_CLOUD_NAME) {
const cloudinaryPlayIcon = this.options.showPlayIcon ? `l_video_to_markdown:${this.providerName}_play,g_center/` : '';
return `http://res.cloudinary.com/${CLOUDINARY_CLOUD_NAME}/image/fetch/h_720/${cloudinaryPlayIcon}${encodeURIComponent(url)}`;
}
getThumbnail_asUrl() {
return this.getThumbnail_asVideoUrl().then(videoUrl => {
if (!this.constructor.useCloudinary) {
return videoUrl;
}

return url;
return cloudinary.create(videoUrl, this, {
showPlayIcon: this.options.showPlayIcon
}).then(response => response.secure_url);
});
}

constructor(url, options) {
constructor(url, options = {}) {
if (!this.constructor.check(url)) {
throw new Error(
`Invalid url for ${this.providerName} provider.`
@@ -0,0 +1,36 @@
import { v2 as cloudinary } from 'cloudinary';
import crypto from 'crypto';

export const search = (providerName, videoId) => cloudinary.search
.expression(`resource_type:"image" AND folder="video_to_markdown/images" AND filename="${providerName}-${videoId}"`)
.sort_by('uploaded_at', 'desc')
.max_results(30)
.execute();

export const create = (source, video, options = {}) => new Promise((resolve, reject) => {
const transformations = options.showPlayIcon ? { overlay: `video_to_markdown:icons:${video.providerName}`, gravity: 'center' } : {};
const hash = crypto.createHash('md5').update(JSON.stringify(options)).digest('hex');

cloudinary.uploader.upload(source, {
folder: 'video_to_markdown/images',
public_id: `${video.providerName}--${video.getId()}-${hash}`,
context: `url=${video.url}`,
secure: true,
transformation: [
{ height: 720 },
{ ...transformations }
]
}, (error, result) => {
if (error) {
return reject(error);
}

resolve(result);
}
);
});

export default {
search,
create
};

This file was deleted.

@@ -1,5 +1,4 @@
import VideoWrapper from './classes/VideoWrapper';
import * as imgbb from './classes/imgbb';

import { sendLambdaEvent } from './classes/google-ua';

@@ -29,7 +28,6 @@ exports.handler = (event, context, callback) => {
sendLambdaEvent(event);

const url = getParam(event, 'url');
const image = getParam(event, 'image');

if (url === undefined || url === null) {
return throwException(422, 'param URL is mandatory.', callback);
@@ -38,52 +36,39 @@ exports.handler = (event, context, callback) => {
let video;
try {
video = VideoWrapper.create(url, {
showPlayIcon: getParam(event, 'showPlayIcon') === 'true'
showPlayIcon: getParam(event, 'showPlayIcon') === 'true',
image: getParam(event, 'image')
});
} catch (e) {
} catch (error) {
return callback(null, {
statusCode: 422,
body: JSON.stringify({
error: true,
message: e.message
message: error.message
})
});
}

video.log('httpMethod', event.httpMethod);
video.log('url', url);
video.log('image', image);

if (image) {
imgbb.create(image).then(imageUrl => {
video
.getThumbnail_asUrl()
.then(imageUrl => {
callback(null, {
statusCode: 200,
body: JSON.stringify({
provider: video.providerName,
url: video.url,
image: imageUrl
image: imageUrl,
}),
});
});
} else {
video
.getThumbnail_asImgbbUrl()
.then(imageUrl => {
callback(null, {
statusCode: 200,
body: JSON.stringify({
provider: video.providerName,
url: video.url,
image: imageUrl,
}),
});
})
.catch(error => {
callback(null, {
statusCode: 422,
body: JSON.stringify({ error: true })
});
})
.catch(error => {
callback(null, {
statusCode: 422,
body: JSON.stringify({ error: true })
});
}
});

};

0 comments on commit a19bced

Please sign in to comment.
You can’t perform that action at this time.