]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/Link.php
Fix uddate issues and improve speed when displaying contact posts
[friendica.git] / src / Model / Post / Link.php
index 83e5da7b314dc49830bcfcf646fe9592e18e8187..87bc457524ede28dcb820b086dfbad969be5c74e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 namespace Friendica\Model\Post;
 
 use Friendica\Core\Logger;
-use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
+use Friendica\Util\HTTPSignature;
+use Friendica\Util\Images;
 use Friendica\Util\Proxy;
+use Friendica\Object\Image;
 
 /**
  * Class Link
@@ -55,7 +57,7 @@ class Link
         * @param int $uriId
         * @param string $url
         * @param string $size
-        * @return string Found link URL + id on success, $url on failture
+        * @return string Found link URL + id on success, $url on failure
         */
        public static function getByLink(int $uriId, string $url, string $size = ''): string
        {
@@ -64,7 +66,7 @@ class Link
                }
 
                if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
-                       Logger::info('Bad URL, quitting', ['uri-id' => $uriId, 'url' => $url, 'callstack' => System::callstack(20)]);
+                       Logger::info('Bad URL, quitting', ['uri-id' => $uriId, 'url' => $url]);
                        return $url;
                }
 
@@ -73,11 +75,13 @@ class Link
                        $id = $link['id'];
                        Logger::info('Found', ['id' => $id, 'uri-id' => $uriId, 'url' => $url]);
                } else {
-                       $mime = self::fetchMimeType($url);
+                       $fields = self::fetchMimeType($url);
+                       $fields['uri-id'] = $uriId;
+                       $fields['url'] = $url;
 
-                       DBA::insert('post-link', ['uri-id' => $uriId, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
+                       DBA::insert('post-link', $fields, Database::INSERT_IGNORE);
                        $id = DBA::lastInsertId();
-                       Logger::info('Inserted', ['id' => $id, 'uri-id' => $uriId, 'url' => $url]);
+                       Logger::info('Inserted', $fields);
                }
 
                if (empty($id)) {
@@ -114,19 +118,33 @@ class Link
         *
         * @param string $url URL to fetch
         * @param string $accept Comma-separated list of expected response MIME type(s)
-        * @return string Discovered MIME type or empty string on failure
+        * @return array Discovered MIME type and blurhash or empty array on failure
         */
-       private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT): string
+       private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT): array
        {
                $timeout = DI::config()->get('system', 'xrd_timeout');
 
-               $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
-
-               if ($curlResult->isSuccess() && empty($media['mimetype'])) {
-                       return $curlResult->getHeader('Content-Type')[0] ?? '';
+               try {
+                       $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
+                       if (empty($curlResult) || !$curlResult->isSuccess()) {
+                               return [];
+                       }
+               } catch (\Exception $exception) {
+                       Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
+                       return [];
+               }
+               $fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
+
+               $img_str = $curlResult->getBody();
+               $image = new Image($img_str, Images::getMimeTypeByData($img_str));
+               if ($image->isValid()) {
+                       $fields['mimetype'] = $image->getType();
+                       $fields['width']    = $image->getWidth();
+                       $fields['height']   = $image->getHeight();
+                       $fields['blurhash'] = $image->getBlurHash();
                }
 
-               return '';
+               return $fields;
        }
 
        /**