Reduce the usage of the "proxifyUrl" function

This commit is contained in:
Michael 2021-06-27 11:50:10 +00:00
parent 3fe6789c9c
commit b46b7b08ba
11 changed files with 205 additions and 71 deletions

View file

@ -1510,14 +1510,10 @@ class Contact
$avatar = $contact['avatar'];
}
if (empty($avatar)) {
$avatar = self::getDefaultAvatar([], $size);
}
if (Proxy::isLocalImage($avatar)) {
if (!empty($avatar) && Proxy::isLocalImage($avatar)) {
return $avatar;
} else {
return Proxy::proxifyUrl($avatar, false, $size);
return self::getAvatarUrlForId($contact['id'], $size);
}
}
@ -1665,12 +1661,47 @@ class Contact
/**
* Get avatar link for given contact id
*
* @param integer $cid contact id
* @param integer $cid contact id
* @param string $size One of the ProxyUtils::SIZE_* constants
* @return string avatar link
*/
public static function getAvatarUrlForId(int $cid):string
public static function getAvatarUrlForId(int $cid, string $size = ''):string
{
return DI::baseUrl() . '/photo/contact/' . $cid;
$url = DI::baseUrl() . '/photo/contact/';
switch ($size) {
case Proxy::SIZE_MICRO:
$url .= Proxy::PIXEL_MICRO . '/';
break;
case Proxy::SIZE_THUMB:
$url .= Proxy::PIXEL_THUMB . '/';
break;
case Proxy::SIZE_SMALL:
$url .= Proxy::PIXEL_SMALL . '/';
break;
case Proxy::SIZE_MEDIUM:
$url .= Proxy::PIXEL_MEDIUM . '/';
break;
case Proxy::SIZE_LARGE:
$url .= Proxy::PIXEL_LARGE . '/';
break;
}
return $url . $cid;
}
/**
* Get avatar link for given contact URL
*
* @param string $url contact url
* @param integer $uid user id
* @param string $size One of the ProxyUtils::SIZE_* constants
* @return string avatar link
*/
public static function getAvatarUrlForUrl(string $url, int $uid, string $size = ''):string
{
$condition = ["`nurl` = ? AND ((`uid` = ? AND `network` IN (?, ?)) OR `uid` = ?)",
Strings::normaliseLink($url), $uid, Protocol::FEED, Protocol::MAIL, 0];
$contact = self::selectFirst(['id'], $condition);
return self::getAvatarUrlForId($contact['id'] ?? 0, $size);
}
/**

View file

@ -2863,12 +2863,8 @@ class Item
continue;
}
$author = ['uid' => 0, 'id' => $item['author-id'],
'network' => $item['author-network'], 'url' => $item['author-link']];
$the_url = Contact::magicLinkByContact($author, $attachment['url']);
if (!empty($attachment['preview'])) {
$preview_url = Proxy::proxifyUrl(Contact::magicLinkByContact($author, $attachment['preview']));
$preview_url = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_LARGE);
} else {
$preview_url = '';
}
@ -2878,7 +2874,7 @@ class Item
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [
'$video' => [
'id' => $attachment['id'],
'src' => $the_url,
'src' => $attachment['url'],
'name' => $attachment['name'] ?: $attachment['url'],
'preview' => $preview_url,
'mime' => $attachment['mimetype'],
@ -2893,8 +2889,8 @@ class Item
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/audio.tpl'), [
'$audio' => [
'id' => $attachment['id'],
'src' => $the_url,
'name' => $attachment['name'] ?: $attachment['url'],
'src' => $attachment['url'],
'name' => $attachment['name'] ?: $attachment['url'],
'mime' => $attachment['mimetype'],
],
]);
@ -2904,14 +2900,11 @@ class Item
$trailing .= $media;
}
} elseif ($attachment['filetype'] == 'image') {
if (empty($preview_url) && (max($attachment['width'], $attachment['height']) > 600)) {
$preview_url = Proxy::proxifyUrl($the_url, false, ($attachment['width'] > $attachment['height']) ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE);
}
$media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [
'$image' => [
'src' => Proxy::proxifyUrl($the_url),
'preview' => $preview_url,
'attachment' => $attachment,
'$image' => [
'src' => Post\Media::getUrlForId($attachment['id']),
'preview' => Post\Media::getPreviewUrlForId($attachment['id'], ($attachment['width'] > $attachment['height']) ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE),
'attachment' => $attachment,
],
]);
// On Diaspora posts the attached pictures are leading
@ -2989,11 +2982,11 @@ class Item
'type' => 'link',
'url' => $attachment['url']];
if ($preview) {
if ($preview && !empty($attachment['preview'])) {
if ($attachment['preview-width'] >= 500) {
$data['image'] = $attachment['preview'] ?? '';
$data['image'] = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_MEDIUM);
} else {
$data['preview'] = $attachment['preview'] ?? '';
$data['preview'] = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_MEDIUM);
}
}

View file

@ -31,6 +31,7 @@ use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Util\Images;
use Friendica\Util\ParseUrl;
use Friendica\Util\Proxy;
use Friendica\Util\Strings;
/**
@ -494,10 +495,10 @@ class Media
/**
* Split the attachment media in the three segments "visual", "link" and "additional"
*
* @param int $uri_id
*
* @param int $uri_id
* @param string $guid
* @param array $links ist of links that shouldn't be added
* @param array $links ist of links that shouldn't be added
* @return array attachments
*/
public static function splitAttachments(int $uri_id, string $guid = '', array $links = [])
@ -526,7 +527,7 @@ class Media
continue 2;
}
}
if (!empty($medium['preview'])) {
$previews[] = $medium['preview'];
}
@ -631,4 +632,64 @@ class Media
return $body;
}
/**
* Get preview link for given media id
*
* @param integer $id media id
* @param string $size One of the ProxyUtils::SIZE_* constants
* @return string preview link
*/
public static function getPreviewUrlForId(int $id, string $size = ''):string
{
$url = DI::baseUrl() . '/photo/preview/';
switch ($size) {
case Proxy::SIZE_MICRO:
$url .= Proxy::PIXEL_MICRO . '/';
break;
case Proxy::SIZE_THUMB:
$url .= Proxy::PIXEL_THUMB . '/';
break;
case Proxy::SIZE_SMALL:
$url .= Proxy::PIXEL_SMALL . '/';
break;
case Proxy::SIZE_MEDIUM:
$url .= Proxy::PIXEL_MEDIUM . '/';
break;
case Proxy::SIZE_LARGE:
$url .= Proxy::PIXEL_LARGE . '/';
break;
}
return $url . $id;
}
/**
* Get media link for given media id
*
* @param integer $id media id
* @param string $size One of the ProxyUtils::SIZE_* constants
* @return string media link
*/
public static function getUrlForId(int $id, string $size = ''):string
{
$url = DI::baseUrl() . '/photo/media/';
switch ($size) {
case Proxy::SIZE_MICRO:
$url .= Proxy::PIXEL_MICRO . '/';
break;
case Proxy::SIZE_THUMB:
$url .= Proxy::PIXEL_THUMB . '/';
break;
case Proxy::SIZE_SMALL:
$url .= Proxy::PIXEL_SMALL . '/';
break;
case Proxy::SIZE_MEDIUM:
$url .= Proxy::PIXEL_MEDIUM . '/';
break;
case Proxy::SIZE_LARGE:
$url .= Proxy::PIXEL_LARGE . '/';
break;
}
return $url . $id;
}
}

View file

@ -329,9 +329,9 @@ class Profile
// correct contact table entry for the logged-in user.
$profile_contact = [];
if (!empty($profile['nurl'] ?? '')) {
if (local_user() && ($profile['uid'] ?? '') != local_user()) {
$profile_contact = Contact::getById(Contact::getIdForURL($profile['nurl'], local_user()));
if (!empty($profile['nurl'])) {
if (local_user() && ($profile['uid'] ?? 0) != local_user()) {
$profile_contact = Contact::getByURL($profile['nurl'], null, ['rel'], local_user());
}
if (!empty($profile['cid']) && self::getMyURL()) {
$profile_contact = Contact::selectFirst(['rel'], ['id' => $profile['cid']]);
@ -359,6 +359,12 @@ class Profile
$profile_url = DI::baseUrl()->get() . '/profile/' . $profile['nickname'];
}
if (!empty($profile['id'])) {
$cid = $profile['id'];
} else {
$cid = Contact::getIdForURL($profile_url, false);
}
$follow_link = null;
$unfollow_link = null;
$subscribe_feed_link = null;
@ -501,11 +507,9 @@ class Profile
$p['address'] = BBCode::convert($p['address']);
}
if (isset($p['photo'])) {
$p['photo'] = ProxyUtils::proxifyUrl($p['photo'], false, ProxyUtils::SIZE_SMALL);
}
$p['photo'] = Contact::getAvatarUrlForId($cid);
$p['url'] = Contact::magicLink(($p['url'] ?? '') ?: $profile_url);
$p['url'] = Contact::magicLink($profile_url);
$tpl = Renderer::getMarkupTemplate('profile/vcard.tpl');
$o .= Renderer::replaceMacros($tpl, [