From 37ce1f4766c43b5ec708923dc8923e83123c1428 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Mon, 28 Apr 2014 12:12:06 +0200 Subject: [PATCH] Better fallback on UnsupportedMediaException --- actions/oembed.php | 7 +++++-- classes/File.php | 15 ++++++--------- lib/activityobject.php | 7 ++++--- lib/attachmentlist.php | 3 ++- lib/inlineattachmentlist.php | 12 ++++-------- plugins/Bookmark/BookmarkPlugin.php | 9 +++++---- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/actions/oembed.php b/actions/oembed.php index 76264566ad..da0352edf8 100644 --- a/actions/oembed.php +++ b/actions/oembed.php @@ -127,11 +127,14 @@ class OembedAction extends Action } } $oembed['url']=$attachment->getUrl(); - $thumb = $attachment->getThumbnail(); - if ($thumb) { + try { + $thumb = $attachment->getThumbnail(); $oembed['thumbnail_url'] = $thumb->getUrl(); $oembed['thumbnail_width'] = $thumb->width; $oembed['thumbnail_height'] = $thumb->height; + unset($thumb); + } catch (UnsupportedMediaException $e) { + // No thumbnail data available } }else{ $oembed['type']='link'; diff --git a/classes/File.php b/classes/File.php index 2925819c36..45aee64a43 100644 --- a/classes/File.php +++ b/classes/File.php @@ -444,9 +444,11 @@ class File extends Managed_DataObject */ public function getThumbnail($width=null, $height=null, $crop=false) { - if ($this->width < 1 || $this->height < 1) { + if (intval($this->width) < 1 || intval($this->height) < 1) { // Old files may have 0 until migrated with scripts/upgrade.php - return null; + // For any legitimately unrepresentable ones, we could generate our + // own image (like a square with MIME type in text) + throw new UnsupportedMediaException('Object does not have an image representation.'); } if ($width === null) { @@ -476,13 +478,8 @@ class File extends Managed_DataObject 'height' => $height); $thumb = File_thumbnail::pkeyGet($params); if ($thumb === null) { - try { - $thumb = $this->generateThumbnail($width, $height, $crop); - } catch (UnsupportedMediaException $e) { - // FIXME: Add "unknown media" icon or something - } catch (ServerException $e) { - // Probably a remote media file, maybe not available locally - } + // throws exception on failure to generate thumbnail + $thumb = $this->generateThumbnail($width, $height, $crop); } return $thumb; } diff --git a/lib/activityobject.php b/lib/activityobject.php index 5a20ab2c18..d436803232 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -573,10 +573,11 @@ class ActivityObject $object->date = $file->date; } - $thumbnail = $file->getThumbnail(); - - if (!empty($thumbnail)) { + try { + $thumbnail = $file->getThumbnail(); $object->thumbnail = $thumbnail; + } catch (UnsupportedMediaException $e) { + $object->thumbnail = null; } switch (ActivityObject::canonicalType($object->type)) { diff --git a/lib/attachmentlist.php b/lib/attachmentlist.php index 0735815933..74248313eb 100644 --- a/lib/attachmentlist.php +++ b/lib/attachmentlist.php @@ -204,7 +204,7 @@ class AttachmentListItem extends Widget try { $thumb = $this->attachment->getThumbnail(); $this->out->element('img', array('alt' => '', 'src' => $thumb->getUrl(), 'width' => $thumb->width, 'height' => $thumb->height)); - } catch (Exception $e) { + } catch (UnsupportedMediaException $e) { // Image representation unavailable } } @@ -325,6 +325,7 @@ class Attachment extends AttachmentListItem try { $thumb = $this->attachment->getThumbnail(); $poster = $thumb->getUrl(); + unset ($thumb); } catch (Exception $e) { $poster = null; } diff --git a/lib/inlineattachmentlist.php b/lib/inlineattachmentlist.php index 622252324f..0eeb9b264b 100644 --- a/lib/inlineattachmentlist.php +++ b/lib/inlineattachmentlist.php @@ -62,16 +62,12 @@ class InlineAttachmentListItem extends AttachmentListItem function show() { - $this->thumb = $this->attachment->getThumbnail(); - if (!empty($this->thumb)) { + try { + $this->thumb = $this->attachment->getThumbnail(); parent::show(); + } catch (UnsupportedMediaException $e) { + $this->thumb = null; } - - } - - function getThumbInfo() - { - return $this->thumb; } function showLink() { diff --git a/plugins/Bookmark/BookmarkPlugin.php b/plugins/Bookmark/BookmarkPlugin.php index c7e074193a..befc4f2291 100644 --- a/plugins/Bookmark/BookmarkPlugin.php +++ b/plugins/Bookmark/BookmarkPlugin.php @@ -492,9 +492,8 @@ class BookmarkPlugin extends MicroAppPlugin // Attributes of the thumbnail, if any - $thumbnail = $target->getThumbnail(); - - if (!empty($thumbnail)) { + try { + $thumbnail = $target->getThumbnail(); $tattrs = array('rel' => 'preview', 'href' => $thumbnail->url); @@ -506,7 +505,9 @@ class BookmarkPlugin extends MicroAppPlugin $tattrs['media:height'] = $thumbnail->height; } - $object->extra[] = array('link', $attrs, null); + $object->extra[] = array('link', $tattrs, null); + } catch (UnsupportedMediaException $e) { + // No image thumbnail metadata available } return $object; -- 2.39.5