Files
5chan/src/utils/getCommentMediaInfo.js
T

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-03-23 14:05:59 +01:00
import extName from 'ext-name';
2023-09-09 17:09:09 +02:00
import { canEmbed } from '../components/Embed';
2023-03-23 14:05:59 +01:00
const getCommentMediaInfo = (comment) => {
2023-09-16 21:40:23 +02:00
if (!comment?.thumbnailUrl && !comment?.link) {
return;
}
2023-04-27 11:34:25 +02:00
2023-09-16 21:40:23 +02:00
if (comment?.link) {
try {
const url = new URL(comment.link);
const host = url.hostname;
let scrapedThumbnailUrl;
2023-09-16 21:40:23 +02:00
if (['youtube.com', 'www.youtube.com', 'youtu.be'].includes(host)) {
const videoId = host === 'youtu.be' ? url.pathname.slice(1) : url.searchParams.get('v');
scrapedThumbnailUrl = `https://img.youtube.com/vi/${videoId}/sddefault.jpg`;
} else if (host.includes('bitchute.com')) {
const videoId = url.pathname.split('/')[2];
scrapedThumbnailUrl = `https://static-3.bitchute.com/live/cover_images/F61vWF4shy8s/${videoId}_640x360.jpg`;
} else if (host.includes('streamable.com')) {
const videoId = url.pathname.split('/')[1];
scrapedThumbnailUrl = `https://cdn-cf-east.streamable.com/image/${videoId}.jpg`;
}
2023-09-16 21:40:23 +02:00
if (canEmbed(url)) {
return {
url: comment.link,
type: 'iframe',
thumbnail: comment.thumbnailUrl || scrapedThumbnailUrl,
};
}
2023-09-09 17:09:09 +02:00
2023-09-16 21:40:23 +02:00
const mime = extName(url.pathname.toLowerCase().replace('/', ''))[0]?.mime;
2023-09-16 21:40:23 +02:00
if (mime?.startsWith('image')) {
return {
url: comment.link,
type: 'image',
};
}
2023-09-16 21:40:23 +02:00
if (mime?.startsWith('video')) {
return {
url: comment.link,
type: 'video',
thumbnail: comment.thumbnailUrl,
};
}
2023-09-16 21:40:23 +02:00
if (mime?.startsWith('audio')) {
return {
url: comment.link,
type: 'audio',
};
}
} catch (error) {
return;
}
}
2023-09-16 21:40:23 +02:00
if (comment?.thumbnailUrl && comment?.thumbnailUrl !== comment?.link) {
return {
url: comment.link,
type: 'webpage',
thumbnail: comment.thumbnailUrl,
};
}
2023-09-16 21:40:23 +02:00
if (comment?.link) {
return {
url: comment.link,
type: 'webpage',
};
}
2023-03-23 14:05:59 +01:00
};
2023-09-16 21:40:23 +02:00
export default getCommentMediaInfo;