From 2adb3c35214824fd849ba6ddc0fb323e5d4bdb24 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 28 Apr 2021 15:01:40 +0000 Subject: [PATCH] [ATTACHMENTS] Add event 'AttachmentFileInfo' to allow a plugin to override the file displayed --- src/Controller/Attachment.php | 17 +++++++++++++---- src/Core/GSFile.php | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Controller/Attachment.php b/src/Controller/Attachment.php index cbf9defa36..3ace61237d 100644 --- a/src/Controller/Attachment.php +++ b/src/Controller/Attachment.php @@ -23,9 +23,11 @@ namespace App\Controller; use App\Core\Controller; use App\Core\DB\DB; +use App\Core\Event; use App\Core\GSFile; use App\Entity\AttachmentThumbnail; use App\Util\Common; +use App\Util\Exception\ClientException; use App\Util\Exception\NotFoundException; use App\Util\Exception\ServerException; use Symfony\Component\HttpFoundation\HeaderUtils; @@ -36,20 +38,27 @@ class Attachment extends Controller { public function attachment_show(Request $request, int $id) { - $res = GSFile::getAttachmentFileInfo($id); - return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); + // If no one else claims this attachment, use the default representation + if (Event::handle('AttachmentFileInfo', [$id, &$res]) != Event::stop) { + $res = GSFile::getAttachmentFileInfo($id); + } + if (!empty($res)) { + return GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); + } else { + throw new ClientException('No such attachment', 404); + } } public function attachment_view(Request $request, int $id) { $res = GSFile::getAttachmentFileInfo($id); - return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); + return GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); } public function attachment_download(Request $request, int $id) { $res = GSFile::getAttachmentFileInfo($id); - return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_ATTACHMENT); + return GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_ATTACHMENT); } /** diff --git a/src/Core/GSFile.php b/src/Core/GSFile.php index e4164b5fc9..55dc5d9c61 100644 --- a/src/Core/GSFile.php +++ b/src/Core/GSFile.php @@ -161,8 +161,8 @@ class GSFile */ public static function getAttachmentFileInfo(int $id): array { - $res = self::getFileInfo($id); - $res['file_path'] = Common::config('attachments', 'dir') . $res['file_hash']; + $res = self::getFileInfo($id); + $res['filepath'] = Common::config('attachments', 'dir') . $res['file_hash']; return $res; }