From 9536f2a90958e146354128a13fa82c11ac925869 Mon Sep 17 00:00:00 2001 From: Miguel Dantas Date: Wed, 26 Jun 2019 03:25:59 +0100 Subject: [PATCH] [CORE] Refactored attachement actions to remove duplicate code --- actions/attachment.php | 21 +++++++++++++++++++++ actions/attachment_download.php | 14 +------------- actions/attachment_view.php | 22 ++++------------------ lib/framework.php | 2 +- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/actions/attachment.php b/actions/attachment.php index fb85a50d07..0e5c69d1a2 100644 --- a/actions/attachment.php +++ b/actions/attachment.php @@ -132,4 +132,25 @@ class AttachmentAction extends ManagedAction $ns = new AttachmentNoticeSection($this); $ns->show(); } + + public function sendFile(string $filepath) { + if (common_config('site', 'use_x_sendfile')) { + header('X-Sendfile: ' . $filepath); + } else { + $filesize = $this->attachment->size; + // 'if available', it says, so ensure we have it + if (empty($filesize)) { + $filesize = filesize($filepath); + } + header("Content-Length: {$filesize}"); + // header('Cache-Control: private, no-transform, no-store, must-revalidate'); + + $ret = @readfile($filepath); + + if ($ret === false || $ret !== $filesize) { + common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " . + "{$filepath}, with id={$this->attachment->id} differ from what was sent to the user."); + } + } + } } diff --git a/actions/attachment_download.php b/actions/attachment_download.php index e9a1667a5a..35c0ecb5d5 100644 --- a/actions/attachment_download.php +++ b/actions/attachment_download.php @@ -30,19 +30,7 @@ class Attachment_downloadAction extends AttachmentAction header("Content-Disposition: attachment; filename=\"{$filename}\""); header('Expires: 0'); header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different? - $filesize = $this->attachment->size; - // 'if available', it says, so ensure we have it - if (empty($filesize)) { - $filesize = filesize($this->attachment->filename); - } - header("Content-Length: {$filesize}"); - // header('Cache-Control: private, no-transform, no-store, must-revalidate'); - $ret = @readfile($filepath); - - if ($ret === false || $ret !== $filesize) { - common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " . - "{$filepath}, with id={$this->attachment->id} differ from what was sent to the user."); - } + $this->sendFile($filepath); } } diff --git a/actions/attachment_view.php b/actions/attachment_view.php index 6a3b7df9f5..d6a7c1c50d 100644 --- a/actions/attachment_view.php +++ b/actions/attachment_view.php @@ -3,13 +3,11 @@ if (!defined('GNUSOCIAL')) { exit(1); } /** - * Download notice attachment + * View notice attachment * - * @category Personal * @package GNUsocial - * @author Mikael Nordfeldth + * @author Miguel Dantas * @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 - * @link https:/gnu.io/social */ class Attachment_viewAction extends AttachmentAction { @@ -33,20 +31,8 @@ class Attachment_viewAction extends AttachmentAction header("Content-Disposition: attachment; filename=\"{$filename}\""); } header('Expires: 0'); - header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different? - $filesize = $this->attachment->size; - // 'if available', it says, so ensure we have it - if (empty($filesize)) { - $filesize = filesize($this->attachment->filename); - } - header("Content-Length: {$filesize}"); - // header('Cache-Control: private, no-transform, no-store, must-revalidate'); - - $ret = @readfile($filepath); + header('Content-Transfer-Encoding: binary'); - if ($ret === false || $ret !== $filesize) { - common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " . - "{$filepath}, with id={$this->attachment->id} differ from what was sent to the user."); - } + $this->sendFile($filepath); } } diff --git a/lib/framework.php b/lib/framework.php index 1bbdaca3f1..24ec1f7cd1 100644 --- a/lib/framework.php +++ b/lib/framework.php @@ -32,7 +32,7 @@ defined('GNUSOCIAL') || die(); define('GNUSOCIAL_ENGINE', 'GNU social'); define('GNUSOCIAL_ENGINE_URL', 'https://www.gnu.org/software/social/'); -define('GNUSOCIAL_BASE_VERSION', '1.24.0'); +define('GNUSOCIAL_BASE_VERSION', '1.24.1'); define('GNUSOCIAL_LIFECYCLE', 'dev'); // 'dev', 'alpha[0-9]+', 'beta[0-9]+', 'rc[0-9]+', 'release' define('GNUSOCIAL_VERSION', GNUSOCIAL_BASE_VERSION . '-' . GNUSOCIAL_LIFECYCLE); -- 2.39.5