[UI][Attachment] Use Attachment methods to get the proper URL, rather than crafting it in a template

This commit is contained in:
Hugo Sales 2021-08-18 14:04:17 +01:00
parent 6799052ff5
commit 4b2a92d052
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 17 additions and 11 deletions

View File

@ -143,9 +143,9 @@ class ImageEncoder extends Plugin
public function onViewAttachmentImage(array $vars, array &$res): bool public function onViewAttachmentImage(array $vars, array &$res): bool
{ {
$res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig', $res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig',
['attachment' => $vars['attachment'], [
'thumbnail_parameters' => $vars['thumbnail_parameters'], 'attachment' => $vars['attachment'],
'note' => $vars['note'], 'note' => $vars['note'],
]); ]);
return Event::stop; return Event::stop;
} }

View File

@ -1,6 +1,7 @@
<figure> <figure>
<img class="u-photo" src="{{ path('attachment_thumbnail', thumbnail_parameters) }}" <img class="u-photo"
alt="{{ attachment.getFilename() }}"> alt="{{ attachment.getBestTitle(note) }}"
src="{{ attachment.getThumbnailUrl() }}">
<figcaption><a <figcaption><a
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a> href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a>
</figcaption> </figcaption>

View File

@ -1,9 +1,9 @@
<div> <div>
<figure> <figure>
<video class="u-video" src="{{ path('attachment_view', {'id': attachment.getId()}) }}" controls poster="{{ path('attachment_thumbnail', thumbnail_parameters) }}"> <video class="u-video" src="{{ attachment.getUrl() }}" controls poster="{{ attachment.getThumbnailUrl()}}">
</video> </video>
<figcaption><a <figcaption><a
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a> href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a>
</figcaption> </figcaption>
</figure> </figure>
</div> </div>

View File

@ -21,6 +21,7 @@
namespace App\Entity; namespace App\Entity;
use App\Core\Router\Router;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\GSFile; use App\Core\GSFile;
@ -315,9 +316,14 @@ class Attachment extends Entity
return is_null($filename) ? null : Common::config('attachments', 'dir') . $filename; return is_null($filename) ? null : Common::config('attachments', 'dir') . $filename;
} }
public function getAttachmentUrl() public function getUrl()
{ {
return Router::url('attachment_thumbnail', ['id' => $this->getAttachmentId(), 'w' => Common::config('attachment', 'width'), 'h' => Common::config('attachment', 'height')]); return Router::url('attachment_view', ['id' => $this->getId()]);
}
public function getThumbnailUrl()
{
return Router::url('attachment_thumbnail', ['id' => $this->getId(), 'w' => Common::config('thumbnail', 'width'), 'h' => Common::config('thumbnail', 'height')]);;
} }
public static function schemaDef(): array public static function schemaDef(): array

View File

@ -1,6 +1,5 @@
{% set thumbnail_parameters = {'id': attachment.getId(), 'w': config('thumbnail','width'), 'h': config('thumbnail','height')} %}
{% set handled = false %} {% set handled = false %}
{% for block in handle_event('ViewAttachment' ~ attachment.getMimetypeMajor() | capitalize , {'attachment': attachment, 'thumbnail_parameters': thumbnail_parameters, 'note': note}) %} {% for block in handle_event('ViewAttachment' ~ attachment.getMimetypeMajor() | capitalize , {'attachment': attachment, 'note': note}) %}
{% set handled = true %} {% set handled = true %}
{{ block | raw }} {{ block | raw }}
{% endfor %} {% endfor %}