X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FPost%2FLink.php;h=3fafdd7eaf7b93f04e175b7124600be812dcec9f;hb=a7fd9e3223ce95dc508c40353b4019b886f42902;hp=52ed5572ceab3204ef34059836f61e844e1f38a3;hpb=182c3db9b8b414cb8aa43602e689418225421ab2;p=friendica.git diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index 52ed5572ce..3fafdd7eaf 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -1,6 +1,6 @@ $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)) { @@ -113,27 +118,41 @@ class Link * Fetches MIME type by URL and Accept: header * * @param string $url URL to fetch - * @param string $accept Accept: line - * @return string Discovered MIME type or empty string on failure + * @param string $accept Comma-separated list of expected response MIME type(s) + * @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; } /** * Add external links and replace them in the body * - * @param integer $uriId URI id - * @param string $body HTML body + * @param integer $uriId + * @param string $body Item body formatted with BBCodes * @return string Body with replaced links */ public static function insertFromBody(int $uriId, string $body): string