Issue 10019: Fix embedding of media objects
authorMichael <heluecht@pirati.ca>
Fri, 12 Mar 2021 23:04:51 +0000 (23:04 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 12 Mar 2021 23:04:51 +0000 (23:04 +0000)
src/Content/Text/BBCode.php
src/Module/ParseUrl.php
src/Util/ParseUrl.php

index 9973705..0b49e75 100644 (file)
@@ -2257,10 +2257,10 @@ class BBCode
                        return $result;
                }
 
-               $siteinfo = ParseUrl::getSiteinfoCached($url);
+               $type = ParseUrl::getContentType($url);
 
-               if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
-                       switch ($siteinfo['type']) {
+               if (in_array($type, ['image', 'video', 'audio'])) {
+                       switch ($type) {
                                case 'video':
                                        $bbcode = "\n" . '[video]' . $url . '[/video]' . "\n";
                                        break;
@@ -2275,6 +2275,8 @@ class BBCode
                        return $bbcode;
                }
 
+               $siteinfo = ParseUrl::getSiteinfoCached($url);
+
                unset($siteinfo['keywords']);
 
                // Bypass attachment if parse url for a comment
index 8e72c4f..7138238 100644 (file)
@@ -94,11 +94,10 @@ class ParseUrl extends BaseModule
                }
 
                if ($format == 'json') {
-                       $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
+                       $type = Util\ParseUrl::getContentType($url);
 
-                       if (empty($siteinfo['title']) && empty($siteinfo['text']) && empty($siteinfo['image'])
-                               && in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
-                               switch ($siteinfo['type']) {
+                       if (in_array($type, ['image', 'video', 'audio'])) {
+                               switch ($type) {
                                        case 'video':
                                                $content_type = 'video';
                                                break;
@@ -114,6 +113,8 @@ class ParseUrl extends BaseModule
                                $ret['data'] = ['url' => $url];
                                $ret['success'] = true;
                        } else {
+                               $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
+
                                unset($siteinfo['keywords']);
 
                                $ret['data'] = $siteinfo;
index de280bc..a634545 100644 (file)
@@ -51,6 +51,30 @@ class ParseUrl
         */
        const MIN_DESC_COUNT = 100;
 
+       /**
+        * Fetch the content type of the given url
+        * @param string $url URL of the page
+        * @return string content type 
+        */
+       public static function getContentType(string $url)
+       {
+               $curlResult = DI::httpRequest()->head($url);
+               if (!$curlResult->isSuccess()) {
+                       return '';
+               }
+
+               $contenttype =  $curlResult->getHeader('Content-Type');
+               if (empty($contenttype)) {
+                       return '';
+               }
+               
+               if (!preg_match('#(image|video|audio)/#i', $contenttype, $matches)) {
+                       return '';
+               }
+
+               return array_pop($matches);
+       }
+
        /**
         * Search for chached embeddable data of an url otherwise fetch it
         *