From 968e3431e10d9ff7ca74ab93d1df3124372d9218 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 12 Aug 2021 04:58:34 +0100 Subject: [PATCH] [Attachment] Sometimes we can't provide download of original file --- src/Controller/Attachment.php | 14 ++++++++++++-- src/Core/GSFile.php | 8 +++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Controller/Attachment.php b/src/Controller/Attachment.php index 31261ac45b..60daa725dd 100644 --- a/src/Controller/Attachment.php +++ b/src/Controller/Attachment.php @@ -90,12 +90,22 @@ class Attachment extends Controller */ public function attachment_view(Request $request, int $id) { - return $this->attachment($id, fn (array $res) => GSFile::sendFile($res['filepath'], $res['mimetype'], GSFile::ensureFilenameWithProperExtension($res['filename'], $res['mimetype']) ?? $res['filename'], HeaderUtils::DISPOSITION_INLINE)); + return $this->attachment($id, function (array $res) { + if (!array_key_exists('filepath', $res)) { + throw new ServerException('This attachment is not stored locally.'); + } + return GSFile::sendFile($res['filepath'], $res['mimetype'], GSFile::ensureFilenameWithProperExtension($res['filename'], $res['mimetype']) ?? $res['filename'], HeaderUtils::DISPOSITION_INLINE); + }); } public function attachment_download(Request $request, int $id) { - return $this->attachment($id, fn (array $res) => GSFile::sendFile($res['filepath'], $res['mimetype'], GSFile::ensureFilenameWithProperExtension($res['filename'], $res['mimetype']) ?? $res['filename'], HeaderUtils::DISPOSITION_ATTACHMENT)); + return $this->attachment($id, function (array $res) { + if (!array_key_exists('filepath', $res)) { + throw new ServerException('This attachment is not stored locally.'); + } + return GSFile::sendFile($res['filepath'], $res['mimetype'], GSFile::ensureFilenameWithProperExtension($res['filename'], $res['mimetype']) ?? $res['filename'], HeaderUtils::DISPOSITION_ATTACHMENT); + }); } /** diff --git a/src/Core/GSFile.php b/src/Core/GSFile.php index 85648cb8c3..9ae0d1f57c 100644 --- a/src/Core/GSFile.php +++ b/src/Core/GSFile.php @@ -133,7 +133,7 @@ class GSFile } return $response; } else { - throw new ServerException(_m('This attachment is not stored locally')); + throw new ServerException(_m('This attachment is not stored locally.')); } } @@ -186,8 +186,10 @@ class GSFile */ public static function getAttachmentFileInfo(int $id): array { - $res = self::getFileInfo($id); - $res['filepath'] = Common::config('attachments', 'dir') . $res['filename']; + $res = self::getFileInfo($id); + if (!is_null($res['filename'])) { + $res['filepath'] = Common::config('attachments', 'dir') . $res['filename']; + } return $res; }