From a31256412e2373b2ad784a7d41e75731c1658b24 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 May 2022 05:37:17 +0000 Subject: [PATCH 01/14] Cache contact avatars locally as files --- .gitignore | 6 +- src/Model/Contact.php | 184 ++++++++++++++++++++++++++-- src/Worker/RemoveUnusedContacts.php | 58 +++++---- static/defaults.config.php | 4 + 4 files changed, 213 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index f0d31912d9..56b90b0e8e 100644 --- a/.gitignore +++ b/.gitignore @@ -85,4 +85,8 @@ venv/ /bin/phpunit #Ignore cache file -.php_cs.cache \ No newline at end of file +.php_cs.cache + +#ignore avatar picture cache path +/avatar + diff --git a/src/Model/Contact.php b/src/Model/Contact.php index c6e8b608fc..f034ca69ad 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -35,11 +35,15 @@ use Friendica\Core\Worker; 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\Network\HTTPException; use Friendica\Network\Probe; +use Friendica\Object\Image; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use Friendica\Util\DateTimeFormat; +use Friendica\Util\HTTPSignature; use Friendica\Util\Images; use Friendica\Util\Network; use Friendica\Util\Proxy; @@ -797,7 +801,7 @@ class Contact public static function remove($id) { // We want just to make sure that we don't delete our "self" contact - $contact = DBA::selectFirst('contact', ['uid'], ['id' => $id, 'self' => false]); + $contact = DBA::selectFirst('contact', ['uri-id', 'photo', 'thumb', 'micro'], ['id' => $id, 'self' => false]); if (!DBA::isResult($contact)) { return; } @@ -805,6 +809,12 @@ class Contact // Archive the contact self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]); + if (!DBA::exists('contact', ['uri-id' => $contact['uri-id'], 'deleted' => false])) { + self::deleteAvatarCache($contact['photo']); + self::deleteAvatarCache($contact['thumb']); + self::deleteAvatarCache($contact['micro']); + } + // Delete it in the background Worker::add(PRIORITY_MEDIUM, 'Contact\Remove', $id); } @@ -827,7 +837,7 @@ class Contact } if (in_array($contact['rel'], [self::SHARING, self::FRIEND])) { - $cdata = Contact::getPublicAndUserContactID($contact['id'], $contact['uid']); + $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']); if (!empty($cdata['public'])) { Worker::add(PRIORITY_HIGH, 'Contact\Unfollow', $cdata['public'], $contact['uid']); } @@ -856,7 +866,7 @@ class Contact } if (in_array($contact['rel'], [self::FOLLOWER, self::FRIEND])) { - $cdata = Contact::getPublicAndUserContactID($contact['id'], $contact['uid']); + $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']); if (!empty($cdata['public'])) { Worker::add(PRIORITY_HIGH, 'Contact\RevokeFollow', $cdata['public'], $contact['uid']); } @@ -882,7 +892,7 @@ class Contact throw new \InvalidArgumentException('Unexpected public contact record'); } - $cdata = Contact::getPublicAndUserContactID($contact['id'], $contact['uid']); + $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']); if (in_array($contact['rel'], [self::SHARING, self::FRIEND]) && !empty($cdata['public'])) { Worker::add(PRIORITY_HIGH, 'Contact\Unfollow', $cdata['public'], $contact['uid']); @@ -1464,7 +1474,7 @@ class Contact $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params)); if ($pager->getStart() == 0) { - $cdata = Contact::getPublicAndUserContactID($cid, local_user()); + $cdata = self::getPublicAndUserContactID($cid, local_user()); if (!empty($cdata['public'])) { $pinned = Post\Collection::selectToArrayForContact($cdata['public'], Post\Collection::FEATURED, $fields); $items = array_merge($items, $pinned); @@ -1477,7 +1487,7 @@ class Contact $items = Post::toArray(Post::selectForUser(local_user(), $fields, $condition, $params)); if ($pager->getStart() == 0) { - $cdata = Contact::getPublicAndUserContactID($cid, local_user()); + $cdata = self::getPublicAndUserContactID($cid, local_user()); if (!empty($cdata['public'])) { $condition = ["`uri-id` IN (SELECT `uri-id` FROM `collection-view` WHERE `cid` = ? AND `type` = ?)", $cdata['public'], Post\Collection::FEATURED]; @@ -1597,6 +1607,27 @@ class Contact private static function getAvatarPath(array $contact, string $size, $no_update = false) { $contact = self::checkAvatarCacheByArray($contact, $no_update); + + if (!DI::config()->get('system', 'avatar_cache')) { + switch ($size) { + case Proxy::SIZE_MICRO: + if (self::getAvatarFile($contact['micro'])) { + return $contact['micro']; + } + break; + case Proxy::SIZE_THUMB: + if (self::getAvatarFile($contact['thumb'])) { + return $contact['thumb']; + } + break; + case Proxy::SIZE_SMALL: + if (self::getAvatarFile($contact['photo'])) { + return $contact['photo']; + } + break; + } + } + return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? ''); } @@ -1905,7 +1936,7 @@ class Contact */ public static function updateAvatar(int $cid, string $avatar, bool $force = false, bool $create_cache = false) { - $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'xmpp', 'addr', 'nurl', 'url', 'network'], + $contact = DBA::selectFirst('contact', ['uid', 'avatar', 'photo', 'thumb', 'micro', 'xmpp', 'addr', 'nurl', 'url', 'network', 'uri-id'], ['id' => $cid, 'self' => false]); if (!DBA::isResult($contact)) { return; @@ -1946,6 +1977,10 @@ class Contact } if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) { + self::deleteAvatarCache($contact['photo']); + self::deleteAvatarCache($contact['thumb']); + self::deleteAvatarCache($contact['micro']); + if ($default_avatar && Proxy::isLocalImage($avatar)) { $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => $avatar, @@ -1997,9 +2032,8 @@ class Contact } } else { Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'photo-type' => Photo::CONTACT_AVATAR]); - $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), - 'photo' => '', 'thumb' => '', 'micro' => '']; - $update = ($avatar != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force; + $fields = self::fetchAvatarContact($contact, $avatar); + $update = ($avatar . $fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force; } if (!$update) { @@ -2030,6 +2064,134 @@ class Contact self::update($fields, ['id' => $cids]); } + /** + * Returns a field array with locally cached avatar pictures + * + * @param array $contact + * @param string $avatar + * @return array + */ + private static function fetchAvatarContact(array $contact, string $avatar): array + { + $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => '']; + + if (!DI::config()->get('system', 'avatar_cache')) { + self::deleteAvatarCache($contact['photo']); + self::deleteAvatarCache($contact['thumb']); + self::deleteAvatarCache($contact['micro']); + return $fields; + } + + if (Network::isLocalLink($avatar)) { + return $fields; + } + + if ($avatar != $contact['avatar']) { + self::deleteAvatarCache($contact['photo']); + self::deleteAvatarCache($contact['thumb']); + self::deleteAvatarCache($contact['micro']); + } elseif (self::getAvatarFile($contact['photo']) && self::getAvatarFile($contact['thumb']) && self::getAvatarFile($contact['micro'])) { + $fields['photo'] = $contact['photo']; + $fields['thumb'] = $contact['thumb']; + $fields['thumb'] = $contact['thumb']; + return $fields; + } + + $guid = Item::guidFromUri($contact['url'], parse_url($contact['url'], PHP_URL_HOST)); + + $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . + substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; + + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]); + $img_str = $fetchResult->getBody(); + if (empty($img_str)) { + return $fields; + } + + $image = new Image($img_str, Images::getMimeTypeByData($img_str)); + if (!$image->isValid()) { + return $fields; + } + + $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL); + $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); + $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); + + Logger::debug('Storing avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); + + return $fields; + } + + private static function storeAvatarCache(Image $image, string $filename, int $size): string + { + $image->scaleDown($size); + if (is_null($image) || !$image->isValid()) { + return ''; + } + + $path = '/avatar/' . $filename . $size . '.' . $image->getExt(); + + $filepath = DI::basePath() . $path; + + $dirpath = dirname($filepath); + + DI::profiler()->startRecording('file'); + + if (!file_exists($dirpath)) { + mkdir($dirpath, 0777, true); + } + + file_put_contents($filepath, $image->asString()); + DI::profiler()->stopRecording(); + + return DI::baseUrl() . $path; + } + + /** + * Fetch the name of locally cached avatar pictures + * + * @param string $avatar + * @return string + */ + private static function getAvatarFile(string $avatar): string + { + if (empty($avatar) || !Network::isLocalLink($avatar)) { + return ''; + } + + $path = Strings::normaliseLink(DI::baseUrl() . '/avatar'); + + if (Network::getUrlMatch($path, $avatar) != $path) { + return ''; + } + + $filename = str_replace($path, DI::basePath(). '/avatar/', Strings::normaliseLink($avatar)); + + DI::profiler()->startRecording('file'); + $exists = file_exists($filename); + DI::profiler()->stopRecording(); + + if (!$exists) { + return ''; + } + return $filename; + } + + /** + * Delete a locally cached avatar picture + * + * @param string $avatar + * @return void + */ + public static function deleteAvatarCache(string $avatar) + { + $localFile = self::getAvatarFile($avatar); + if (!empty($localFile)) { + unlink($localFile); + Logger::debug('Unlink avatar', ['avatar' => $avatar]); + } + } + public static function deleteContactByUrl(string $url) { // Update contact data for all users @@ -2789,7 +2951,7 @@ class Contact return; } - $cdata = Contact::getPublicAndUserContactID($contact['id'], $contact['uid']); + $cdata = self::getPublicAndUserContactID($contact['id'], $contact['uid']); DI::notification()->deleteForUserByVerb($contact['uid'], Activity::FOLLOW, ['actor-id' => $cdata['public']]); } diff --git a/src/Worker/RemoveUnusedContacts.php b/src/Worker/RemoveUnusedContacts.php index 40a94baaf5..b656eb8259 100644 --- a/src/Worker/RemoveUnusedContacts.php +++ b/src/Worker/RemoveUnusedContacts.php @@ -25,6 +25,7 @@ use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\Database\DBStructure; +use Friendica\Model\Contact; use Friendica\Model\Photo; use Friendica\Util\DateTimeFormat; @@ -47,37 +48,40 @@ class RemoveUnusedContacts $total = DBA::count('contact', $condition); Logger::notice('Starting removal', ['total' => $total]); $count = 0; - $contacts = DBA::select('contact', ['id', 'uid'], $condition); + $contacts = DBA::select('contact', ['id', 'uid', 'photo', 'thumb', 'micro'], $condition); while ($contact = DBA::fetch($contacts)) { - if (Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']])) { - if (DBStructure::existsTable('thread')) { - DBA::delete('thread', ['owner-id' => $contact['id']]); - DBA::delete('thread', ['author-id' => $contact['id']]); - } - if (DBStructure::existsTable('item')) { - DBA::delete('item', ['owner-id' => $contact['id']]); - DBA::delete('item', ['author-id' => $contact['id']]); - DBA::delete('item', ['causer-id' => $contact['id']]); - } + Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']]); + Contact::deleteAvatarCache($contact['photo']); + Contact::deleteAvatarCache($contact['thumb']); + Contact::deleteAvatarCache($contact['micro']); - // There should be none entry for the contact in these tables when none was found in "post-user". - // But we want to be sure since the foreign key prohibits deletion otherwise. - DBA::delete('post', ['owner-id' => $contact['id']]); - DBA::delete('post', ['author-id' => $contact['id']]); - DBA::delete('post', ['causer-id' => $contact['id']]); - - DBA::delete('post-thread', ['owner-id' => $contact['id']]); - DBA::delete('post-thread', ['author-id' => $contact['id']]); - DBA::delete('post-thread', ['causer-id' => $contact['id']]); + if (DBStructure::existsTable('thread')) { + DBA::delete('thread', ['owner-id' => $contact['id']]); + DBA::delete('thread', ['author-id' => $contact['id']]); + } + if (DBStructure::existsTable('item')) { + DBA::delete('item', ['owner-id' => $contact['id']]); + DBA::delete('item', ['author-id' => $contact['id']]); + DBA::delete('item', ['causer-id' => $contact['id']]); + } - DBA::delete('post-thread-user', ['owner-id' => $contact['id']]); - DBA::delete('post-thread-user', ['author-id' => $contact['id']]); - DBA::delete('post-thread-user', ['causer-id' => $contact['id']]); + // There should be none entry for the contact in these tables when none was found in "post-user". + // But we want to be sure since the foreign key prohibits deletion otherwise. + DBA::delete('post', ['owner-id' => $contact['id']]); + DBA::delete('post', ['author-id' => $contact['id']]); + DBA::delete('post', ['causer-id' => $contact['id']]); + + DBA::delete('post-thread', ['owner-id' => $contact['id']]); + DBA::delete('post-thread', ['author-id' => $contact['id']]); + DBA::delete('post-thread', ['causer-id' => $contact['id']]); - DBA::delete('contact', ['id' => $contact['id']]); - if ((++$count % 1000) == 0) { - Logger::notice('In removal', ['count' => $count, 'total' => $total]); - } + DBA::delete('post-thread-user', ['owner-id' => $contact['id']]); + DBA::delete('post-thread-user', ['author-id' => $contact['id']]); + DBA::delete('post-thread-user', ['causer-id' => $contact['id']]); + + DBA::delete('contact', ['id' => $contact['id']]); + if ((++$count % 1000) == 0) { + Logger::notice('In removal', ['count' => $count, 'total' => $total]); } } DBA::close($contacts); diff --git a/static/defaults.config.php b/static/defaults.config.php index 336916ceff..959315f888 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -118,6 +118,10 @@ return [ // chose "Remember me" when logging in is considered logged out. 'auth_cookie_lifetime' => 7, + // avatar_cache (Boolean) + // Cache avatar pictures as files (experimental) + 'avatar_cache' => false, + // big_emojis (Boolean) // Display "Emoji Only" posts in big. 'big_emojis' => false, From 2d856f48b71d5929a389dbf8e2c52a84d4433f11 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 May 2022 05:51:18 +0000 Subject: [PATCH 02/14] Use the default timeout --- src/Model/Contact.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index f034ca69ad..834988043d 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2102,7 +2102,7 @@ class Contact $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; - $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]); + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); $img_str = $fetchResult->getBody(); if (empty($img_str)) { return $fields; From 7b0187c4c576cc6c79582a5854f7da56d957ea08 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 May 2022 08:53:18 +0000 Subject: [PATCH 03/14] Don't overwrite already replaced cache file paths --- src/Model/Contact.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 834988043d..eb065ce8db 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1587,8 +1587,8 @@ class Contact self::updateAvatar($cid, $contact['avatar'], true); return; } - } elseif (!empty($contact['photo']) || !empty($contact['thumb']) || !empty($contact['micro'])) { - Logger::info('Removing avatar cache', ['id' => $cid, 'contact' => $contact]); + } elseif (!self::getAvatarFile($contact['photo']) || !self::getAvatarFile($contact['thumb']) || !self::getAvatarFile($contact['micro'])) { + Logger::info('Removing/replacing avatar cache', ['id' => $cid, 'contact' => $contact]); self::updateAvatar($cid, $contact['avatar'], true); return; } @@ -2090,10 +2090,12 @@ class Contact self::deleteAvatarCache($contact['photo']); self::deleteAvatarCache($contact['thumb']); self::deleteAvatarCache($contact['micro']); + Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); } elseif (self::getAvatarFile($contact['photo']) && self::getAvatarFile($contact['thumb']) && self::getAvatarFile($contact['micro'])) { $fields['photo'] = $contact['photo']; $fields['thumb'] = $contact['thumb']; $fields['thumb'] = $contact['thumb']; + Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); return $fields; } @@ -2105,11 +2107,13 @@ class Contact $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); $img_str = $fetchResult->getBody(); if (empty($img_str)) { + Logger::debug('Avatar is invalid', ['avatar' => $avatar]); return $fields; } $image = new Image($img_str, Images::getMimeTypeByData($img_str)); if (!$image->isValid()) { + Logger::debug('Avatar picture is invalid', ['avatar' => $avatar]); return $fields; } @@ -2117,7 +2121,7 @@ class Contact $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); - Logger::debug('Storing avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); + Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); return $fields; } From dd422a1e03c2faa8a480a09b740d477167e04e3d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 May 2022 08:57:41 +0000 Subject: [PATCH 04/14] Fix contact field name --- src/Model/Contact.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index eb065ce8db..25af61dece 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2094,7 +2094,7 @@ class Contact } elseif (self::getAvatarFile($contact['photo']) && self::getAvatarFile($contact['thumb']) && self::getAvatarFile($contact['micro'])) { $fields['photo'] = $contact['photo']; $fields['thumb'] = $contact['thumb']; - $fields['thumb'] = $contact['thumb']; + $fields['micro'] = $contact['micro']; Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); return $fields; } From 805dc8e6bdc0088caf4509d546365c203d528cb5 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 May 2022 09:34:30 +0000 Subject: [PATCH 05/14] Fix config call --- src/Model/Contact.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 25af61dece..c00c6b2e8a 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1608,7 +1608,7 @@ class Contact { $contact = self::checkAvatarCacheByArray($contact, $no_update); - if (!DI::config()->get('system', 'avatar_cache')) { + if (DI::config()->get('system', 'avatar_cache')) { switch ($size) { case Proxy::SIZE_MICRO: if (self::getAvatarFile($contact['micro'])) { From f220e26f0050463520b2cefefd0ccfedbdb84529 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 May 2022 10:32:29 +0000 Subject: [PATCH 06/14] Use new function to check for a local avatar cache file --- src/Content/Conversation.php | 15 +++++++++++++-- src/Model/Contact.php | 21 ++++++++++++++++----- src/Object/Post.php | 19 +++++++++++++++---- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/Content/Conversation.php b/src/Content/Conversation.php index 4ad1890a26..fdb1f51d89 100644 --- a/src/Content/Conversation.php +++ b/src/Content/Conversation.php @@ -662,11 +662,22 @@ class Conversation if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) { $owner_avatar = $author_avatar = $item['contact-id']; $owner_updated = $author_updated = ''; + $owner_thumb = $author_thumb = $item['contact-avatar']; } else { $owner_avatar = $item['owner-id']; $owner_updated = $item['owner-updated']; + $owner_thumb = $item['owner-avatar']; $author_avatar = $item['author-id']; $author_updated = $item['author-updated']; + $author_thumb = $item['author-avatar']; + } + + if (!Contact::isAvatarFile($owner_thumb)) { + $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); + } + + if (!Contact::isAvatarFile($author_thumb)) { + $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); } $tmp_item = [ @@ -686,7 +697,7 @@ class Conversation 'name' => $profile_name, 'sparkle' => $sparkle, 'lock' => false, - 'thumb' => $this->baseURL->remove(Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated)), + 'thumb' => $this->baseURL->remove($author_thumb), 'title' => $title, 'body_html' => $body_html, 'tags' => $tags['tags'], @@ -707,7 +718,7 @@ class Conversation 'indent' => '', 'owner_name' => '', 'owner_url' => '', - 'owner_photo' => $this->baseURL->remove(Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated)), + 'owner_photo' => $this->baseURL->remove($owner_thumb), 'plink' => ItemModel::getPlink($item), 'edpost' => false, 'pinned' => $pinned, diff --git a/src/Model/Contact.php b/src/Model/Contact.php index c00c6b2e8a..f16a2e9c8a 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1587,7 +1587,7 @@ class Contact self::updateAvatar($cid, $contact['avatar'], true); return; } - } elseif (!self::getAvatarFile($contact['photo']) || !self::getAvatarFile($contact['thumb']) || !self::getAvatarFile($contact['micro'])) { + } elseif (!self::isAvatarFile($contact['photo']) || !self::isAvatarFile($contact['thumb']) || !self::isAvatarFile($contact['micro'])) { Logger::info('Removing/replacing avatar cache', ['id' => $cid, 'contact' => $contact]); self::updateAvatar($cid, $contact['avatar'], true); return; @@ -1611,17 +1611,17 @@ class Contact if (DI::config()->get('system', 'avatar_cache')) { switch ($size) { case Proxy::SIZE_MICRO: - if (self::getAvatarFile($contact['micro'])) { + if (self::isAvatarFile($contact['micro'])) { return $contact['micro']; } break; case Proxy::SIZE_THUMB: - if (self::getAvatarFile($contact['thumb'])) { + if (self::isAvatarFile($contact['thumb'])) { return $contact['thumb']; } break; case Proxy::SIZE_SMALL: - if (self::getAvatarFile($contact['photo'])) { + if (self::isAvatarFile($contact['photo'])) { return $contact['photo']; } break; @@ -2091,7 +2091,7 @@ class Contact self::deleteAvatarCache($contact['thumb']); self::deleteAvatarCache($contact['micro']); Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); - } elseif (self::getAvatarFile($contact['photo']) && self::getAvatarFile($contact['thumb']) && self::getAvatarFile($contact['micro'])) { + } elseif (self::isAvatarFile($contact['photo']) && self::isAvatarFile($contact['thumb']) && self::isAvatarFile($contact['micro'])) { $fields['photo'] = $contact['photo']; $fields['thumb'] = $contact['thumb']; $fields['micro'] = $contact['micro']; @@ -2181,6 +2181,17 @@ class Contact return $filename; } + /** + * Check if the avatar cache file is locally stored + * + * @param string $avatar + * @return boolean + */ + public static function isAvatarFile(string $avatar): bool + { + return !empty(self::getAvatarFile($avatar)); + } + /** * Delete a locally cached avatar picture * diff --git a/src/Object/Post.php b/src/Object/Post.php index a949c24aa7..0a7bea91bd 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -123,8 +123,8 @@ class Post /** * Fetch the privacy of the post * - * @param array $item - * @return string + * @param array $item + * @return string */ private function fetchPrivacy(array $item):string { @@ -453,11 +453,22 @@ class Post if (in_array($item['network'], [Protocol::FEED, Protocol::MAIL])) { $owner_avatar = $author_avatar = $item['contact-id']; $owner_updated = $author_updated = ''; + $owner_thumb = $author_thumb = $item['contact-avatar']; } else { $owner_avatar = $item['owner-id']; $owner_updated = $item['owner-updated']; + $owner_thumb = $item['owner-avatar']; $author_avatar = $item['author-id']; $author_updated = $item['author-updated']; + $author_thumb = $item['author-avatar']; + } + + if (!Contact::isAvatarFile($owner_thumb)) { + $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); + } + + if (!Contact::isAvatarFile($author_thumb)) { + $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); } $tmp_item = [ @@ -491,7 +502,7 @@ class Post 'profile_url' => $profile_link, 'name' => $profile_name, 'item_photo_menu_html' => DI::contentItem()->photoMenu($item, $formSecurityToken), - 'thumb' => DI::baseUrl()->remove(Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated)), + 'thumb' => DI::baseUrl()->remove($author_thumb), 'osparkle' => $osparkle, 'sparkle' => $sparkle, 'title' => $title, @@ -508,7 +519,7 @@ class Post 'shiny' => $shiny, 'owner_self' => $item['author-link'] == Session::get('my_url'), 'owner_url' => $this->getOwnerUrl(), - 'owner_photo' => DI::baseUrl()->remove(Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated)), + 'owner_photo' => DI::baseUrl()->remove($owner_thumb), 'owner_name' => $this->getOwnerName(), 'plink' => Item::getPlink($item), 'browsershare' => $browsershare, From e3692c01053d4714292449d1170ef2d60ad575d7 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 04:26:00 +0000 Subject: [PATCH 07/14] Replaced check with hardwired path --- src/Content/Conversation.php | 7 ++++--- src/Model/Contact.php | 8 ++++---- src/Model/Photo.php | 12 ++++++++++++ src/Object/Post.php | 5 +++-- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Content/Conversation.php b/src/Content/Conversation.php index fdb1f51d89..a86c7f5081 100644 --- a/src/Content/Conversation.php +++ b/src/Content/Conversation.php @@ -37,6 +37,7 @@ use Friendica\Core\Theme; use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Model\Item as ItemModel; +use Friendica\Model\Photo; use Friendica\Model\Post; use Friendica\Model\Tag; use Friendica\Model\User; @@ -672,11 +673,11 @@ class Conversation $author_thumb = $item['author-avatar']; } - if (!Contact::isAvatarFile($owner_thumb)) { + if (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) { $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); } - - if (!Contact::isAvatarFile($author_thumb)) { + + if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) { $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index f16a2e9c8a..ca286ab587 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1611,17 +1611,17 @@ class Contact if (DI::config()->get('system', 'avatar_cache')) { switch ($size) { case Proxy::SIZE_MICRO: - if (self::isAvatarFile($contact['micro'])) { + if (!empty($contact['micro']) && !Photo::isPhotoURI($contact['micro'])) { return $contact['micro']; } break; case Proxy::SIZE_THUMB: - if (self::isAvatarFile($contact['thumb'])) { + if (!empty($contact['thumb']) && !Photo::isPhotoURI($contact['thumb'])) { return $contact['thumb']; } break; case Proxy::SIZE_SMALL: - if (self::isAvatarFile($contact['photo'])) { + if (!empty($contact['photo']) && !Photo::isPhotoURI($contact['photo'])) { return $contact['photo']; } break; @@ -2187,7 +2187,7 @@ class Contact * @param string $avatar * @return boolean */ - public static function isAvatarFile(string $avatar): bool + private static function isAvatarFile(string $avatar): bool { return !empty(self::getAvatarFile($avatar)); } diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 8583577890..6537c41d1f 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -710,6 +710,18 @@ class Photo return $image_uri; } + /** + * Checks if the given URL is a local photo. + * Since it is meant for time critical occasions, the check is done without any database requests. + * + * @param string $url + * @return boolean + */ + public static function isPhotoURI(string $url): bool + { + return !empty(self::ridFromURI($url)); + } + /** * Changes photo permissions that had been embedded in a post * diff --git a/src/Object/Post.php b/src/Object/Post.php index 0a7bea91bd..3c01028292 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -32,6 +32,7 @@ use Friendica\Core\Session; use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Item; +use Friendica\Model\Photo; use Friendica\Model\Post as PostModel; use Friendica\Model\Tag; use Friendica\Model\User; @@ -463,11 +464,11 @@ class Post $author_thumb = $item['author-avatar']; } - if (!Contact::isAvatarFile($owner_thumb)) { + if (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) { $owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated); } - if (!Contact::isAvatarFile($author_thumb)) { + if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) { $author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated); } From 53d064c28382b2b444495a78aa34eead345fe446 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 06:27:46 +0000 Subject: [PATCH 08/14] Avatar handling is moved to a separate class --- src/Contact/Contact.php | 193 ++++++++++++++++++++++++++++ src/Model/Contact.php | 161 +---------------------- src/Worker/RemoveUnusedContacts.php | 5 +- 3 files changed, 200 insertions(+), 159 deletions(-) create mode 100644 src/Contact/Contact.php diff --git a/src/Contact/Contact.php b/src/Contact/Contact.php new file mode 100644 index 0000000000..d452096e40 --- /dev/null +++ b/src/Contact/Contact.php @@ -0,0 +1,193 @@ +. + * + */ + +namespace Friendica\Contact; + +use Friendica\Core\Logger; +use Friendica\DI; +use Friendica\Model\Item; +use Friendica\Network\HTTPClient\Client\HttpClientAccept; +use Friendica\Network\HTTPClient\Client\HttpClientOptions; +use Friendica\Object\Image; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\HTTPSignature; +use Friendica\Util\Images; +use Friendica\Util\Network; +use Friendica\Util\Proxy; +use Friendica\Util\Strings; + +/** + * functions for handling contact avatar caching + */ +class Avatar +{ + /** + * Returns a field array with locally cached avatar pictures + * + * @param array $contact + * @param string $avatar + * @return array + */ + public static function fetchAvatarContact(array $contact, string $avatar): array + { + $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => '']; + + if (!DI::config()->get('system', 'avatar_cache')) { + self::deleteCache($contact); + return $fields; + } + + if (Network::isLocalLink($avatar)) { + return $fields; + } + + if ($avatar != $contact['avatar']) { + self::deleteCache($contact); + Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); + } elseif (self::isCacheFile($contact['photo']) && self::isCacheFile($contact['thumb']) && self::isCacheFile($contact['micro'])) { + $fields['photo'] = $contact['photo']; + $fields['thumb'] = $contact['thumb']; + $fields['micro'] = $contact['micro']; + Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); + return $fields; + } + + $guid = Item::guidFromUri($contact['url'], parse_url($contact['url'], PHP_URL_HOST)); + + $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . + substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; + + $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + $img_str = $fetchResult->getBody(); + if (empty($img_str)) { + Logger::debug('Avatar is invalid', ['avatar' => $avatar]); + return $fields; + } + + $image = new Image($img_str, Images::getMimeTypeByData($img_str)); + if (!$image->isValid()) { + Logger::debug('Avatar picture is invalid', ['avatar' => $avatar]); + return $fields; + } + + $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL); + $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); + $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); + + Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); + + return $fields; + } + + private static function storeAvatarCache(Image $image, string $filename, int $size): string + { + $image->scaleDown($size); + if (is_null($image) || !$image->isValid()) { + return ''; + } + + $path = '/avatar/' . $filename . $size . '.' . $image->getExt(); + + $filepath = DI::basePath() . $path; + + $dirpath = dirname($filepath); + + DI::profiler()->startRecording('file'); + + if (!file_exists($dirpath)) { + mkdir($dirpath, 0777, true); + } + + file_put_contents($filepath, $image->asString()); + DI::profiler()->stopRecording(); + + return DI::baseUrl() . $path; + } + + /** + * Check if the avatar cache file is locally stored + * + * @param string $avatar + * @return boolean + */ + public static function isCacheFile(string $avatar): bool + { + return !empty(self::getCacheFile($avatar)); + } + + /** + * Fetch the name of locally cached avatar pictures + * + * @param string $avatar + * @return string + */ + private static function getCacheFile(string $avatar): string + { + if (empty($avatar) || !Network::isLocalLink($avatar)) { + return ''; + } + + $path = Strings::normaliseLink(DI::baseUrl() . '/avatar'); + + if (Network::getUrlMatch($path, $avatar) != $path) { + return ''; + } + + $filename = str_replace($path, DI::basePath(). '/avatar/', Strings::normaliseLink($avatar)); + + DI::profiler()->startRecording('file'); + $exists = file_exists($filename); + DI::profiler()->stopRecording(); + + if (!$exists) { + return ''; + } + return $filename; + } + + /** + * Delete locally cached avatar pictures of a contact + * + * @param string $avatar + * @return void + */ + public static function deleteCache(array $contact) + { + self::deleteCacheFile($contact['photo']); + self::deleteCacheFile($contact['thumb']); + self::deleteCacheFile($contact['micro']); + } + + /** + * Delete a locally cached avatar picture + * + * @param string $avatar + * @return void + */ + private static function deleteCacheFile(string $avatar) + { + $localFile = self::getCacheFile($avatar); + if (!empty($localFile)) { + unlink($localFile); + Logger::debug('Unlink avatar', ['avatar' => $avatar]); + } + } +} diff --git a/src/Model/Contact.php b/src/Model/Contact.php index ca286ab587..c83bf1f416 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -21,7 +21,7 @@ namespace Friendica\Model; -use Friendica\App\BaseURL; +use Friendica\Contact\Avatar; use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException; use Friendica\Content\Pager; use Friendica\Content\Text\HTML; @@ -35,15 +35,11 @@ use Friendica\Core\Worker; 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\Network\HTTPException; use Friendica\Network\Probe; -use Friendica\Object\Image; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use Friendica\Util\DateTimeFormat; -use Friendica\Util\HTTPSignature; use Friendica\Util\Images; use Friendica\Util\Network; use Friendica\Util\Proxy; @@ -810,9 +806,7 @@ class Contact self::update(['archive' => true, 'network' => Protocol::PHANTOM, 'deleted' => true], ['id' => $id]); if (!DBA::exists('contact', ['uri-id' => $contact['uri-id'], 'deleted' => false])) { - self::deleteAvatarCache($contact['photo']); - self::deleteAvatarCache($contact['thumb']); - self::deleteAvatarCache($contact['micro']); + Avatar::deleteCache($contact); } // Delete it in the background @@ -1587,7 +1581,7 @@ class Contact self::updateAvatar($cid, $contact['avatar'], true); return; } - } elseif (!self::isAvatarFile($contact['photo']) || !self::isAvatarFile($contact['thumb']) || !self::isAvatarFile($contact['micro'])) { + } elseif (!Avatar::isCacheFile($contact['photo']) || !Avatar::isCacheFile($contact['thumb']) || !Avatar::isCacheFile($contact['micro'])) { Logger::info('Removing/replacing avatar cache', ['id' => $cid, 'contact' => $contact]); self::updateAvatar($cid, $contact['avatar'], true); return; @@ -1977,9 +1971,7 @@ class Contact } if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) { - self::deleteAvatarCache($contact['photo']); - self::deleteAvatarCache($contact['thumb']); - self::deleteAvatarCache($contact['micro']); + Avatar::deleteCache($contact); if ($default_avatar && Proxy::isLocalImage($avatar)) { $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), @@ -2032,7 +2024,7 @@ class Contact } } else { Photo::delete(['uid' => $uid, 'contact-id' => $cid, 'photo-type' => Photo::CONTACT_AVATAR]); - $fields = self::fetchAvatarContact($contact, $avatar); + $fields = Avatar::fetchAvatarContact($contact, $avatar); $update = ($avatar . $fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force; } @@ -2064,149 +2056,6 @@ class Contact self::update($fields, ['id' => $cids]); } - /** - * Returns a field array with locally cached avatar pictures - * - * @param array $contact - * @param string $avatar - * @return array - */ - private static function fetchAvatarContact(array $contact, string $avatar): array - { - $fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(), 'photo' => '', 'thumb' => '', 'micro' => '']; - - if (!DI::config()->get('system', 'avatar_cache')) { - self::deleteAvatarCache($contact['photo']); - self::deleteAvatarCache($contact['thumb']); - self::deleteAvatarCache($contact['micro']); - return $fields; - } - - if (Network::isLocalLink($avatar)) { - return $fields; - } - - if ($avatar != $contact['avatar']) { - self::deleteAvatarCache($contact['photo']); - self::deleteAvatarCache($contact['thumb']); - self::deleteAvatarCache($contact['micro']); - Logger::debug('Avatar file name changed', ['new' => $avatar, 'old' => $contact['avatar']]); - } elseif (self::isAvatarFile($contact['photo']) && self::isAvatarFile($contact['thumb']) && self::isAvatarFile($contact['micro'])) { - $fields['photo'] = $contact['photo']; - $fields['thumb'] = $contact['thumb']; - $fields['micro'] = $contact['micro']; - Logger::debug('Using existing cache files', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); - return $fields; - } - - $guid = Item::guidFromUri($contact['url'], parse_url($contact['url'], PHP_URL_HOST)); - - $filename = substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' . - substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; - - $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); - $img_str = $fetchResult->getBody(); - if (empty($img_str)) { - Logger::debug('Avatar is invalid', ['avatar' => $avatar]); - return $fields; - } - - $image = new Image($img_str, Images::getMimeTypeByData($img_str)); - if (!$image->isValid()) { - Logger::debug('Avatar picture is invalid', ['avatar' => $avatar]); - return $fields; - } - - $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL); - $fields['thumb'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_THUMB); - $fields['micro'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_MICRO); - - Logger::debug('Storing new avatar cache', ['uri-id' => $contact['uri-id'], 'fields' => $fields]); - - return $fields; - } - - private static function storeAvatarCache(Image $image, string $filename, int $size): string - { - $image->scaleDown($size); - if (is_null($image) || !$image->isValid()) { - return ''; - } - - $path = '/avatar/' . $filename . $size . '.' . $image->getExt(); - - $filepath = DI::basePath() . $path; - - $dirpath = dirname($filepath); - - DI::profiler()->startRecording('file'); - - if (!file_exists($dirpath)) { - mkdir($dirpath, 0777, true); - } - - file_put_contents($filepath, $image->asString()); - DI::profiler()->stopRecording(); - - return DI::baseUrl() . $path; - } - - /** - * Fetch the name of locally cached avatar pictures - * - * @param string $avatar - * @return string - */ - private static function getAvatarFile(string $avatar): string - { - if (empty($avatar) || !Network::isLocalLink($avatar)) { - return ''; - } - - $path = Strings::normaliseLink(DI::baseUrl() . '/avatar'); - - if (Network::getUrlMatch($path, $avatar) != $path) { - return ''; - } - - $filename = str_replace($path, DI::basePath(). '/avatar/', Strings::normaliseLink($avatar)); - - DI::profiler()->startRecording('file'); - $exists = file_exists($filename); - DI::profiler()->stopRecording(); - - if (!$exists) { - return ''; - } - return $filename; - } - - /** - * Check if the avatar cache file is locally stored - * - * @param string $avatar - * @return boolean - */ - private static function isAvatarFile(string $avatar): bool - { - return !empty(self::getAvatarFile($avatar)); - } - - /** - * Delete a locally cached avatar picture - * - * @param string $avatar - * @return void - */ - public static function deleteAvatarCache(string $avatar) - { - $localFile = self::getAvatarFile($avatar); - if (!empty($localFile)) { - unlink($localFile); - Logger::debug('Unlink avatar', ['avatar' => $avatar]); - } - } - public static function deleteContactByUrl(string $url) { // Update contact data for all users diff --git a/src/Worker/RemoveUnusedContacts.php b/src/Worker/RemoveUnusedContacts.php index b656eb8259..850760254a 100644 --- a/src/Worker/RemoveUnusedContacts.php +++ b/src/Worker/RemoveUnusedContacts.php @@ -21,6 +21,7 @@ namespace Friendica\Worker; +use Friendica\Contact\Avatar; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; @@ -51,9 +52,7 @@ class RemoveUnusedContacts $contacts = DBA::select('contact', ['id', 'uid', 'photo', 'thumb', 'micro'], $condition); while ($contact = DBA::fetch($contacts)) { Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']]); - Contact::deleteAvatarCache($contact['photo']); - Contact::deleteAvatarCache($contact['thumb']); - Contact::deleteAvatarCache($contact['micro']); + Avatar::deleteCache($contact); if (DBStructure::existsTable('thread')) { DBA::delete('thread', ['owner-id' => $contact['id']]); From 681d19a3bca8e0108071d86f9fbc405525479ad5 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 06:31:09 +0000 Subject: [PATCH 09/14] Standards and renamed class --- src/Contact/{Contact.php => Avatar.php} | 1 + 1 file changed, 1 insertion(+) rename src/Contact/{Contact.php => Avatar.php} (99%) diff --git a/src/Contact/Contact.php b/src/Contact/Avatar.php similarity index 99% rename from src/Contact/Contact.php rename to src/Contact/Avatar.php index d452096e40..f4e5b71afe 100644 --- a/src/Contact/Contact.php +++ b/src/Contact/Avatar.php @@ -76,6 +76,7 @@ class Avatar substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-'; $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); + $img_str = $fetchResult->getBody(); if (empty($img_str)) { Logger::debug('Avatar is invalid', ['avatar' => $avatar]); From 006b7a95f0d7a789556603848d3bbac0a666fcde Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 06:57:47 +0000 Subject: [PATCH 10/14] More avatar handling isolation --- src/Contact/Avatar.php | 2 +- src/Model/Contact.php | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index f4e5b71afe..18f63b07b2 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -129,7 +129,7 @@ class Avatar * @param string $avatar * @return boolean */ - public static function isCacheFile(string $avatar): bool + private static function isCacheFile(string $avatar): bool { return !empty(self::getCacheFile($avatar)); } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index c83bf1f416..6a98286994 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1581,10 +1581,14 @@ class Contact self::updateAvatar($cid, $contact['avatar'], true); return; } - } elseif (!Avatar::isCacheFile($contact['photo']) || !Avatar::isCacheFile($contact['thumb']) || !Avatar::isCacheFile($contact['micro'])) { - Logger::info('Removing/replacing avatar cache', ['id' => $cid, 'contact' => $contact]); + } elseif (Photo::isPhotoURI($contact['photo']) || Photo::isPhotoURI($contact['thumb']) || Photo::isPhotoURI($contact['micro'])) { + Logger::info('Replacing legacy avatar cache', ['id' => $cid, 'contact' => $contact]); self::updateAvatar($cid, $contact['avatar'], true); return; + } elseif (DI::config()->get('system', 'avatar_cache') && (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']))) { + Logger::info('Adding avatar cache file', ['id' => $cid, 'contact' => $contact]); + self::updateAvatar($cid, $contact['avatar'], true); + return; } } From 9d1ff0a4cee01232be08e0b25727c4dacbd841c2 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 08:20:09 +0000 Subject: [PATCH 11/14] Set permissions --- src/Contact/Avatar.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 18f63b07b2..e0c0f4970d 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -114,10 +114,14 @@ class Avatar DI::profiler()->startRecording('file'); if (!file_exists($dirpath)) { - mkdir($dirpath, 0777, true); + mkdir($dirpath, 0775, true); + } else { + chmod($dirpath, 0775); } file_put_contents($filepath, $image->asString()); + chmod($filepath, 0775); + DI::profiler()->stopRecording(); return DI::baseUrl() . $path; From f744dd362de295c17a6d51d2277c69721ec92a01 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 08:28:23 +0000 Subject: [PATCH 12/14] Picture shouldn't be executable --- src/Contact/Avatar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index e0c0f4970d..4a54833b0c 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -120,7 +120,7 @@ class Avatar } file_put_contents($filepath, $image->asString()); - chmod($filepath, 0775); + chmod($filepath, 0664); DI::profiler()->stopRecording(); From f785026289f486921cfa6896b1da1f9255d27de3 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 08:47:02 +0000 Subject: [PATCH 13/14] Don't return a filename when it wasn't stored --- src/Contact/Avatar.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 4a54833b0c..6114788021 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -124,6 +124,11 @@ class Avatar DI::profiler()->stopRecording(); + if (!file_exists($filepath)) { + Logger::notice('Avatar cache file could not be stored', ['file' => $filepath]); + return ''; + } + return DI::baseUrl() . $path; } From cbe4a42906dc40f893825b4d2abf2102039afca0 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 9 May 2022 14:36:41 +0000 Subject: [PATCH 14/14] Use a warning instead --- src/Contact/Avatar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 6114788021..13a99f9106 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -125,7 +125,7 @@ class Avatar DI::profiler()->stopRecording(); if (!file_exists($filepath)) { - Logger::notice('Avatar cache file could not be stored', ['file' => $filepath]); + Logger::warning('Avatar cache file could not be stored', ['file' => $filepath]); return ''; }