From 5a3d74d9a8424cbc79b6f1b38749568cea958e9c Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Tue, 17 Jun 2014 11:54:05 +0200 Subject: [PATCH] UseFileAsThumbnailException (helps support GIFs) --- actions/attachment_thumbnail.php | 7 ++++- lib/activityobject.php | 2 ++ lib/attachmentlistitem.php | 2 ++ lib/imagefile.php | 4 +++ lib/usefileasthumbnailexception.php | 46 +++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/usefileasthumbnailexception.php diff --git a/actions/attachment_thumbnail.php b/actions/attachment_thumbnail.php index b564dee50e..b71ea6c801 100644 --- a/actions/attachment_thumbnail.php +++ b/actions/attachment_thumbnail.php @@ -58,7 +58,12 @@ class Attachment_thumbnailAction extends AttachmentAction function showPage() { // Returns a File_thumbnail object or throws exception if not available - $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c); + try { + $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c); + } catch (UseFileAsThumbnailException $e) { + // Since we're only using the ->getUrl() function, we can use the File object + $thumbnail = $e->file; + } common_redirect($thumbnail->getUrl()); } } diff --git a/lib/activityobject.php b/lib/activityobject.php index fc47485c18..505a16e755 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -578,6 +578,8 @@ class ActivityObject try { $thumbnail = $file->getThumbnail(); $object->thumbnail = $thumbnail; + } catch (UseFileAsThumbnailException $e) { + $object->thumbnail = null; } catch (UnsupportedMediaException $e) { $object->thumbnail = null; } diff --git a/lib/attachmentlistitem.php b/lib/attachmentlistitem.php index 1af89ef7b7..320218b1a3 100644 --- a/lib/attachmentlistitem.php +++ b/lib/attachmentlistitem.php @@ -115,6 +115,8 @@ class AttachmentListItem extends Widget try { $thumb = $this->attachment->getThumbnail(); $this->out->element('img', array('src' => $thumb->getUrl(), 'alt' => '')); + } catch (UseFileAsThumbnailException $e) { + $this->out->element('img', array('src' => $e->file->getUrl(), 'alt' => $e->file->title)); } catch (UnsupportedMediaException $e) { // FIXME: Show a good representation of unsupported/unshowable images } diff --git a/lib/imagefile.php b/lib/imagefile.php index 535afaacb6..c051fc3ac9 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -264,6 +264,10 @@ class ImageFile $this->resizeToFile($outpath, $box); } + if (!file_exists($outpath)) { + throw new UseFileAsThumbnailException($this->id); + } + return $outpath; } diff --git a/lib/usefileasthumbnailexception.php b/lib/usefileasthumbnailexception.php new file mode 100644 index 0000000000..cafbe692b2 --- /dev/null +++ b/lib/usefileasthumbnailexception.php @@ -0,0 +1,46 @@ +. + * + * @category Exception + * @package GNUsocial + * @author Mikael Nordfeldth + * @copyright 2014 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link https://www.gnu.org/software/social/ + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +class UseFileAsThumbnailException extends UnsupportedMediaException +{ + public $file = null; + + public function __construct($file_id) + { + $this->file = File::getKV('id', $file_id); + if (!$this->file instanceof File) { + throw new ServerException('No File ID supplied to exception'); + } + + parent::__construct('Thumbnail not generated', $this->file->getPath()); + } +}