From 3e5ce46e98101e8f615dd37a0176f1b1bd47f023 Mon Sep 17 00:00:00 2001 From: Miguel Dantas Date: Sun, 30 Jun 2019 15:24:11 +0100 Subject: [PATCH] [CORE] Fixed bug where the http connection was using the wrong size for thumbnails, and returning the wrong one --- actions/attachment.php | 2 +- actions/attachment_download.php | 2 +- actions/attachment_thumbnail.php | 8 +++--- actions/attachment_view.php | 2 +- classes/File.php | 42 +++++++++++++++++++++++++------- classes/File_thumbnail.php | 4 +-- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/actions/attachment.php b/actions/attachment.php index 0851f1ee4e..a7e1c5d161 100644 --- a/actions/attachment.php +++ b/actions/attachment.php @@ -204,7 +204,7 @@ class AttachmentAction extends ManagedAction /** * Include $filepath in the response, for viewing and downloading */ - static function sendFile(string $filepath, int $filesize) { + static function sendFile(string $filepath, $filesize) { if (common_config('site', 'use_x_sendfile')) { header('X-Sendfile: ' . $filepath); } else { diff --git a/actions/attachment_download.php b/actions/attachment_download.php index ebd5e5d956..38ff046943 100644 --- a/actions/attachment_download.php +++ b/actions/attachment_download.php @@ -17,7 +17,7 @@ class Attachment_downloadAction extends AttachmentAction { // Checks file exists or throws FileNotFoundException $filepath = $this->attachment->getFileOrThumbnailPath(); - $filesize = $this->attachment->size ?: 0; + $filesize = $this->attachment->getFileOrThumbnailSize(); $mimetype = $this->attachment->getFileOrThumbnailMimetype(); if (empty($filepath)) { diff --git a/actions/attachment_thumbnail.php b/actions/attachment_thumbnail.php index e54e999983..8d4eb6f884 100644 --- a/actions/attachment_thumbnail.php +++ b/actions/attachment_thumbnail.php @@ -55,8 +55,6 @@ class Attachment_thumbnailAction extends AttachmentAction public function showPage() { - // Checks file exists or throws FileNotFoundException - $size = $this->attachment->size ?: 0; // Returns a File_thumbnail object or throws exception if not available try { @@ -69,8 +67,10 @@ class Attachment_thumbnailAction extends AttachmentAction $this->clientError(_('No such attachment'), 404); } - $filepath = $file->getFileOrThumbnailPath(); - $mimetype = $file->getFileOrThumbnailMimetype(); + // Checks file exists or throws FileNotFoundException + $filepath = $file->getFileOrThumbnailPath($thumbnail); + $filesize = $this->attachment->getFileOrThumbnailSize($thumbnail); + $mimetype = $file->getFileOrThumbnailMimetype($thumbnail); $filename = MediaFile::getDisplayName($file); // Disable errors, to not mess with the file contents (suppress errors in case access to this diff --git a/actions/attachment_view.php b/actions/attachment_view.php index 61aba9bd0f..e5f1c94da3 100644 --- a/actions/attachment_view.php +++ b/actions/attachment_view.php @@ -15,7 +15,7 @@ class Attachment_viewAction extends AttachmentAction { // Checks file exists or throws FileNotFoundException $filepath = $this->attachment->getFileOrThumbnailPath(); - $filesize = $this->attachment->size ?: 0; + $filesize = $this->attachment->getFileOrThumbnailSize(); $mimetype = $this->attachment->getFileOrThumbnailMimetype(); if (empty($filepath)) { diff --git a/classes/File.php b/classes/File.php index 113690b4b9..564bdb0055 100644 --- a/classes/File.php +++ b/classes/File.php @@ -603,8 +603,11 @@ class File extends Managed_DataObject * @throws InvalidFilenameException * @throws ServerException */ - public function getFileOrThumbnailPath() : string + public function getFileOrThumbnailPath($thumbnail = null) : string { + if (!empty($thumbnail)) { + return $thumbnail->getPath(); + } if (!empty($this->filename)) { $filepath = self::path($this->filename); if (file_exists($filepath)) { @@ -630,19 +633,40 @@ class File extends Managed_DataObject * @throws ServerException * @throws UnsupportedMediaException */ - public function getFileOrThumbnailMimetype() : string + public function getFileOrThumbnailMimetype($thumbnail = null) : string { - if (empty($this->filename)) { + if (!empty($thumbnail)) { + $filepath = $thumbnail->getPath(); + } elseif (empty($this->filename)) { $filepath = File_thumbnail::byFile($this)->getPath(); - $info = @getimagesize($filepath); - if ($info !== false) { - return $info['mime']; - } else { - throw new UnsupportedMediaException(_("Thumbnail is not an image.")); - } } else { return $this->mimetype; } + + $info = @getimagesize($filepath); + if ($info !== false) { + return $info['mime']; + } else { + throw new UnsupportedMediaException(_("Thumbnail is not an image.")); + } + } + + /** + * Return the size of the thumbnail if we have it, or, if not, of the File + * @return int + * @throws FileNotFoundException + * @throws NoResultException + * @throws ServerException + */ + public function getFileOrThumbnailSize($thumbnail = null) : int + { + if (!empty($thumbnail)) { + return filesize($thumbnail->getPath()); + } elseif (!empty($this->filename)) { + return $this->size; + } else { + return filesize(File_thumbnail::byFile($this)->getPath()); + } } public function getAttachmentUrl() diff --git a/classes/File_thumbnail.php b/classes/File_thumbnail.php index 68cb2a737f..5e751c3c4d 100644 --- a/classes/File_thumbnail.php +++ b/classes/File_thumbnail.php @@ -172,9 +172,9 @@ class File_thumbnail extends Managed_DataObject } /** - * * @return string full filesystem path to the locally stored thumbnail file - * @throws + * @throws FileNotFoundException + * @throws ServerException */ public function getPath() {