]> git.mxchange.org Git - friendica.git/commitdiff
New console command to clear the avatar cache
authorMichael <heluecht@pirati.ca>
Mon, 15 Jul 2024 17:48:45 +0000 (17:48 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 18 Jul 2024 10:53:49 +0000 (10:53 +0000)
src/Console/ClearAvatarCache.php [new file with mode: 0644]
src/Contact/Avatar.php
src/Core/Console.php
view/lang/C/messages.po

diff --git a/src/Console/ClearAvatarCache.php b/src/Console/ClearAvatarCache.php
new file mode 100644 (file)
index 0000000..3d50db5
--- /dev/null
@@ -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;
+       }
+}
index deef2dcc747e23615bd8cc40d4fedf5f5dbcbf61..12efe19c35465c43dd2f328ee289269c9c4c6426 100644 (file)
@@ -23,14 +23,11 @@ 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;
 
 /**
@@ -97,7 +94,7 @@ class Avatar
                        return $fields;
                }
 
-               $filename  = self::getFilename($contact['url'], $avatar);
+               $filename  = self::getFilename($contact['url']);
                $timestamp = time();
 
                $fields['blurhash'] = $image->getBlurHash();
@@ -125,7 +122,7 @@ class Avatar
                        return $fields;
                }
 
-               $filename  = self::getFilename($contact['url'], $contact['avatar']);
+               $filename  = self::getFilename($contact['url']);
                $timestamp = time();
 
                $fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp);
@@ -135,12 +132,10 @@ class Avatar
                return $fields;
        }
 
-       private static function getFilename(string $url, string $host): string
+       private static function getFilename(string $url): string
        {
-               $guid = Item::guidFromUri($url, $host);
-
-               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) . '-';
+               $guid = hash('ripemd128', $url);
+               return substr($guid, 0, 3) . '/' . substr($guid, 4) . '-';
        }
 
        private static function storeAvatarCache(Image $image, string $filename, int $size, int $timestamp): string
@@ -279,7 +274,7 @@ class Avatar
                $localFile = self::getCacheFile($avatar);
                if (!empty($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
         */
-       private static function baseUrl(): string
+       public static function baseUrl(): string
        {
                $baseurl = DI::config()->get('system', 'avatar_cache_url');
                if (!empty($baseurl)) {
index ee10558091c103b80ca7639aca1b2a7214b92176..b1e89d749f78a4d42e4a28a16de6b14c3434a09c 100644 (file)
@@ -47,6 +47,7 @@ Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
 Commands:
        addon                  Addon management
        cache                  Manage node cache
+       clearavatarcache       Clear the file based avatar cache
        config                 Edit site config
        contact                Contact management
        createdoxygen          Generate Doxygen headers
@@ -84,6 +85,7 @@ HELP;
                'archivecontact'         => Friendica\Console\ArchiveContact::class,
                'autoinstall'            => Friendica\Console\AutomaticInstallation::class,
                'cache'                  => Friendica\Console\Cache::class,
+               'clearavatarcache'       => Friendica\Console\ClearAvatarCache::class,
                'config'                 => Friendica\Console\Config::class,
                'contact'                => Friendica\Console\Contact::class,
                'createdoxygen'          => Friendica\Console\CreateDoxygen::class,
index 5fbee4e4feec1b4c94b5be4c68f8a5a0771d5d51..9452211f2fe5e1d1df076352fdcc04b3758e174a 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: 2024.06-rc\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"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -812,6 +812,10 @@ msgstr ""
 msgid "The contact entries have been archived"
 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/Module/Moderation/Blocklist/Contact.php:65
 #, php-format
@@ -8884,15 +8888,15 @@ msgstr ""
 msgid "Items tagged with: %s"
 msgstr ""
 
-#: src/Module/Search/Saved.php:59
+#: src/Module/Search/Saved.php:63
 msgid "Search term was not saved."
 msgstr ""
 
-#: src/Module/Search/Saved.php:62
+#: src/Module/Search/Saved.php:66
 msgid "Search term already saved."
 msgstr ""
 
-#: src/Module/Search/Saved.php:68
+#: src/Module/Search/Saved.php:72
 msgid "Search term was not removed."
 msgstr ""