[CORE] Refactored attachement actions to remove duplicate code

This commit is contained in:
Miguel Dantas 2019-06-26 03:25:59 +01:00 committed by Diogo Cordeiro
parent c7475d78b4
commit 9536f2a909
4 changed files with 27 additions and 32 deletions

View File

@ -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.");
}
}
}
}

View File

@ -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);
}
}

View File

@ -3,13 +3,11 @@
if (!defined('GNUSOCIAL')) { exit(1); }
/**
* Download notice attachment
* View notice attachment
*
* @category Personal
* @package GNUsocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @author Miguel Dantas <biodantasgs@gmail.com>
* @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');
header('Content-Transfer-Encoding: binary');
$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);
}
}

View File

@ -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);