<?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
*
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\Proxy;
/**
*/
class Link
{
+ /**
+ * Check if the link is stored
+ *
+ * @param int $uri_id
+ * @param string $url
+ * @return bool
+ */
+ public static function exists(int $uri_id, string $url)
+ {
+ return DBA::exists('post-link', ['uri-id' => $uri_id, 'url' => $url]);
+ }
+
public static function getByLink(int $uri_id, string $url, $size = '')
{
if (empty($uri_id) || empty($url) || Proxy::isLocalImage($url)) {
}
if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
- Logger::info('Bad URL, quitting', ['uri-id' => $uri_id, 'url' => $url]);
+ Logger::info('Bad URL, quitting', ['uri-id' => $uri_id, 'url' => $url, 'callstack' => System::callstack(20)]);
return $url;
}
} else {
$mime = self::fetchMimeType($url);
- DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime]);
+ DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
$id = DBA::lastInsertId();
Logger::info('Inserted', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
}
return $url . $id;
}
- private static function fetchMimeType(string $url)
+ private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT)
{
$timeout = DI::config()->get('system', 'xrd_timeout');
- $curlResult = DI::httpRequest()->head($url, ['timeout' => $timeout]);
+ $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
if ($curlResult->isSuccess()) {
if (empty($media['mimetype'])) {
- return $curlResult->getHeader('Content-Type');
+ return $curlResult->getHeader('Content-Type')[0] ?? '';
}
}
return '';