Skip to content
Permalink
Browse files

Cache image with imgbb. Add image permanent redirection.

  • Loading branch information...
marcomontalbano committed Sep 19, 2019
1 parent e22270b commit c9d44dc47f91a159837de472b7b58b18625d2095
@@ -19,7 +19,7 @@ export default class Asciinema extends VideoProvider {
return false
}

getThumbnail_asUrl() {
getThumbnail_asVideoUrl() {
return new Promise(resolve => resolve(`${this.url}.png`));
}
}
@@ -19,7 +19,7 @@ export default class Dailymotion extends VideoProvider {
];
}

getThumbnail_asUrl() {
getThumbnail_asVideoUrl() {
const endpoint = `https://api.dailymotion.com/video/${this.getId()}?fields=title,thumbnail_720_url`;

return fetch(endpoint)
@@ -21,7 +21,7 @@ export default class Facebook extends VideoProvider {
];
}

getThumbnail_asUrl() {
getThumbnail_asVideoUrl() {
// unfortunately the FACEBOOK_ACCESS_TOKEN is temporally and there is no way to get a quality thumbnail without a valid token.
if (FACEBOOK_ACCESS_TOKEN) {
return fetch(`https://graph.facebook.com/${this.getId()}?access_token=${FACEBOOK_ACCESS_TOKEN}&fields=title,description,updated_time,id,thumbnails`)
@@ -19,7 +19,7 @@ export default class Vimeo extends VideoProvider {
];
}

getThumbnail_asUrl() {
getThumbnail_asVideoUrl() {
const endpoint = `https://vimeo.com/api/v2/video/${this.getId()}.json`;

return fetch(endpoint)
@@ -34,7 +34,7 @@ export default class Youtube extends VideoProvider {
];
}

getThumbnail_asUrl() {
getThumbnail_asVideoUrl() {
const maxVideoImage = `https://i.ytimg.com/vi/${this.getId()}/maxresdefault.jpg`;
const hqVideoImage = `https://i.ytimg.com/vi/${this.getId()}/hqdefault.jpg`;

@@ -1,6 +1,6 @@
import fetch from './proxiedFetch';

const { CLOUDINARY_CLOUD_NAME } = process.env;
const { CLOUDINARY_CLOUD_NAME, IMGBB_API_KEY } = process.env;

export default class VideoProvider {
static get regex() {}
@@ -30,17 +30,28 @@ export default class VideoProvider {
return this.constructor.getVideoId(this.url);
}

getThumbnail_asUrl() {
return new Promise()
getThumbnail_asVideoUrl() {
return new Promise();
}

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

getThumbnail_asImgbbUrl() {
return this.getThumbnail_asCloudinaryUrl().then(url => {
return fetch(`https://api.imgbb.com/1/upload?key=${IMGBB_API_KEY}&image=${this.getThumbnail_validateUrl(url)}`)
.then(response => response.json())
.then(json => json.data.image.url);
});
}

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

return fetch(this.getThumbnail_validateUrl(url))
.then(response => response.buffer());
return fetch(url).then(response => response.buffer());
})
}

@@ -36,21 +36,38 @@ exports.handler = (event, context, callback) => {
video.log('GET', url);

video
.getThumbnail_asBuffer()
.then(buffer => {
.getThumbnail_asImgbbUrl()
.then(url => {
callback(null, {
statusCode: 200,
statusCode: 301,
headers: {
'Content-Type': fileType(buffer).mime
},
body: isDevelopment ? buffer : buffer.toString('base64'),
isBase64Encoded: isDevelopment ? false : true
});
'Location': url
}
});;
})
.catch(error => {
callback(null, {
statusCode: 422,
body: JSON.stringify({ error: true })
});
});

// video
// .getThumbnail_asBuffer()
// .then(buffer => {
// callback(null, {
// statusCode: 200,
// headers: {
// 'Content-Type': fileType(buffer).mime
// },
// body: isDevelopment ? buffer : buffer.toString('base64'),
// isBase64Encoded: isDevelopment ? false : true
// });
// })
// .catch(error => {
// callback(null, {
// statusCode: 422,
// body: JSON.stringify({ error: true })
// });
// });
};

0 comments on commit c9d44dc

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