mirror of
https://github.com/friendica/friendica
synced 2025-04-27 07:10:12 +00:00
Reduce the number of HTTP requests in the media handling
This commit is contained in:
parent
69345432e1
commit
e7d9c6c254
11 changed files with 207 additions and 89 deletions
|
@ -86,7 +86,7 @@ class Item
|
|||
|
||||
// Field list that is used to display the items
|
||||
const DISPLAY_FIELDLIST = [
|
||||
'uid', 'id', 'parent', 'guid', 'network', 'gravity',
|
||||
'uid', 'id', 'parent', 'guid', 'network', 'protocol', 'gravity',
|
||||
'uri-id', 'uri', 'thr-parent-id', 'thr-parent', 'parent-uri-id', 'parent-uri', 'conversation',
|
||||
'commented', 'created', 'edited', 'received', 'verb', 'object-type', 'postopts', 'plink',
|
||||
'wall', 'private', 'starred', 'origin', 'parent-origin', 'title', 'body', 'language', 'sensitive',
|
||||
|
@ -4169,10 +4169,11 @@ class Item
|
|||
* @param string $uri
|
||||
* @param int $uid
|
||||
* @param int $completion
|
||||
* @param string $mimetype
|
||||
*
|
||||
* @return integer item id
|
||||
*/
|
||||
public static function fetchByLink(string $uri, int $uid = 0, int $completion = ActivityPub\Receiver::COMPLETION_MANUAL): int
|
||||
public static function fetchByLink(string $uri, int $uid = 0, int $completion = ActivityPub\Receiver::COMPLETION_MANUAL, string $mimetype = ''): int
|
||||
{
|
||||
Logger::info('Trying to fetch link', ['uid' => $uid, 'uri' => $uri]);
|
||||
$item_id = self::searchByLink($uri, $uid);
|
||||
|
@ -4194,35 +4195,49 @@ class Item
|
|||
Hook::callAll('item_by_link', $hookData);
|
||||
|
||||
if (isset($hookData['item_id'])) {
|
||||
Logger::info('Hook link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $hookData['item_id']]);
|
||||
return is_numeric($hookData['item_id']) ? $hookData['item_id'] : 0;
|
||||
}
|
||||
|
||||
try {
|
||||
$curlResult = DI::httpClient()->head($uri, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON_AS, HttpClientOptions::REQUEST => HttpClientRequest::ACTIVITYPUB]);
|
||||
if (!HTTPSignature::isValidContentType($curlResult->getContentType(), $uri) && (current(explode(';', $curlResult->getContentType())) == 'application/json')) {
|
||||
if (!$mimetype) {
|
||||
try {
|
||||
$curlResult = DI::httpClient()->head($uri, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::JSON_AS, HttpClientOptions::REQUEST => HttpClientRequest::ACTIVITYPUB]);
|
||||
$mimetype = empty($curlResult) ? '' : $curlResult->getContentType();
|
||||
} catch (\Throwable $th) {
|
||||
Logger::info('Error while fetching HTTP link via HEAD', ['uid' => $uid, 'uri' => $uri, 'code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!HTTPSignature::isValidContentType($mimetype, $uri) && (current(explode(';', $mimetype)) == 'application/json')) {
|
||||
try {
|
||||
// Issue 14126: Workaround for Mastodon servers that return "application/json" on a "head" request.
|
||||
$curlResult = HTTPSignature::fetchRaw($uri, $uid);
|
||||
$mimetype = empty($curlResult) ? '' : $curlResult->getContentType();
|
||||
} catch (\Throwable $th) {
|
||||
Logger::info('Error while fetching HTTP link via signed GET', ['uid' => $uid, 'uri' => $uri, 'code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||
return 0;
|
||||
}
|
||||
if (HTTPSignature::isValidContentType($curlResult->getContentType(), $uri)) {
|
||||
$fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', $completion, $uid);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
Logger::info('Invalid link', ['uid' => $uid, 'uri' => $uri, 'code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!empty($fetched_uri)) {
|
||||
$item_id = self::searchByLink($fetched_uri, $uid);
|
||||
} else {
|
||||
$item_id = Diaspora::fetchByURL($uri);
|
||||
if (HTTPSignature::isValidContentType($mimetype, $uri)) {
|
||||
$fetched_uri = ActivityPub\Processor::fetchMissingActivity($uri, [], '', $completion, $uid);
|
||||
if (!empty($fetched_uri)) {
|
||||
$item_id = self::searchByLink($fetched_uri, $uid);
|
||||
if ($item_id) {
|
||||
Logger::info('ActivityPub link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]);
|
||||
return $item_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($item_id)) {
|
||||
Logger::info('Link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]);
|
||||
$item_id = Diaspora::fetchByURL($uri);
|
||||
if ($item_id) {
|
||||
Logger::info('Diaspora link fetched', ['uid' => $uid, 'uri' => $uri, 'id' => $item_id]);
|
||||
return $item_id;
|
||||
}
|
||||
|
||||
Logger::info('Link not found', ['uid' => $uid, 'uri' => $uri]);
|
||||
Logger::info('This is not an item link', ['uid' => $uid, 'uri' => $uri]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -51,6 +51,8 @@ class Media
|
|||
const ACTIVITY = 20;
|
||||
const ACCOUNT = 21;
|
||||
const HLS = 22;
|
||||
const JSON = 23;
|
||||
const LD = 24;
|
||||
const DOCUMENT = 128;
|
||||
|
||||
/**
|
||||
|
@ -180,14 +182,14 @@ class Media
|
|||
}
|
||||
|
||||
// Fetch the mimetype or size if missing.
|
||||
if (Network::isValidHttpUrl($media['url']) && (empty($media['mimetype']) || empty($media['size']))) {
|
||||
if (Network::isValidHttpUrl($media['url']) && empty($media['mimetype']) && !in_array($media['type'], [self::IMAGE, self::HLS])) {
|
||||
$timeout = DI::config()->get('system', 'xrd_timeout');
|
||||
try {
|
||||
$curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTENTTYPE]);
|
||||
$curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::AS_DEFAULT, HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTENTTYPE]);
|
||||
|
||||
// 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() && in_array($curlResult->getReturnCode(), [400, 403, 405])) {
|
||||
$curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::AS_DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
|
||||
}
|
||||
if ($curlResult->isSuccess()) {
|
||||
if (empty($media['mimetype'])) {
|
||||
|
@ -197,16 +199,20 @@ class Media
|
|||
$media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? strlen($curlResult->getBodyString() ?? ''));
|
||||
}
|
||||
} else {
|
||||
Logger::notice('Could not fetch head', ['media' => $media]);
|
||||
Logger::notice('Could not fetch head', ['media' => $media, 'code' => $curlResult->getReturnCode()]);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
$filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
|
||||
if (($media['type'] != self::DOCUMENT) && !empty($media['mimetype'])) {
|
||||
$media = self::addType($media);
|
||||
}
|
||||
|
||||
if (($media['type'] == self::IMAGE) || ($filetype == 'image')) {
|
||||
Logger::debug('Got type for url', ['type' => $media['type'], 'mimetype' => $media['mimetype'] ?? '', 'url' => $media['url']]);
|
||||
|
||||
if ($media['type'] == self::IMAGE) {
|
||||
$imagedata = Images::getInfoFromURLCached($media['url'], empty($media['description']));
|
||||
if ($imagedata) {
|
||||
$media['mimetype'] = $imagedata['mime'];
|
||||
|
@ -223,23 +229,19 @@ class Media
|
|||
}
|
||||
}
|
||||
|
||||
if ($media['type'] != self::DOCUMENT) {
|
||||
$media = self::addType($media);
|
||||
}
|
||||
|
||||
if (!empty($media['preview'])) {
|
||||
$media = self::addPreviewData($media);
|
||||
}
|
||||
|
||||
if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
|
||||
$media = self::addActivity($media);
|
||||
}
|
||||
|
||||
if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
|
||||
if (in_array($media['type'], [self::TEXT, self::ACTIVITY, self::LD, self::JSON, self::HTML, self::XML, self::PLAIN])) {
|
||||
$media = self::addAccount($media);
|
||||
}
|
||||
|
||||
if ($media['type'] == self::HTML) {
|
||||
if (in_array($media['type'], [self::ACTIVITY, self::LD, self::JSON])) {
|
||||
$media = self::addActivity($media);
|
||||
}
|
||||
|
||||
if (in_array($media['type'], [self::HTML, self::LD, self::JSON])) {
|
||||
$media = self::addPage($media);
|
||||
}
|
||||
|
||||
|
@ -254,6 +256,16 @@ class Media
|
|||
|
||||
$imagedata = Images::getInfoFromURLCached($media['preview']);
|
||||
if ($imagedata) {
|
||||
$media['blurhash'] = $imagedata['blurhash'] ?? null;
|
||||
|
||||
// When the original picture is potentially animated but the preview isn't, we override the preview
|
||||
if (in_array($media['mimetype'] ?? '', ['image/gif', 'image/png']) && !in_array($imagedata['mime'], ['image/gif', 'image/png'])) {
|
||||
$media['preview'] = $media['url'];
|
||||
$media['preview-width'] = $media['width'];
|
||||
$media['preview-height'] = $media['height'];
|
||||
return $media;
|
||||
}
|
||||
|
||||
$media['preview-width'] = $imagedata[0];
|
||||
$media['preview-height'] = $imagedata[1];
|
||||
}
|
||||
|
@ -269,19 +281,22 @@ class Media
|
|||
*/
|
||||
private static function addActivity(array $media): array
|
||||
{
|
||||
$id = Item::fetchByLink($media['url'], 0, ActivityPub\Receiver::COMPLETION_ASYNC);
|
||||
$id = Item::fetchByLink($media['url'], 0, ActivityPub\Receiver::COMPLETION_ASYNC, $media['mimetype'] ?? '');
|
||||
if (empty($id)) {
|
||||
$media['type'] = $media['type'] == self::ACTIVITY ? self::JSON : $media['type'];
|
||||
return $media;
|
||||
}
|
||||
|
||||
$item = Post::selectFirst([], ['id' => $id, 'network' => Protocol::FEDERATED]);
|
||||
if (empty($item['id'])) {
|
||||
Logger::debug('Not a federated activity', ['id' => $id, 'uri-id' => $media['uri-id'], 'url' => $media['url']]);
|
||||
$media['type'] = $media['type'] == self::ACTIVITY ? self::JSON : $media['type'];
|
||||
return $media;
|
||||
}
|
||||
|
||||
if ($item['uri-id'] == $media['uri-id']) {
|
||||
Logger::info('Media-Uri-Id is identical to Uri-Id', ['uri-id' => $media['uri-id']]);
|
||||
$media['type'] = $media['type'] == self::ACTIVITY ? self::JSON : $media['type'];
|
||||
return $media;
|
||||
}
|
||||
|
||||
|
@ -290,6 +305,7 @@ class Media
|
|||
parse_url($item['plink'], PHP_URL_HOST) != parse_url($item['uri'], PHP_URL_HOST)
|
||||
) {
|
||||
Logger::debug('Not a link to an activity', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
|
||||
$media['type'] = $media['type'] == self::ACTIVITY ? self::JSON : $media['type'];
|
||||
return $media;
|
||||
}
|
||||
|
||||
|
@ -375,14 +391,23 @@ class Media
|
|||
*/
|
||||
private static function addPage(array $media): array
|
||||
{
|
||||
$data = ParseUrl::getSiteinfoCached($media['url']);
|
||||
$data = ParseUrl::getSiteinfoCached($media['url'], $media['mimetype'] ?? '');
|
||||
if (empty($data['images'][0]['src']) && empty($data['text']) && empty($data['title'])) {
|
||||
if (!empty($media['preview'])) {
|
||||
$media = self::addPreviewData($media);
|
||||
Logger::debug('Detected site data is empty, use suggested media data instead', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'type' => $data['type']]);
|
||||
}
|
||||
} else {
|
||||
$media['preview'] = $data['images'][0]['src'] ?? null;
|
||||
$media['preview-height'] = $data['images'][0]['height'] ?? null;
|
||||
$media['preview-width'] = $data['images'][0]['width'] ?? null;
|
||||
$media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
|
||||
$media['description'] = $data['text'] ?? null;
|
||||
$media['name'] = $data['title'] ?? null;
|
||||
}
|
||||
|
||||
$media['type'] = self::HTML;
|
||||
$media['size'] = $data['size'] ?? null;
|
||||
$media['preview'] = $data['images'][0]['src'] ?? null;
|
||||
$media['preview-height'] = $data['images'][0]['height'] ?? null;
|
||||
$media['preview-width'] = $data['images'][0]['width'] ?? null;
|
||||
$media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
|
||||
$media['description'] = $data['text'] ?? null;
|
||||
$media['name'] = $data['title'] ?? null;
|
||||
$media['author-url'] = $data['author_url'] ?? null;
|
||||
$media['author-name'] = $data['author_name'] ?? null;
|
||||
$media['author-image'] = $data['author_img'] ?? null;
|
||||
|
@ -481,6 +506,12 @@ class Media
|
|||
$type = self::TORRENT;
|
||||
} elseif (($filetype == 'application') && ($subtype == 'vnd.apple.mpegurl')) {
|
||||
$type = self::HLS;
|
||||
} elseif (($filetype == 'application') && ($subtype == 'activity+json')) {
|
||||
$type = self::ACTIVITY;
|
||||
} elseif (($filetype == 'application') && ($subtype == 'ld+json')) {
|
||||
$type = self::LD;
|
||||
} elseif (($filetype == 'application') && ($subtype == 'json')) {
|
||||
$type = self::JSON;
|
||||
} elseif ($filetype == 'application') {
|
||||
$type = self::APPLICATION;
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue