fixed discord video embedding

This commit is contained in:
Sergio Brighenti 2022-06-19 23:21:27 +02:00
parent 8bf64a6537
commit 1979c3f318
4 changed files with 67 additions and 14 deletions

View file

@ -53,10 +53,6 @@ class MediaController extends Controller
$userAgent = $request->getHeaderLine('User-Agent');
$mime = $filesystem->getMimetype($media->storage_path);
if (UA::isBot($userAgent) && !(UA::embedsLinks($userAgent) && isDisplayableImage($mime) && $this->getSetting('image_embeds') === 'on')) {
return $this->streamMedia($request, $response, $filesystem, $media);
}
try {
$media->mimetype = $mime;
$media->extension = pathinfo($media->filename, PATHINFO_EXTENSION);
@ -80,6 +76,22 @@ class MediaController extends Controller
throw new HttpNotFoundException($request);
}
if (
UA::isBot($userAgent) &&
!(
// embed if enabled
(UA::embedsLinks($userAgent) &&
isEmbeddable($mime) &&
$this->getSetting('image_embeds') === 'on') ||
// if the file is too large to be displayed as non embedded
(UA::embedsLinks($userAgent) &&
isEmbeddable($mime) &&
$size >= (8 * 1024 * 1024))
)
) {
return $this->streamMedia($request, $response, $filesystem, $media);
}
return view()->render($response, 'upload/public.twig', [
'delete_token' => $token,
'media' => $media,
@ -133,9 +145,9 @@ class MediaController extends Controller
$media = $this->getMedia($userCode, $mediaCode, false);
if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get(
'admin',
false
))) {
'admin',
false
))) {
throw new HttpNotFoundException($request);
}
@ -167,9 +179,9 @@ class MediaController extends Controller
$media = $this->getMedia($userCode, $mediaCode, false);
if (!$media || (!$media->published && $this->session->get('user_id') !== $media->user_id && !$this->session->get(
'admin',
false
))) {
'admin',
false
))) {
throw new HttpNotFoundException($request);
}
@ -373,9 +385,9 @@ class MediaController extends Controller
$mime = $storage->getMimetype($media->storage_path);
if ((param($request, 'width') !== null || param($request, 'height') !== null) && explode(
'/',
$mime
)[0] === 'image') {
'/',
$mime
)[0] === 'image') {
return $this->makeThumbnail(
$storage,
$media,

View file

@ -19,8 +19,9 @@ class UA
'Slack' => false,
'Twitterbot/' => false,
'discord' => true,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.6; rv:92.0) Gecko/20100101 Firefox/92.0' => true
// discord image bot
'Mozilla/5.0 (Macintosh; Intel Mac OS X 11.6; rv:92.0) Gecko/20100101 Firefox/92.0' => true,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0' => true,
];
/**

View file

@ -73,6 +73,30 @@ if (!function_exists('isDisplayableImage')) {
}
}
if (!function_exists('isEmbeddable')) {
/**
* @param ?string $mime
*
* @return bool
*/
function isEmbeddable(?string $mime): bool
{
return in_array($mime, [
'image/apng',
'image/bmp',
'image/gif',
'image/x-icon',
'image/jpeg',
'image/png',
'image/tiff',
'image/webp',
'video/mp4',
'video/ogg',
'video/webm',
]);
}
}
if (!function_exists('stringToBytes')) {
/**
* @param $str

View file

@ -19,6 +19,22 @@
<meta id="image-src" name="twitter:image:src" content="{{ url }}/raw">
<meta id="discord" name="twitter:image" content="{{ url }}/raw">
<meta id="image-src" name="twitter:image:src" content="{{ url }}/raw">
{% elseif type == 'video' %}
<meta name="twitter:card" content="player" />
<meta name="twitter:title" content="{{ media.filename }} ({{ media.size }})" />
<meta name="twitter:image" content="0" />
<meta name="twitter:player:stream" content="{{ url }}/raw" />
<meta name="twitter:player:width" content="720" />
<meta name="twitter:player:height" content="480" />
<meta name="twitter:player:stream:content_type" content="{{ media.mimetype }}" />
<meta property="og:url" content="{{ url }}/raw" />
<meta property="og:video" content="{{ url }}/raw" />
<meta property="og:video:secure_url" content="{{ url }}/raw" />
<meta property="og:video:type" content="{{ media.mimetype }}" />
<meta property="og:video:width" content="720" />
<meta property="og:video:height" content="480" />
<meta property="og:image" content="0" />
{% endif %}
{% endblock %}