Skip to content

Commit

Permalink
Refactor cloudinary integration
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Jan 10, 2020
1 parent aa5d343 commit a19bced
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 87 deletions.
7 changes: 3 additions & 4 deletions .env.sample
@@ -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=
3 changes: 1 addition & 2 deletions netlify.toml
Expand Up @@ -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"
21 changes: 17 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
Binary file not shown.
39 changes: 11 additions & 28 deletions src/lambda/classes/VideoProvider.js
@@ -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() {}
Expand Down Expand Up @@ -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.`
Expand Down
36 changes: 36 additions & 0 deletions src/lambda/classes/cloudinary.js
@@ -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
};
20 changes: 0 additions & 20 deletions src/lambda/classes/imgbb.js

This file was deleted.

43 changes: 14 additions & 29 deletions src/lambda/image-json.js
@@ -1,5 +1,4 @@
import VideoWrapper from './classes/VideoWrapper';
import * as imgbb from './classes/imgbb';

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

Expand Down Expand Up @@ -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);
Expand All @@ -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.