Permalink
Browse files

Partially added 'image.json' API

  • Loading branch information...
marcomontalbano committed Sep 15, 2018
1 parent 17a28ec commit 00efde739c3fea05ca7ca12c189876b318da8d1d
Showing with 53 additions and 0 deletions.
  1. +53 −0 src/lambda/image.json.js
View
@@ -0,0 +1,53 @@
import fileType from 'file-type';
import VideoWrapper from './classes/VideoWrapper';
const isDevelopment = process.env.NODE_ENV === 'development';
const throwException = (statusCode, message, callback) => {
callback = callback || (() => {});
const error = {
statusCode: statusCode,
body: JSON.stringify({
errors: [
{
message: message
}
]
})
};
callback(null, error);
return error;
};
exports.handler = (event, context, callback) => {
const url = event.queryStringParameters.url;
const video = VideoWrapper.create(url);
if (url === undefined) {
return throwException(422, 'param URL is mandatory.', callback);
}
video.log('GET', url);
video
.getThumbnail_asBuffer()
.then(buffer => {
callback(null, {
statusCode: 200,
body: JSON.stringify({
provider: video.providerName,
url: video.url,
mime: fileType(buffer).mime,
base64: `data:${fileType(buffer).mime};base64,${buffer.toString('base64')}`,
}),
});
})
.catch(error => {
callback(null, {
statusCode: 422,
body: JSON.stringify({ error })
});
});
};

0 comments on commit 00efde7

Please sign in to comment.