Merge pull request #14301 from annando/ckearavatarcache

New console command to clear the avatar cache
This commit is contained in:
Tobias Diekershoff 2024-07-19 07:30:54 +02:00 committed by GitHub
commit fb3021e4ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 136 additions and 16 deletions

View file

@ -0,0 +1,119 @@
<?php
/**
* @copyright Copyright (C) 2010-2024, 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\Console;
use Friendica\App\BaseURL;
use Friendica\Contact\Avatar;
use Friendica\Core\L10n;
use Friendica\Model\Contact;
use Friendica\Core\Config\Capability\IManageConfigValues;
/**
* tool to clear the avatar file cache.
*/
class ClearAvatarCache extends \Asika\SimpleConsole\Console
{
protected $helpOptions = ['h', 'help', '?'];
/**
* @var $dba Friendica\Database\Database
*/
private $dba;
/**
* @var $baseurl Friendica\App\BaseURL
*/
private $baseUrl;
/**
* @var L10n
*/
private $l10n;
/**
* @var IManageConfigValues
*/
private $config;
protected function getHelp()
{
$help = <<<HELP
console clearavatarcache - Clear the file based avatar cache
Synopsis
bin/console clearavatarcache
Description
bin/console clearavatarcache
Clear the file based avatar cache
Options
-h|--help|-? Show help information
HELP;
return $help;
}
public function __construct(\Friendica\Database\Database $dba, BaseURL $baseUrl, L10n $l10n, IManageConfigValues $config, array $argv = null)
{
parent::__construct($argv);
$this->dba = $dba;
$this->baseUrl = $baseUrl;
$this->l10n = $l10n;
$this->config = $config;
}
protected function doExecute(): int
{
if ($this->config->get('system', 'avatar_cache')) {
$this->err($this->l10n->t('The avatar cache needs to be disabled in local.config.php to use this command.'));
return 2;
}
// Contacts (but not self contacts) with cached avatars.
$condition = ["NOT `self` AND (`photo` != ? OR `thumb` != ? OR `micro` != ?)", '', '', ''];
$total = $this->dba->count('contact', $condition);
$count = 0;
$contacts = $this->dba->select('contact', ['id', 'uri-id', 'url', 'uid', 'photo', 'thumb', 'micro'], $condition);
while ($contact = $this->dba->fetch($contacts)) {
if (Avatar::deleteCache($contact) || $this->isAvatarCache($contact)) {
Contact::update(['photo' => '', 'thumb' => '', 'micro' => ''], ['id' => $contact['id']]);
}
$this->out(++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t" . $contact['photo']);
}
$this->dba->close($contacts);
return 0;
}
private function isAvatarCache(array $contact): bool
{
if (!empty($contact['photo']) && strpos($contact['photo'], Avatar::baseUrl()) === 0) {
return true;
}
if (!empty($contact['thumb']) && strpos($contact['thumb'], Avatar::baseUrl()) === 0) {
return true;
}
if (!empty($contact['micro']) && strpos($contact['micro'], Avatar::baseUrl()) === 0) {
return true;
}
return false;
}
}

View file

@ -23,14 +23,11 @@ namespace Friendica\Contact;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Network\HTTPClient\Client\HttpClientAccept; use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPClient\Client\HttpClientOptions; use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature; use Friendica\Util\HTTPSignature;
use Friendica\Util\Images;
use Friendica\Util\Network;
use Friendica\Util\Proxy; use Friendica\Util\Proxy;
/** /**
@ -97,7 +94,7 @@ class Avatar
return $fields; return $fields;
} }
$filename = self::getFilename($contact['url'], $avatar); $filename = self::getFilename($contact['url']);
$timestamp = time(); $timestamp = time();
$fields['blurhash'] = $image->getBlurHash(); $fields['blurhash'] = $image->getBlurHash();
@ -125,7 +122,7 @@ class Avatar
return $fields; return $fields;
} }
$filename = self::getFilename($contact['url'], $contact['avatar']); $filename = self::getFilename($contact['url']);
$timestamp = time(); $timestamp = time();
$fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp); $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp);
@ -135,12 +132,10 @@ class Avatar
return $fields; return $fields;
} }
private static function getFilename(string $url, string $host): string private static function getFilename(string $url): string
{ {
$guid = Item::guidFromUri($url, $host); $guid = hash('ripemd128', $url);
return substr($guid, 0, 3) . '/' . substr($guid, 4) . '-';
return 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) . '-';
} }
private static function storeAvatarCache(Image $image, string $filename, int $size, int $timestamp): string private static function storeAvatarCache(Image $image, string $filename, int $size, int $timestamp): string
@ -279,7 +274,7 @@ class Avatar
$localFile = self::getCacheFile($avatar); $localFile = self::getCacheFile($avatar);
if (!empty($localFile)) { if (!empty($localFile)) {
@unlink($localFile); @unlink($localFile);
Logger::debug('Unlink avatar', ['avatar' => $avatar]); Logger::debug('Unlink avatar', ['avatar' => $avatar, 'local' => $localFile]);
} }
} }
@ -316,7 +311,7 @@ class Avatar
* *
* @return string * @return string
*/ */
private static function baseUrl(): string public static function baseUrl(): string
{ {
$baseurl = DI::config()->get('system', 'avatar_cache_url'); $baseurl = DI::config()->get('system', 'avatar_cache_url');
if (!empty($baseurl)) { if (!empty($baseurl)) {

View file

@ -47,6 +47,7 @@ Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
Commands: Commands:
addon Addon management addon Addon management
cache Manage node cache cache Manage node cache
clearavatarcache Clear the file based avatar cache
config Edit site config config Edit site config
contact Contact management contact Contact management
createdoxygen Generate Doxygen headers createdoxygen Generate Doxygen headers
@ -84,6 +85,7 @@ HELP;
'archivecontact' => Friendica\Console\ArchiveContact::class, 'archivecontact' => Friendica\Console\ArchiveContact::class,
'autoinstall' => Friendica\Console\AutomaticInstallation::class, 'autoinstall' => Friendica\Console\AutomaticInstallation::class,
'cache' => Friendica\Console\Cache::class, 'cache' => Friendica\Console\Cache::class,
'clearavatarcache' => Friendica\Console\ClearAvatarCache::class,
'config' => Friendica\Console\Config::class, 'config' => Friendica\Console\Config::class,
'contact' => Friendica\Console\Contact::class, 'contact' => Friendica\Console\Contact::class,
'createdoxygen' => Friendica\Console\CreateDoxygen::class, 'createdoxygen' => Friendica\Console\CreateDoxygen::class,

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 2024.06-rc\n" "Project-Id-Version: 2024.06-rc\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-11 15:55+0000\n" "POT-Creation-Date: 2024-07-16 20:38+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -812,6 +812,10 @@ msgstr ""
msgid "The contact entries have been archived" msgid "The contact entries have been archived"
msgstr "" msgstr ""
#: src/Console/ClearAvatarCache.php:87
msgid "The avatar cache needs to be disabled in local.config.php to use this command."
msgstr ""
#: src/Console/GlobalCommunityBlock.php:96 #: src/Console/GlobalCommunityBlock.php:96
#: src/Module/Moderation/Blocklist/Contact.php:65 #: src/Module/Moderation/Blocklist/Contact.php:65
#, php-format #, php-format
@ -8884,15 +8888,15 @@ msgstr ""
msgid "Items tagged with: %s" msgid "Items tagged with: %s"
msgstr "" msgstr ""
#: src/Module/Search/Saved.php:59 #: src/Module/Search/Saved.php:63
msgid "Search term was not saved." msgid "Search term was not saved."
msgstr "" msgstr ""
#: src/Module/Search/Saved.php:62 #: src/Module/Search/Saved.php:66
msgid "Search term already saved." msgid "Search term already saved."
msgstr "" msgstr ""
#: src/Module/Search/Saved.php:68 #: src/Module/Search/Saved.php:72
msgid "Search term was not removed." msgid "Search term was not removed."
msgstr "" msgstr ""