diff --git a/plugins/Embed/Controller/Embed.php b/plugins/Embed/Controller/OEmbed.php similarity index 99% rename from plugins/Embed/Controller/Embed.php rename to plugins/Embed/Controller/OEmbed.php index cb9d275678..f0c6915471 100644 --- a/plugins/Embed/Controller/Embed.php +++ b/plugins/Embed/Controller/OEmbed.php @@ -44,7 +44,7 @@ use Symfony\Component\HttpFoundation\Request; * @copyright 2019, 2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ -class Embed extends Controller +class OEmbed extends Controller { /** * Handle OEmbed server requests diff --git a/plugins/Embed/Embed.php b/plugins/Embed/Embed.php index b6654aaa42..63fee9bd40 100644 --- a/plugins/Embed/Embed.php +++ b/plugins/Embed/Embed.php @@ -118,8 +118,7 @@ class Embed extends Plugin */ public function onAddRoute(RouteLoader $m): bool { - $m->connect('oembed', 'main/oembed', Controller\Embed::class); - $m->connect('embed', 'main/embed', Controller\Embed::class); + $m->connect('oembed', 'main/oembed', Controller\OEmbed::class); return Event::next; } @@ -141,7 +140,7 @@ class Embed extends Plugin 'link' => [ 'rel' => 'alternate', 'type' => "application/{$format}+oembed", - 'href' => Router::url('embed', ['format' => $format, 'url' => $url]), + 'href' => Router::url('oembed', ['format' => $format, 'url' => $url]), 'title' => 'oEmbed', ], ]; } diff --git a/plugins/Embed/Entity/AttachmentEmbed.php b/plugins/Embed/Entity/AttachmentEmbed.php index 899a39b5a7..af9f3fb19d 100644 --- a/plugins/Embed/Entity/AttachmentEmbed.php +++ b/plugins/Embed/Entity/AttachmentEmbed.php @@ -57,8 +57,6 @@ class AttachmentEmbed extends Entity private ?string $author_name; private ?string $author_url; private ?string $thumbnail_url; - private ?int $thumbnail_width; - private ?int $thumbnail_height; private \DateTimeInterface $modified; public function setLinkId(int $link_id): self @@ -188,12 +186,13 @@ class AttachmentEmbed extends Entity { $attr = ['class' => 'u-photo embed']; $attachment = DB::find('attachment', ['id' => $this->getAttachmentId()]); + $thumbnail = $attachment->getThumbnail('medium'); if (is_null($attachment) || is_null($attachment->getWidth()) || is_null($attachment->getHeight())) { $attr['has_attachment'] = false; } else { $attr['has_attachment'] = true; - $attr['width'] = $attachment->getWidth(); - $attr['height'] = $attachment->getHeight(); + $attr['width'] = $thumbnail->getWidth(); + $attr['height'] = $thumbnail->getHeight(); } return $attr; } diff --git a/plugins/Embed/templates/embed/embedView.html.twig b/plugins/Embed/templates/embed/embedView.html.twig index ce3cfa27eb..2d0956cf2f 100644 --- a/plugins/Embed/templates/embed/embedView.html.twig +++ b/plugins/Embed/templates/embed/embedView.html.twig @@ -3,7 +3,7 @@ {% if attributes['has_attachment'] != false %} {% set thumbnail_parameters = { 'id': embed.getAttachmentId(), - 'size': 'small' + 'size': 'medium' } %} {{embed.getTitle() | escape}} + {% set thumbnail = attachment.getThumbnail() %} {{ attachment.getBestTitle(note) }} + src="{{ attachment.getThumbnailUrl() }}" + width="{{ thumbnail.getWidth() }}" + height="{{ thumbnail.getHeight() }}">
{{ attachment.getBestTitle(note) }}
diff --git a/plugins/StoreRemoteMedia/StoreRemoteMedia.php b/plugins/StoreRemoteMedia/StoreRemoteMedia.php index 67f81f25ff..047dd2b734 100644 --- a/plugins/StoreRemoteMedia/StoreRemoteMedia.php +++ b/plugins/StoreRemoteMedia/StoreRemoteMedia.php @@ -178,7 +178,7 @@ class StoreRemoteMedia extends Plugin if (!$this->getStoreOriginal()) { $thumbnail = AttachmentThumbnail::getOrCreate( attachment: $attachment, - size: 'small', + size: 'medium', crop: $this->getSmartCrop() ); $attachment->deleteStorage(); diff --git a/plugins/VideoEncoder/VideoEncoder.php b/plugins/VideoEncoder/VideoEncoder.php index 5a2e88f33a..b41bef9916 100644 --- a/plugins/VideoEncoder/VideoEncoder.php +++ b/plugins/VideoEncoder/VideoEncoder.php @@ -174,12 +174,12 @@ class VideoEncoder extends Plugin * * @return bool */ - public function onViewAttachmentVideo(array $vars, array &$res): bool + public function onViewAttachment(array $vars, array &$res): bool { $res[] = Formatting::twigRenderFile('videoEncoder/videoEncoderView.html.twig', - ['attachment' => $vars['attachment'], - 'thumbnail_parameters' => $vars['thumbnail_parameters'], - 'note' => $vars['note'], + [ + 'attachment' => $vars['attachment'], + 'note' => $vars['note'], ]); return Event::stop; } diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index 49c031e3cc..8143b18f47 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -30,6 +30,7 @@ use function App\Core\I18n\_m; use App\Core\Log; use App\Core\Router\Router; use App\Util\Common; +use App\Util\Exception\ClientException; use App\Util\Exception\DuplicateFoundException; use App\Util\Exception\NotFoundException; use App\Util\Exception\ServerException; @@ -345,6 +346,21 @@ class Attachment extends Entity return Router::url('attachment_view', ['id' => $this->getId()]); } + /** + * @param null|string $size + * @param bool $crop + * + * @throws ClientException + * @throws NotFoundException + * @throws ServerException + * + * @return AttachmentThumbnail + */ + public function getThumbnail(?string $size = null, bool $crop = false): AttachmentThumbnail + { + return AttachmentThumbnail::getOrCreate(attachment: $this, size: $size, crop: $crop); + } + public function getThumbnailUrl(?string $size = null) { return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'size' => $size ?? Common::config('thumbnail', 'default_size')]); diff --git a/src/Entity/AttachmentThumbnail.php b/src/Entity/AttachmentThumbnail.php index a4bc914a34..a5c657c0b4 100644 --- a/src/Entity/AttachmentThumbnail.php +++ b/src/Entity/AttachmentThumbnail.php @@ -65,6 +65,8 @@ class AttachmentThumbnail extends Entity private ?string $mimetype; private int $size = self::SIZE_SMALL; private string $filename; + private int $width; + private int $height; private \DateTimeInterface $modified; public function setAttachmentId(int $attachment_id): self @@ -122,6 +124,28 @@ class AttachmentThumbnail extends Entity return $this->modified; } + public function getWidth(): int + { + return $this->width; + } + + public function setWidth(int $width): self + { + $this->width = $width; + return $this; + } + + public function getHeight(): int + { + return $this->height; + } + + public function setHeight(int $height): self + { + $this->height = $height; + return $this; + } + // @codeCoverageIgnoreEnd // }}} Autocode @@ -199,6 +223,8 @@ class AttachmentThumbnail extends Entity $filename = "{$predicted_width}x{$predicted_height}{$ext}-" . $attachment->getFilehash(); $thumbnail->setFilename($filename); $thumbnail->setMimetype($mimetype); + $thumbnail->setWidth($predicted_width); + $thumbnail->setHeight($predicted_height); DB::persist($thumbnail); DB::flush(); $temp->move(Common::config('thumbnail', 'dir'), $filename); @@ -326,6 +352,8 @@ class AttachmentThumbnail extends Entity 'mimetype' => ['type' => 'varchar', 'length' => 129, 'description' => 'resource mime type 64+1+64, images hardly will show up with long mimetypes, this is probably safe considering rfc6838#section-4.2'], 'size' => ['type' => 'int', 'not null' => true, 'default' => 0, 'description' => '0 = small; 1 = medium; 2 = big'], 'filename' => ['type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'thumbnail filename'], + 'width' => ['type' => 'int', 'not null' => true, 'description' => 'width in pixels, if it can be described as such and data is available'], + 'height' => ['type' => 'int', 'not null' => true, 'description' => 'height in pixels, if it can be described as such and data is available'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], ], 'primary key' => ['attachment_id', 'size'],