]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Better fallback on UnsupportedMediaException
authorMikael Nordfeldth <mmn@hethane.se>
Mon, 28 Apr 2014 10:12:06 +0000 (12:12 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Mon, 28 Apr 2014 10:12:06 +0000 (12:12 +0200)
actions/oembed.php
classes/File.php
lib/activityobject.php
lib/attachmentlist.php
lib/inlineattachmentlist.php
plugins/Bookmark/BookmarkPlugin.php

index 76264566ad118de7797e38db4ac55c5c51f1f53a..da0352edf878816f8dbde6e8f08ab473b6a94a4d 100644 (file)
@@ -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';
index 2925819c36e1f4fa2d0cd238cadae5756889f556..45aee64a43763a903f3db537f005772ea7833599 100644 (file)
@@ -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;
     }
index 5a20ab2c18d35965d9d307f1b21ce2d3137b79a3..d43680323245877455c6f28fcc42d7a5356574d7 100644 (file)
@@ -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)) {
index 073581593381fb3ea3405286d663b71670ae8705..74248313ebadd095235170cc26b54915323ab9da 100644 (file)
@@ -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;
                     }
index 622252324f05832e52d0a91ac08dab4339152d83..0eeb9b264b3d024277e20de4eccbfa7d5a361863 100644 (file)
@@ -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() {
index c7e074193a4229f98196aca02dca678763e475ac..befc4f22910990f5d5583c14470f5111ecf3df21 100644 (file)
@@ -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;