Merge remote-tracking branch 'upstream/2021.03-rc' into issue-10019
authorMichael <heluecht@pirati.ca>
Sat, 13 Mar 2021 12:30:41 +0000 (12:30 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 13 Mar 2021 12:30:41 +0000 (12:30 +0000)
src/Module/ParseUrl.php
src/Util/ParseUrl.php

index 8e72c4f..ed48ea1 100644 (file)
@@ -96,8 +96,7 @@ class ParseUrl extends BaseModule
                if ($format == 'json') {
                        $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
 
-                       if (empty($siteinfo['title']) && empty($siteinfo['text']) && empty($siteinfo['image'])
-                               && in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
+                       if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
                                switch ($siteinfo['type']) {
                                        case 'video':
                                                $content_type = 'video';
index de280bc..83d0d84 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
         *
@@ -186,6 +210,12 @@ class ParseUrl
                        return $siteinfo;
                }
 
+               $type = self::getContentType($url);
+               if (in_array($type, ['image', 'video', 'audio'])) {
+                       $siteinfo['type'] = $type;
+                       return $siteinfo;
+               }
+
                $curlResult = DI::httpRequest()->get($url);
                if (!$curlResult->isSuccess()) {
                        return $siteinfo;
@@ -227,7 +257,7 @@ class ParseUrl
                        $oembed_data = OEmbed::fetchURL($url);
 
                        if (!empty($oembed_data->type)) {
-                               if (!in_array($oembed_data->type, ['error', 'rich', ''])) {
+                               if (!in_array($oembed_data->type, ['error', 'rich', 'image', 'video', 'audio', ''])) {
                                        $siteinfo['type'] = $oembed_data->type;
                                }