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

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-03-23 14:05:59 +01:00
import extName from 'ext-name';
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;
let embedUrl = null;
if (['youtube.com', 'www.youtube.com', 'youtu.be'].includes(host)) {
const videoId = host === 'youtu.be' ? url.pathname.slice(1) : url.searchParams.get('v');
embedUrl = `https://www.youtube-nocookie.com/embed/${videoId}`;
2023-09-01 21:43:36 +02:00
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/sddefault.jpg`;
return {
url: comment.link,
embedUrl,
type: 'iframe',
thumbnail: comment.thumbnailUrl || thumbnailUrl,
};
} else if (host.includes('odysee.com')) {
const pathComponents = url.pathname.split('/');
const channelAndId = pathComponents[1];
const videoAndId = pathComponents[2];
embedUrl = `https://odysee.com/$/embed/${channelAndId}/${videoAndId}`;
// TODO: add support for Rumble, video id is dynamic
// } else if (host.includes('rumble.com')) {
// const regex = /(\/v.+?)-/;
// const match = regex.exec(url.pathname);
// if (match) {
// const videoId = match[1].slice(1);
// embedUrl = `https://rumble.com/embed/${videoId}/?pub=4`;
// }
} else if (host.includes('bitchute.com')) {
const videoId = url.pathname.split('/')[2];
embedUrl = `https://www.bitchute.com/embed/${videoId}/`;
} else if (host.includes('streamable.com')) {
const videoId = url.pathname.split('/')[1];
embedUrl = `https://streamable.com/e/${videoId}?quality=highest`;
// TODO: Add support for Giphy (doesn't have thumbnails, size is variable)
// } else if (host.includes('giphy.com')) {
// const hyphenComponents = url.pathname.split('-');
// const gifId = hyphenComponents[hyphenComponents.length - 1];
// embedUrl = `https://giphy.com/embed/${gifId}`;
}
if (embedUrl) {
return {
url: comment.link,
embedUrl,
type: 'iframe',
thumbnail: comment.thumbnailUrl,
};
}
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;