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

80 lines
2.0 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) => {
if (!comment?.thumbnailUrl && !comment?.link) {
return;
}
2023-04-27 11:34:25 +02:00
2023-03-23 14:05:59 +01:00
if (comment?.link) {
2023-04-12 17:09:12 +02:00
try {
const url = new URL(comment.link);
const host = url.hostname;
2023-09-09 17:09:09 +02:00
let scrapedThumbnailUrl;
if (['youtube.com', 'www.youtube.com', 'youtu.be'].includes(host)) {
const videoId = host === 'youtu.be' ? url.pathname.slice(1) : url.searchParams.get('v');
2023-09-09 17:09:09 +02:00
scrapedThumbnailUrl = `https://img.youtube.com/vi/${videoId}/sddefault.jpg`;
} else if (host.includes('bitchute.com')) {
const videoId = url.pathname.split('/')[2];
2023-09-09 17:09:09 +02:00
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];
2023-09-09 17:09:09 +02:00
scrapedThumbnailUrl = `https://cdn-cf-east.streamable.com/image/${videoId}.jpg`;
}
2023-09-09 17:09:09 +02:00
if (canEmbed(url)) {
return {
url: comment.link,
type: 'iframe',
2023-09-09 17:09:09 +02:00
thumbnail: comment.thumbnailUrl || scrapedThumbnailUrl,
};
}
2023-07-27 16:56:40 +02:00
const mime = extName(url.pathname.toLowerCase().replace('/', ''))[0]?.mime;
if (mime?.startsWith('image')) {
return {
url: comment.link,
type: 'image',
};
}
if (mime?.startsWith('video')) {
return {
url: comment.link,
type: 'video',
thumbnail: comment.thumbnailUrl,
};
}
if (mime?.startsWith('audio')) {
return {
url: comment.link,
type: 'audio',
};
}
2023-04-12 17:09:12 +02:00
} catch (error) {
return;
}
2023-03-23 14:05:59 +01:00
}
2023-04-27 11:34:25 +02:00
2023-04-27 18:33:51 +02:00
if (comment?.thumbnailUrl && comment?.thumbnailUrl !== comment?.link) {
2023-04-27 11:34:25 +02:00
return {
2023-04-30 16:23:32 +02:00
url: comment.link,
2023-04-27 11:34:25 +02:00
type: 'webpage',
2023-04-30 16:23:32 +02:00
thumbnail: comment.thumbnailUrl,
2023-04-27 11:34:25 +02:00
};
}
2023-04-29 17:10:11 +02:00
if (comment?.link) {
return {
url: comment.link,
type: 'webpage',
};
}
2023-03-23 14:05:59 +01:00
};
2023-04-27 11:34:25 +02:00
export default getCommentMediaInfo;