[MEDIA] Simplify Attachment actions

This commit is contained in:
Diogo Cordeiro 2019-10-19 01:57:36 +01:00
parent 7298468df7
commit d058a70557
6 changed files with 135 additions and 153 deletions

View File

@ -1,33 +1,20 @@
<?php <?php
/** // This file is part of GNU social - https://www.gnu.org/software/social
* StatusNet, the distributed open-source microblogging tool //
* // GNU social is free software: you can redistribute it and/or modify
* Show notice attachments // it under the terms of the GNU Affero General Public License as published by
* // the Free Software Foundation, either version 3 of the License, or
* PHP version 5 // (at your option) any later version.
* //
* LICENCE: This program is free software: you can redistribute it and/or modify // GNU social is distributed in the hope that it will be useful,
* it under the terms of the GNU Affero General Public License as published by // but WITHOUT ANY WARRANTY; without even the implied warranty of
* the Free Software Foundation, either version 3 of the License, or // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* (at your option) any later version. // GNU Affero General Public License for more details.
* //
* This program is distributed in the hope that it will be useful, // You should have received a copy of the GNU Affero General Public License
* but WITHOUT ANY WARRANTY; without even the implied warranty of // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Personal
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2008-2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* Show notice attachments * Show notice attachments
@ -43,7 +30,13 @@ class AttachmentAction extends ManagedAction
/** /**
* Attachment File object to show * Attachment File object to show
*/ */
var $attachment = null; public $attachment = null;
public $filehash = null;
public $filepath = null;
public $filesize = null;
public $mimetype = null;
public $filename = null;
/** /**
* Load attributes based on database arguments * Load attributes based on database arguments
@ -52,41 +45,48 @@ class AttachmentAction extends ManagedAction
* *
* @param array $args $_REQUEST array * @param array $args $_REQUEST array
* *
* @return success flag * @return bool flag
* @throws ClientException
* @throws FileNotFoundException
* @throws FileNotStoredLocallyException
* @throws InvalidFilenameException
* @throws ServerException
*/ */
protected function prepare(array $args = [])
protected function prepare(array $args=array())
{ {
parent::prepare($args); parent::prepare($args);
try { try {
if (!empty($id = $this->trimmed('attachment'))) { if (!empty($id = $this->trimmed('attachment'))) {
$this->attachment = File::getByID($id); $this->attachment = File::getByID($id);
} elseif (!empty($filehash = $this->trimmed('filehash'))) { } elseif (!empty($this->filehash = $this->trimmed('filehash'))) {
$this->attachment = File::getByHash($filehash); $this->attachment = File::getByHash($this->filehash);
} }
} catch (Exception $e) { } catch (Exception $e) {
// Not found // Not found
} }
if (!$this->attachment instanceof File) { if (!$this->attachment instanceof File) {
// TRANS: Client error displayed trying to get a non-existing attachment. // TRANS: Client error displayed trying to get a non-existing attachment.
$this->clientError(_('No such attachment.'), 404); $this->clientError(_m('No such attachment.'), 404);
} }
$filename = $this->attachment->getFileOrThumbnailPath(); $this->filepath = $this->attachment->getFileOrThumbnailPath();
if (empty($this->filepath)) {
if (empty($filename)) { $this->clientError(_m('Requested local URL for a file that is not stored locally.'), 404);
$this->clientError(_('Requested local URL for a file that is not stored locally.'), 404);
} }
$this->filesize = $this->attachment->getFileOrThumbnailSize();
$this->mimetype = $this->attachment->getFileOrThumbnailMimetype();
$this->filename = MediaFile::getDisplayName($this->attachment);
return true; return true;
} }
/** /**
* Is this action read-only? * Is this action read-only?
* *
* @return boolean true * @return bool true
*/ */
function isReadOnly($args) public function isReadOnly($args): bool
{ {
return true; return true;
} }
@ -96,15 +96,15 @@ class AttachmentAction extends ManagedAction
* *
* @return string title of the page * @return string title of the page
*/ */
function title() public function title(): string
{ {
$a = new Attachment($this->attachment); $a = new Attachment($this->attachment);
return $a->title(); return $a->title();
} }
public function showPage() public function showPage(): void
{ {
if (empty($this->attachment->getFileOrThumbnailPath())) { if (empty($this->filepath)) {
// if it's not a local file, gtfo // if it's not a local file, gtfo
common_redirect($this->attachment->getUrl(), 303); common_redirect($this->attachment->getUrl(), 303);
} }
@ -119,10 +119,10 @@ class AttachmentAction extends ManagedAction
* *
* @return void * @return void
*/ */
function showContent() public function showContent(): void
{ {
$ali = new Attachment($this->attachment, $this); $ali = new Attachment($this->attachment, $this);
$cnt = $ali->show(); $ali->show();
} }
/** /**
@ -130,7 +130,7 @@ class AttachmentAction extends ManagedAction
* *
* @return void * @return void
*/ */
function showPageNoticeBlock() public function showPageNoticeBlock(): void
{ {
} }
@ -139,7 +139,8 @@ class AttachmentAction extends ManagedAction
* *
* @return void * @return void
*/ */
function showSections() { public function showSections(): void
{
$ns = new AttachmentNoticeSection($this); $ns = new AttachmentNoticeSection($this);
$ns->show(); $ns->show();
} }
@ -148,13 +149,14 @@ class AttachmentAction extends ManagedAction
* Last-modified date for file * Last-modified date for file
* *
* @return int last-modified date as unix timestamp * @return int last-modified date as unix timestamp
* @throws ServerException
*/ */
public function lastModified() public function lastModified(): ?int
{ {
if (common_config('site', 'use_x_sendfile')) { if (common_config('site', 'use_x_sendfile')) {
return null; return null;
} }
$path = $this->attachment->getFileOrThumbnailPath(); $path = $this->filepath;
if (!empty($path)) { if (!empty($path)) {
return filemtime($path); return filemtime($path);
} else { } else {
@ -169,26 +171,26 @@ class AttachmentAction extends ManagedAction
* but in decimal instead of hex. * but in decimal instead of hex.
* *
* @return string etag http header * @return string etag http header
* @throws ServerException
*/ */
function etag() public function etag(): string
{ {
if (common_config('site', 'use_x_sendfile')) { if (common_config('site', 'use_x_sendfile')) {
return null; return null;
} }
$path = $this->attachment->getFileOrThumbnailPath(); $path = $this->filepath;
$cache = Cache::instance(); $cache = Cache::instance();
if($cache) { if ($cache) {
if (empty($path)) { if (empty($path)) {
return null; return null;
} }
$key = Cache::key('attachments:etag:' . $path); $key = Cache::key('attachments:etag:' . $path);
$etag = $cache->get($key); $etag = $cache->get($key);
if($etag === false) { if ($etag === false) {
$etag = crc32(file_get_contents($path)); $etag = crc32(file_get_contents($path));
$cache->set($key,$etag); $cache->set($key, $etag);
} }
return $etag; return $etag;
} }
@ -205,30 +207,30 @@ class AttachmentAction extends ManagedAction
* Include $filepath in the response, for viewing and downloading. * Include $filepath in the response, for viewing and downloading.
* If provided, $filesize is used to size the HTTP request, * If provided, $filesize is used to size the HTTP request,
* otherwise it's value is calculated * otherwise it's value is calculated
* @param string $filepath the absolute path to the file to send * @throws ServerException
* @param $filesize optional, calculated if unkown
*/ */
static function sendFile(string $filepath, $filesize) { public function sendFile(): void
{
if (is_string(common_config('site', 'x-static-delivery'))) { if (is_string(common_config('site', 'x-static-delivery'))) {
$tmp = explode(INSTALLDIR, $filepath); $tmp = explode(INSTALLDIR, $this->filepath);
$relative_path = end($tmp); $relative_path = end($tmp);
common_debug("Using Static Delivery with header: '" . common_debug("Using Static Delivery with header: '" .
common_config('site', 'x-static-delivery') . ": {$relative_path}'"); common_config('site', 'x-static-delivery') . ": {$relative_path}'");
header(common_config('site', 'x-static-delivery') . ": {$relative_path}"); header(common_config('site', 'x-static-delivery') . ": {$relative_path}");
} else { } else {
if (empty($filesize)) { if (empty($this->filesize)) {
$filesize = filesize($filepath); $this->filesize = filesize($this->filepath);
} }
header("Content-Length: {$filesize}"); header("Content-Length: {$this->filesize}");
// header('Cache-Control: private, no-transform, no-store, must-revalidate'); // header('Cache-Control: private, no-transform, no-store, must-revalidate');
$ret = @readfile($filepath); $ret = @readfile($this->filepath);
if ($ret === false) { if ($ret === false) {
common_log(LOG_ERR, "Couldn't read file at {$filepath}."); common_log(LOG_ERR, "Couldn't read file at {$this->filepath}.");
} elseif ($ret !== $filesize) { } elseif ($ret !== $this->filesize) {
common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " . common_log(LOG_ERR, "The lengths of the file as recorded on the DB (or on disk) for the file " .
"{$filepath} differ from what was sent to the user ({$filesize} vs {$ret})."); "{$this->filepath} differ from what was sent to the user ({$this->filesize} vs {$ret}).");
} }
} }
} }

View File

@ -13,30 +13,19 @@ if (!defined('GNUSOCIAL')) { exit(1); }
*/ */
class Attachment_downloadAction extends AttachmentAction class Attachment_downloadAction extends AttachmentAction
{ {
public function showPage() public function showPage(): void
{ {
// Checks file exists or throws FileNotFoundException
$filepath = $this->attachment->getFileOrThumbnailPath();
$filesize = $this->attachment->getFileOrThumbnailSize();
$mimetype = $this->attachment->getFileOrThumbnailMimetype();
if (empty($filepath)) {
$this->clientError(_('No such attachment'), 404);
}
$filename = MediaFile::getDisplayName($this->attachment);
// Disable errors, to not mess with the file contents (suppress errors in case access to this // Disable errors, to not mess with the file contents (suppress errors in case access to this
// function is blocked, like in some shared hosts). Automatically reset at the end of the // function is blocked, like in some shared hosts). Automatically reset at the end of the
// script execution, and we don't want to have any more errors until then, so don't reset it // script execution, and we don't want to have any more errors until then, so don't reset it
@ini_set('display_errors', 0); @ini_set('display_errors', 0);
header("Content-Description: File Transfer"); header("Content-Description: File Transfer");
header("Content-Type: {$mimetype}"); header("Content-Type: {$this->mimetype}");
header("Content-Disposition: attachment; filename=\"{$filename}\""); header("Content-Disposition: attachment; filename=\"{$this->filename}\"");
header('Expires: 0'); header('Expires: 0');
header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different? header('Content-Transfer-Encoding: binary'); // FIXME? Can this be different?
AttachmentAction::sendFile($filepath, $filesize); parent::sendFile();
} }
} }

View File

@ -1,33 +1,20 @@
<?php <?php
/** // This file is part of GNU social - https://www.gnu.org/software/social
* StatusNet, the distributed open-source microblogging tool //
* // GNU social is free software: you can redistribute it and/or modify
* Show notice attachments // it under the terms of the GNU Affero General Public License as published by
* // the Free Software Foundation, either version 3 of the License, or
* PHP version 5 // (at your option) any later version.
* //
* LICENCE: This program is free software: you can redistribute it and/or modify // GNU social is distributed in the hope that it will be useful,
* it under the terms of the GNU Affero General Public License as published by // but WITHOUT ANY WARRANTY; without even the implied warranty of
* the Free Software Foundation, either version 3 of the License, or // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* (at your option) any later version. // GNU Affero General Public License for more details.
* //
* This program is distributed in the hope that it will be useful, // You should have received a copy of the GNU Affero General Public License
* but WITHOUT ANY WARRANTY; without even the implied warranty of // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Personal
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2008-2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* Show notice attachments * Show notice attachments
@ -58,10 +45,12 @@ class Attachment_thumbnailAction extends AttachmentAction
* requested in the GET variables (read in the constructor). Tries * requested in the GET variables (read in the constructor). Tries
* to send the most appropriate file with the correct size and * to send the most appropriate file with the correct size and
* headers or displays an error if it's not possible. * headers or displays an error if it's not possible.
* @throws ClientException
* @throws ReflectionException
* @throws ServerException
*/ */
public function showPage() public function showPage(): void
{ {
// Returns a File_thumbnail object or throws exception if not available // Returns a File_thumbnail object or throws exception if not available
try { try {
$thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c); $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c);
@ -69,27 +58,21 @@ class Attachment_thumbnailAction extends AttachmentAction
} catch (UseFileAsThumbnailException $e) { } catch (UseFileAsThumbnailException $e) {
// With this exception, the file exists locally // With this exception, the file exists locally
$file = $e->file; $file = $e->file;
} catch(FileNotFoundException $e) { } catch (FileNotFoundException $e) {
$this->clientError(_('No such attachment'), 404); $this->clientError(_m('No such attachment'), 404);
} }
// 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 // Disable errors, to not mess with the file contents (suppress errors in case access to this
// function is blocked, like in some shared hosts). Automatically reset at the end of the // function is blocked, like in some shared hosts). Automatically reset at the end of the
// script execution, and we don't want to have any more errors until then, so don't reset it // script execution, and we don't want to have any more errors until then, so don't reset it
@ini_set('display_errors', 0); @ini_set('display_errors', 0);
header("Content-Description: File Transfer"); header("Content-Description: File Transfer");
header("Content-Type: {$mimetype}"); header("Content-Type: {$this->mimetype}");
header("Content-Disposition: inline; filename=\"{$filename}\""); header("Content-Disposition: inline; filename=\"{$this->filename}\"");
header('Expires: 0'); header('Expires: 0');
header('Content-Transfer-Encoding: binary'); header('Content-Transfer-Encoding: binary');
AttachmentAction::sendFile($filepath, $filesize); parent::sendFile();
} }
} }

View File

@ -1,6 +1,20 @@
<?php <?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
if (!defined('GNUSOCIAL')) { exit(1); } defined('GNUSOCIAL') || die();
/** /**
* View notice attachment * View notice attachment
@ -11,34 +25,23 @@ if (!defined('GNUSOCIAL')) { exit(1); }
*/ */
class Attachment_viewAction extends AttachmentAction class Attachment_viewAction extends AttachmentAction
{ {
public function showPage() public function showPage(): void
{ {
// Checks file exists or throws FileNotFoundException
$filepath = $this->attachment->getFileOrThumbnailPath();
$filesize = $this->attachment->getFileOrThumbnailSize();
$mimetype = $this->attachment->getFileOrThumbnailMimetype();
if (empty($filepath)) {
$this->clientError(_('No such attachment'), 404);
}
$filename = MediaFile::getDisplayName($this->attachment);
// Disable errors, to not mess with the file contents (suppress errors in case access to this // Disable errors, to not mess with the file contents (suppress errors in case access to this
// function is blocked, like in some shared hosts). Automatically reset at the end of the // function is blocked, like in some shared hosts). Automatically reset at the end of the
// script execution, and we don't want to have any more errors until then, so don't reset it // script execution, and we don't want to have any more errors until then, so don't reset it
@ini_set('display_errors', 0); @ini_set('display_errors', 0);
header("Content-Description: File Transfer"); header("Content-Description: File Transfer");
header("Content-Type: {$mimetype}"); header("Content-Type: {$this->mimetype}");
if (in_array(common_get_mime_media($mimetype), ['image', 'video'])) { if (in_array(common_get_mime_media($this->mimetype), ['image', 'video'])) {
header("Content-Disposition: inline; filename=\"{$filename}\""); header("Content-Disposition: inline; filename=\"{$this->filename}\"");
} else { } else {
header("Content-Disposition: attachment; filename=\"{$filename}\""); header("Content-Disposition: attachment; filename=\"{$this->filename}\"");
} }
header('Expires: 0'); header('Expires: 0');
header('Content-Transfer-Encoding: binary'); header('Content-Transfer-Encoding: binary');
AttachmentAction::sendFile($filepath, $filesize); parent::sendFile();
} }
} }

View File

@ -564,7 +564,7 @@ class File extends Managed_DataObject
* @throws UnsupportedMediaException if, despite trying, we can't understand how to make a thumbnail for this format * @throws UnsupportedMediaException if, despite trying, we can't understand how to make a thumbnail for this format
* @throws ServerException on various other errors * @throws ServerException on various other errors
*/ */
public function getThumbnail($width=null, $height=null, $crop=false, $force_still=true, $upscale=null): File_thumbnail public function getThumbnail($width = null, $height = null, $crop = false, $force_still = true, $upscale = null): File_thumbnail
{ {
// Get some more information about this file through our ImageFile class // Get some more information about this file through our ImageFile class
$image = ImageFile::fromFileObject($this); $image = ImageFile::fromFileObject($this);
@ -610,7 +610,7 @@ class File extends Managed_DataObject
* @throws InvalidFilenameException * @throws InvalidFilenameException
* @throws ServerException * @throws ServerException
*/ */
public function getFileOrThumbnailPath(?File_thumbnail $thumbnail = null) : string public function getFileOrThumbnailPath(?File_thumbnail $thumbnail = null): string
{ {
if (!empty($thumbnail)) { if (!empty($thumbnail)) {
return $thumbnail->getPath(); return $thumbnail->getPath();
@ -803,7 +803,7 @@ class File extends Managed_DataObject
public function noticeCount() public function noticeCount()
{ {
$cacheKey = sprintf('file:notice-count:%d', $this->id); $cacheKey = sprintf('file:notice-count:%d', $this->id);
$count = self::cacheGet($cacheKey); $count = self::cacheGet($cacheKey);
if ($count === false) { if ($count === false) {

View File

@ -28,34 +28,39 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } if (!defined('GNUSOCIAL')) {
exit(1);
}
/** /**
* used for one-off attachment action * used for one-off attachment action
*/ */
class Attachment extends AttachmentListItem class Attachment extends AttachmentListItem
{ {
function showNoticeAttachment() { public function showNoticeAttachment()
if (Event::handle('StartShowAttachmentLink', array($this->out, $this->attachment))) { {
$this->out->elementStart('div', array('id' => 'attachment_view', if (Event::handle('StartShowAttachmentLink', [$this->out, $this->attachment])) {
'class' => 'h-entry')); $this->out->elementStart('div', ['id' => 'attachment_view',
'class' => 'h-entry']);
$this->out->elementStart('div', 'entry-title'); $this->out->elementStart('div', 'entry-title');
$this->out->element('a', $this->linkAttr(), _('Download link')); $this->out->element('a', $this->linkAttr(), _m('Download link'));
$this->out->elementEnd('div'); $this->out->elementEnd('div');
$this->out->elementStart('article', 'e-content'); $this->out->elementStart('article', 'e-content');
$this->showRepresentation(); $this->showRepresentation();
$this->out->elementEnd('article'); $this->out->elementEnd('article');
Event::handle('EndShowAttachmentLink', array($this->out, $this->attachment)); Event::handle('EndShowAttachmentLink', [$this->out, $this->attachment]);
$this->out->elementEnd('div'); $this->out->elementEnd('div');
} }
} }
function show() { public function show()
{
$this->showNoticeAttachment(); $this->showNoticeAttachment();
} }
function linkAttr() { public function linkAttr()
{
return array('rel' => 'external', 'href' => $this->attachment->getAttachmentDownloadUrl()); return array('rel' => 'external', 'href' => $this->attachment->getAttachmentDownloadUrl());
} }
} }