[ATTACHMENTS] Add event 'AttachmentFileInfo' to allow a plugin to override the file displayed

This commit is contained in:
Hugo Sales 2021-04-28 15:01:40 +00:00
parent e08767cec0
commit 6da8cf7f14
2 changed files with 15 additions and 6 deletions

View File

@ -23,9 +23,11 @@ namespace App\Controller;
use App\Core\Controller; use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event;
use App\Core\GSFile; use App\Core\GSFile;
use App\Entity\AttachmentThumbnail; use App\Entity\AttachmentThumbnail;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\HeaderUtils;
@ -36,20 +38,27 @@ class Attachment extends Controller
{ {
public function attachment_show(Request $request, int $id) public function attachment_show(Request $request, int $id)
{ {
$res = GSFile::getAttachmentFileInfo($id); // If no one else claims this attachment, use the default representation
return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); 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) public function attachment_view(Request $request, int $id)
{ {
$res = GSFile::getAttachmentFileInfo($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) public function attachment_download(Request $request, int $id)
{ {
$res = GSFile::getAttachmentFileInfo($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);
} }
/** /**

View File

@ -161,8 +161,8 @@ class GSFile
*/ */
public static function getAttachmentFileInfo(int $id): array public static function getAttachmentFileInfo(int $id): array
{ {
$res = self::getFileInfo($id); $res = self::getFileInfo($id);
$res['file_path'] = Common::config('attachments', 'dir') . $res['file_hash']; $res['filepath'] = Common::config('attachments', 'dir') . $res['file_hash'];
return $res; return $res;
} }