Merge pull request 'Proxyable embeds for imgur' (#249) from 3np/teddit:imgur-proxy into main

Reviewed-on: https://codeberg.org/teddit/teddit/pulls/249
This commit is contained in:
teddit 2021-11-27 15:07:48 +01:00
commit 0eb5ce1b6e
2 changed files with 26 additions and 7 deletions

View File

@ -69,7 +69,9 @@ const config = {
initial_limit: 100, // This is the amount of page loads one IP address can make in one minute without getting limited.
limit_after_limited: 30 // When an IP is limited, this is the amount of page loads the IP can make in one minute.
},
valid_media_domains: ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'emoji.redditmedia.com', 'styles.redditmedia.com', 'www.redditstatic.com', 'thumbs.gfycat.com', 'i.ytimg.com'],
valid_media_domains: process.env.VALID_MEDIA_DOMAINS
? JSON.parse(process.env.VALID_MEDIA_DOMAINS)
: ['preview.redd.it', 'external-preview.redd.it', 'i.redd.it', 'v.redd.it', 'a.thumbs.redditmedia.com', 'b.thumbs.redditmedia.com', 'emoji.redditmedia.com', 'styles.redditmedia.com', 'www.redditstatic.com', 'thumbs.gfycat.com', 'i.ytimg.com'],
valid_embed_video_domains: ['gfycat.com', 'youtube.com'],
reddit_api_error_text: `Seems like your instance is either blocked (e.g. due to API rate limiting), reddit is currently down, or your API key is expired and not renewd properly. This can also happen for other reasons.`
};

View File

@ -93,9 +93,12 @@ module.exports = function() {
if(!gif_to_mp4) {
if(post.preview) {
if(post.preview.reddit_video_preview) {
if(post.preview.reddit_video_preview.fallback_url) {
const url = post.domain === 'i.imgur.com'
? replaceDomains(post.url_overridden_by_dest.replace(/\.gifv$/, '.mp4'))
: post.preview.reddit_video_preview.fallback_url;
if(url) {
obj.media = {
source: await downloadAndSave(post.preview.reddit_video_preview.fallback_url),
source: await downloadAndSave(url),
height: post.preview.reddit_video_preview.height,
width: post.preview.reddit_video_preview.width,
duration: post.preview.reddit_video_preview.duration,
@ -127,12 +130,26 @@ module.exports = function() {
*/
if(!post_media && !has_gif && !post.gallery_data && post.url != '') {
try {
let u = new URL(post.url)
let url = replaceDomains(post.url)
const u = new URL(url)
if(config.valid_media_domains.includes(u.hostname)) {
let ext = u.pathname.split('.')[1]
if(ext === 'jpg' || ext === 'png') {
const ext = u.pathname.split('.')[1]
if(['jpg', 'png', 'jpeg', 'gif'].includes(ext)) {
obj.images = {
source: await downloadAndSave(post.url)
source: await downloadAndSave(url)
}
}
else if(['gifv', 'mp4'].includes(ext)) {
if (obj.domain === 'i.imgur.com') {
url = url.replace(/\.gifv$/, '.mp4');
}
obj.has_media = true
obj.media = {
source: await downloadAndSave(url)
}
if (post.preview && post.preview.images) {
obj.media.height = post.preview.images[0].source.height;
obj.media.width = post.preview.images[0].source.width;
}
}
}