Better fallback on UnsupportedMediaException

This commit is contained in:
Mikael Nordfeldth 2014-04-28 12:12:06 +02:00
parent e526909bd8
commit 37ce1f4766
6 changed files with 26 additions and 27 deletions

View File

@ -127,11 +127,14 @@ class OembedAction extends Action
} }
} }
$oembed['url']=$attachment->getUrl(); $oembed['url']=$attachment->getUrl();
$thumb = $attachment->getThumbnail(); try {
if ($thumb) { $thumb = $attachment->getThumbnail();
$oembed['thumbnail_url'] = $thumb->getUrl(); $oembed['thumbnail_url'] = $thumb->getUrl();
$oembed['thumbnail_width'] = $thumb->width; $oembed['thumbnail_width'] = $thumb->width;
$oembed['thumbnail_height'] = $thumb->height; $oembed['thumbnail_height'] = $thumb->height;
unset($thumb);
} catch (UnsupportedMediaException $e) {
// No thumbnail data available
} }
}else{ }else{
$oembed['type']='link'; $oembed['type']='link';

View File

@ -444,9 +444,11 @@ class File extends Managed_DataObject
*/ */
public function getThumbnail($width=null, $height=null, $crop=false) 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 // 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) { if ($width === null) {
@ -476,13 +478,8 @@ class File extends Managed_DataObject
'height' => $height); 'height' => $height);
$thumb = File_thumbnail::pkeyGet($params); $thumb = File_thumbnail::pkeyGet($params);
if ($thumb === null) { if ($thumb === null) {
try { // throws exception on failure to generate thumbnail
$thumb = $this->generateThumbnail($width, $height, $crop); $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
}
} }
return $thumb; return $thumb;
} }

View File

@ -573,10 +573,11 @@ class ActivityObject
$object->date = $file->date; $object->date = $file->date;
} }
$thumbnail = $file->getThumbnail(); try {
$thumbnail = $file->getThumbnail();
if (!empty($thumbnail)) {
$object->thumbnail = $thumbnail; $object->thumbnail = $thumbnail;
} catch (UnsupportedMediaException $e) {
$object->thumbnail = null;
} }
switch (ActivityObject::canonicalType($object->type)) { switch (ActivityObject::canonicalType($object->type)) {

View File

@ -204,7 +204,7 @@ class AttachmentListItem extends Widget
try { try {
$thumb = $this->attachment->getThumbnail(); $thumb = $this->attachment->getThumbnail();
$this->out->element('img', array('alt' => '', 'src' => $thumb->getUrl(), 'width' => $thumb->width, 'height' => $thumb->height)); $this->out->element('img', array('alt' => '', 'src' => $thumb->getUrl(), 'width' => $thumb->width, 'height' => $thumb->height));
} catch (Exception $e) { } catch (UnsupportedMediaException $e) {
// Image representation unavailable // Image representation unavailable
} }
} }
@ -325,6 +325,7 @@ class Attachment extends AttachmentListItem
try { try {
$thumb = $this->attachment->getThumbnail(); $thumb = $this->attachment->getThumbnail();
$poster = $thumb->getUrl(); $poster = $thumb->getUrl();
unset ($thumb);
} catch (Exception $e) { } catch (Exception $e) {
$poster = null; $poster = null;
} }

View File

@ -62,16 +62,12 @@ class InlineAttachmentListItem extends AttachmentListItem
function show() function show()
{ {
$this->thumb = $this->attachment->getThumbnail(); try {
if (!empty($this->thumb)) { $this->thumb = $this->attachment->getThumbnail();
parent::show(); parent::show();
} catch (UnsupportedMediaException $e) {
$this->thumb = null;
} }
}
function getThumbInfo()
{
return $this->thumb;
} }
function showLink() { function showLink() {

View File

@ -492,9 +492,8 @@ class BookmarkPlugin extends MicroAppPlugin
// Attributes of the thumbnail, if any // Attributes of the thumbnail, if any
$thumbnail = $target->getThumbnail(); try {
$thumbnail = $target->getThumbnail();
if (!empty($thumbnail)) {
$tattrs = array('rel' => 'preview', $tattrs = array('rel' => 'preview',
'href' => $thumbnail->url); 'href' => $thumbnail->url);
@ -506,7 +505,9 @@ class BookmarkPlugin extends MicroAppPlugin
$tattrs['media:height'] = $thumbnail->height; $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; return $object;