. */ namespace App\Twig; use App\Entity\Attachments\Attachment; use App\Services\Attachments\AttachmentURLGenerator; use App\Services\Misc\FAIconGenerator; use Twig\Attribute\AsTwigFunction; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; final readonly class AttachmentExtension { public function __construct(private AttachmentURLGenerator $attachmentURLGenerator, private FAIconGenerator $FAIconGenerator) { } /** * Returns the URL of the thumbnail of the given attachment. Returns null if no thumbnail is available. */ #[AsTwigFunction("attachment_thumbnail")] public function attachmentThumbnail(Attachment $attachment, string $filter_name = 'thumbnail_sm'): ?string { return $this->attachmentURLGenerator->getThumbnailURL($attachment, $filter_name); } /** * Return the font-awesome icon type for the given file extension. Returns "file" if no specific icon is available. * Null is allowed for files withot extension * @param string|null $extension * @return string */ #[AsTwigFunction("ext_to_fa_icon")] public function extToFAIcon(?string $extension): string { return $this->FAIconGenerator->fileExtensionToFAType($extension ?? ''); } }