[CORE] Fixed bug where the http connection was using the wrong size for thumbnails, and returning the wrong one

This commit is contained in:
Miguel Dantas 2019-06-30 15:24:11 +01:00 committed by Diogo Cordeiro
parent f746866b65
commit 3e5ce46e98
6 changed files with 42 additions and 18 deletions

View File

@ -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 {

View File

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

View File

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

View File

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

View File

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

View File

@ -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()
{