From 9a7f1358c2e98e5f8a17134e86b20db0d46c4e85 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 25 Apr 2021 21:26:53 +0000 Subject: [PATCH] [ENTITY] Add utils to Attachment and AttachmentThumbnail to get the corresponding URL and html representation parameters --- src/Entity/Attachment.php | 5 +++++ src/Entity/AttachmentThumbnail.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index 23a3a8d757..60880eb1e4 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -233,6 +233,11 @@ class Attachment extends Entity return Common::config('attachments', 'dir') . $this->getFilename(); } + public function getAttachmentUrl() + { + return Router::url('attachment_thumbnail', ['id' => $this->getAttachmentId(), 'w' => Common::config('attachment', 'width'), 'h' => Common::config('attachment', 'height')]); + } + public static function schemaDef(): array { return [ diff --git a/src/Entity/AttachmentThumbnail.php b/src/Entity/AttachmentThumbnail.php index b596636f80..140d715b0f 100644 --- a/src/Entity/AttachmentThumbnail.php +++ b/src/Entity/AttachmentThumbnail.php @@ -27,6 +27,7 @@ use App\Core\Entity; use App\Core\Event; use App\Core\GSFile; use App\Core\Log; +use App\Core\Router; use App\Util\Common; use App\Util\Exception\NotFoundException; use App\Util\Exception\ServerException; @@ -149,6 +150,24 @@ class AttachmentThumbnail extends Entity return Common::config('thumbnail', 'dir') . $this->getFilename(); } + public function getUrl() + { + return Router::url('attachment_thumbnail', ['id' => $this->getAttachmentId(), 'w' => $this->getWidth(), 'h' => $this->getHeight()]); + } + + /** + * Get the HTML attributes for this thumbnail + */ + public function getHTMLAttributes(array $orig = [], bool $overwrite = true) + { + $attrs = [ + 'height' => $this->getHeight(), + 'width' => $this->getWidth(), + 'src' => $this->getUrl(), + ]; + return $overwrite ? array_merge($orig, $attrs) : array_merge($attrs, $orig); + } + /** * Delete a attachment thumbnail. This table doesn't own all the attachments, only itself */