]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/Media.php
Merge pull request #11526 from annando/ap-endpoint-cache
[friendica.git] / src / Model / Post / Media.php
index a29b2dcc7269279419547165ada68161182c2b92..78f27b7a099252380b09821bc3c170d9f2dd17a3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -30,7 +30,8 @@ use Friendica\DI;
 use Friendica\Model\Item;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
-use Friendica\Network\HTTPClientOptions;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
 use Friendica\Util\Images;
 use Friendica\Util\Network;
 use Friendica\Util\ParseUrl;
@@ -168,7 +169,13 @@ class Media
                // Fetch the mimetype or size if missing.
                if (empty($media['mimetype']) || empty($media['size'])) {
                        $timeout = DI::config()->get('system', 'xrd_timeout');
-                       $curlResult = DI::httpClient()->head($media['url'], [HTTPClientOptions::TIMEOUT => $timeout]);
+                       $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]);
+
+                       // Workaround for systems that can't handle a HEAD request
+                       if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
+                               $curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
+                       }
+
                        if ($curlResult->isSuccess()) {
                                if (empty($media['mimetype'])) {
                                        $media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';
@@ -388,6 +395,10 @@ class Media
                }
 
                foreach ($attachments as $attachment) {
+                       if (Post\Link::exists($uriid, $attachment['preview'] ?? $attachment['url'])) {
+                               continue;
+                       }
+
                        // Only store attachments that are part of the unshared body
                        if (Item::containsLink($unshared_body, $attachment['preview'] ?? $attachment['url'], $attachment['type'])) {
                                self::insert($attachment);
@@ -508,7 +519,7 @@ class Media
                        $condition = DBA::mergeConditions($condition, ['type' => $types]);
                }
 
-               return DBA::selectToArray('post-media', [], $condition);
+               return DBA::selectToArray('post-media', [], $condition, ['order' => ['id']]);
        }
 
        /**
@@ -536,12 +547,17 @@ class Media
         * @param int    $uri_id
         * @param string $guid
         * @param array  $links list of links that shouldn't be added
+        * @param bool   $has_media
         * @return array attachments
         */
-       public static function splitAttachments(int $uri_id, string $guid = '', array $links = [])
+       public static function splitAttachments(int $uri_id, string $guid = '', array $links = [], bool $has_media = true)
        {
                $attachments = ['visual' => [], 'link' => [], 'additional' => []];
 
+               if (!$has_media) {
+                       return $attachments;
+               }
+
                $media = self::getByURIId($uri_id);
                if (empty($media)) {
                        return $attachments;
@@ -682,7 +698,7 @@ class Media
         * Get preview link for given media id
         *
         * @param integer $id   media id
-        * @param string  $size One of the ProxyUtils::SIZE_* constants
+        * @param string  $size One of the Proxy::SIZE_* constants
         * @return string preview link
         */
        public static function getPreviewUrlForId(int $id, string $size = ''):string
@@ -712,7 +728,7 @@ class Media
         * Get media link for given media id
         *
         * @param integer $id   media id
-        * @param string  $size One of the ProxyUtils::SIZE_* constants
+        * @param string  $size One of the Proxy::SIZE_* constants
         * @return string media link
         */
        public static function getUrlForId(int $id, string $size = ''):string