Merge pull request #11477 from annando/avatar-file-cache

Cache contact avatars locally as files
This commit is contained in:
Hypolite Petovan 2022-05-09 11:13:08 -04:00 committed by GitHub
commit e1f32f7f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 328 additions and 48 deletions

6
.gitignore vendored
View File

@ -85,4 +85,8 @@ venv/
/bin/phpunit
#Ignore cache file
.php_cs.cache
.php_cs.cache
#ignore avatar picture cache path
/avatar

203
src/Contact/Avatar.php Normal file
View File

@ -0,0 +1,203 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
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, 0775, true);
} else {
chmod($dirpath, 0775);
}
file_put_contents($filepath, $image->asString());
chmod($filepath, 0664);
DI::profiler()->stopRecording();
if (!file_exists($filepath)) {
Logger::warning('Avatar cache file could not be stored', ['file' => $filepath]);
return '';
}
return DI::baseUrl() . $path;
}
/**
* Check if the avatar cache file is locally stored
*
* @param string $avatar
* @return boolean
*/
private 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]);
}
}
}

View File

@ -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;
@ -662,11 +663,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 (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) {
$owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated);
}
if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) {
$author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated);
}
$tmp_item = [
@ -686,7 +698,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 +719,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,

View File

@ -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;
@ -797,7 +797,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 +805,10 @@ 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])) {
Avatar::deleteCache($contact);
}
// Delete it in the background
Worker::add(PRIORITY_MEDIUM, 'Contact\Remove', $id);
}
@ -827,7 +831,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 +860,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 +886,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 +1468,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 +1481,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];
@ -1577,10 +1581,14 @@ 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 (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;
}
}
@ -1597,6 +1605,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 (!empty($contact['micro']) && !Photo::isPhotoURI($contact['micro'])) {
return $contact['micro'];
}
break;
case Proxy::SIZE_THUMB:
if (!empty($contact['thumb']) && !Photo::isPhotoURI($contact['thumb'])) {
return $contact['thumb'];
}
break;
case Proxy::SIZE_SMALL:
if (!empty($contact['photo']) && !Photo::isPhotoURI($contact['photo'])) {
return $contact['photo'];
}
break;
}
}
return self::getAvatarUrlForId($contact['id'], $size, $contact['updated'] ?? '');
}
@ -1905,7 +1934,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 +1975,8 @@ class Contact
}
if (in_array($contact['network'], [Protocol::FEED, Protocol::MAIL]) || $cache_avatar) {
Avatar::deleteCache($contact);
if ($default_avatar && Proxy::isLocalImage($avatar)) {
$fields = ['avatar' => $avatar, 'avatar-date' => DateTimeFormat::utcNow(),
'photo' => $avatar,
@ -1997,9 +2028,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 = Avatar::fetchAvatarContact($contact, $avatar);
$update = ($avatar . $fields['photo'] . $fields['thumb'] . $fields['micro'] != $contact['avatar'] . $contact['photo'] . $contact['thumb'] . $contact['micro']) || $force;
}
if (!$update) {
@ -2789,7 +2819,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']]);
}

View File

@ -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
*

View File

@ -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;
@ -123,8 +124,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 +454,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 (empty($owner_thumb) || Photo::isPhotoURI($owner_thumb)) {
$owner_thumb = Contact::getAvatarUrlForId($owner_avatar, Proxy::SIZE_THUMB, $owner_updated);
}
if (empty($author_thumb) || Photo::isPhotoURI($author_thumb)) {
$author_thumb = Contact::getAvatarUrlForId($author_avatar, Proxy::SIZE_THUMB, $author_updated);
}
$tmp_item = [
@ -491,7 +503,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 +520,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,

View File

@ -21,10 +21,12 @@
namespace Friendica\Worker;
use Friendica\Contact\Avatar;
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 +49,38 @@ 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']]);
Avatar::deleteCache($contact);
// 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);

View File

@ -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,