From d4be5349b30f49fa049dbfc854bb2a95eeb1d5c1 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 7 Jan 2016 17:35:37 +0100 Subject: [PATCH] think I have managed to show oEmbed images better now --- classes/File.php | 23 +++++++++++++++++-- lib/attachmentlist.php | 4 ++-- lib/attachmentlistitem.php | 39 +++++++++++++++++++++------------ plugins/Oembed/OembedPlugin.php | 13 +++++------ 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/classes/File.php b/classes/File.php index a3ded46ec9..2390f848de 100644 --- a/classes/File.php +++ b/classes/File.php @@ -356,28 +356,47 @@ class File extends Managed_DataObject return $protocol.'://'.$server.$path.$filename; } + static $_enclosures = array(); + function getEnclosure(){ + if (isset(self::$_enclosures[$this->getID()])) { + common_debug('Found cached enclosure for file id=='.$this->getID()); + return self::$_enclosures[$this->getID()]; + } + $enclosure = (object) array(); foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype') as $key) { $enclosure->$key = $this->$key; } - $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml'); + $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml', 'text/html'); if (!isset($this->filename) && in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) { // This fetches enclosure metadata for non-local links with unset/HTML mimetypes, // which may be enriched through oEmbed or similar (implemented as plugins) Event::handle('FileEnclosureMetadata', array($this, &$enclosure)); } - if (empty($enclosure->mimetype) || in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) { + if (empty($enclosure->mimetype)) { // This means we either don't know what it is, so it can't // be shown as an enclosure, or it is an HTML link which // does not link to a resource with further metadata. throw new ServerException('Unknown enclosure mimetype, not enough metadata'); } + + self::$_enclosures[$this->getID()] = $enclosure; return $enclosure; } + public function hasThumbnail() + { + try { + $this->getThumbnail(); + } catch (Exception $e) { + return false; + } + return true; + } + /** * Get the attachment's thumbnail record, if any. * Make sure you supply proper 'int' typed variables (or null). diff --git a/lib/attachmentlist.php b/lib/attachmentlist.php index dcae917be3..4d4b451167 100644 --- a/lib/attachmentlist.php +++ b/lib/attachmentlist.php @@ -76,8 +76,8 @@ class AttachmentList extends Widget { $attachments = $this->notice->attachments(); foreach ($attachments as $key=>$att) { - // Only show attachments representable with a title - if ($att->getTitle() === null) { + // Remove attachments which are not representable with neither a title nor thumbnail + if ($att->getTitle() === null && !$att->hasThumbnail()) { unset($attachments[$key]); } } diff --git a/lib/attachmentlistitem.php b/lib/attachmentlistitem.php index 132b2002ed..655ae73300 100644 --- a/lib/attachmentlistitem.php +++ b/lib/attachmentlistitem.php @@ -105,9 +105,24 @@ class AttachmentListItem extends Widget } function showRepresentation() { + $enclosure = $this->attachment->getEnclosure(); + if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) { - if (!empty($this->attachment->mimetype)) { - $mediatype = common_get_mime_media($this->attachment->mimetype); + if (!empty($enclosure->mimetype)) { + // First, prepare a thumbnail if it exists. + $thumb = null; + try { + // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still") + $thumb = $this->attachment->getThumbnail(null, null, false, false); + } catch (UseFileAsThumbnailException $e) { + $thumb = null; + } catch (UnsupportedMediaException $e) { + // FIXME: Show a good representation of unsupported/unshowable images + $thumb = null; + } + + // Then get the kind of mediatype we're dealing with + $mediatype = common_get_mime_media($enclosure->mimetype); // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it: // $ mediainfo --inform='General;%InternetMediaType%' @@ -117,27 +132,22 @@ class AttachmentListItem extends Widget switch ($mediatype) { // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation case 'image': - try { - // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still") - $thumb = $this->attachment->getThumbnail(null, null, false, false); + if ($thumb instanceof File_thumbnail) { $this->out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo', 'alt' => ''])); - } catch (UseFileAsThumbnailException $e) { - $this->out->element('img', array('class'=>'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title)); - } catch (UnsupportedMediaException $e) { - // FIXME: Show a good representation of unsupported/unshowable images + } else { + $this->out->element('img', array('class'=>'u-photo', 'src' => $this->attachment->getUrl(), 'alt' => $this->attachment->getTitle())); } + unset($thumb); // there's no need carrying this along after this break; // HTML5 media elements case 'audio': case 'video': - try { - $thumb = $this->attachment->getThumbnail(); + if ($thumb instanceof File_thumbnail) { $poster = $thumb->getUrl(); - unset ($thumb); - } catch (Exception $e) { - $poster = null; + unset($thumb); // there's no need carrying this along after this } + $this->out->elementStart($mediatype, array('class'=>"attachment_player u-{$mediatype}", 'poster'=>$poster, @@ -149,6 +159,7 @@ class AttachmentListItem extends Widget break; default: + unset($thumb); // there's no need carrying this along switch ($this->attachment->mimetype) { case 'text/html': if (!empty($this->attachment->filename) diff --git a/plugins/Oembed/OembedPlugin.php b/plugins/Oembed/OembedPlugin.php index f9bd2af896..7d66bdebef 100644 --- a/plugins/Oembed/OembedPlugin.php +++ b/plugins/Oembed/OembedPlugin.php @@ -187,7 +187,7 @@ class OembedPlugin extends Plugin return true; } - public function onStartShowAttachmentRepresentation(HTMLOutputter $out, File $file) + public function onShowUnsupportedAttachmentRepresentation(HTMLOutputter $out, File $file) { try { $oembed = File_oembed::getByFile($file); @@ -195,6 +195,7 @@ class OembedPlugin extends Plugin return true; } + // the 'photo' type is shown through ordinary means, using StartShowAttachmentRepresentation! switch ($oembed->type) { case 'rich': case 'video': @@ -207,15 +208,11 @@ class OembedPlugin extends Plugin 'elements'=>'*+object+embed'); $out->raw(htmLawed($oembed->html,$config)); } + return false; break; - - case 'photo': - $out->element('img', array('src' => $oembed->url, 'width' => $oembed->width, 'height' => $oembed->height, 'alt' => 'alt')); - break; - - default: - Event::handle('ShowUnsupportedAttachmentRepresentation', array($out, $file)); } + + return true; } public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null) -- 2.39.2