Console command to move avatars to the avatar cache
This commit is contained in:
parent
c4ec80e839
commit
d6242aacf6
4 changed files with 302 additions and 129 deletions
128
src/Console/MoveToAvatarCache.php
Normal file
128
src/Console/MoveToAvatarCache.php
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
<?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\Console;
|
||||
|
||||
use Friendica\App\BaseURL;
|
||||
use Friendica\Contact\Avatar;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Model\Contact;
|
||||
use Friendica\Model\Photo;
|
||||
use Friendica\Util\Images;
|
||||
use Friendica\Object\Image;
|
||||
|
||||
/**
|
||||
* tool to control the list of ActivityPub relay servers from the CLI
|
||||
*
|
||||
* With this script you can access the relay servers of your node from
|
||||
* the CLI.
|
||||
*/
|
||||
class MoveToAvatarCache 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;
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
$help = <<<HELP
|
||||
console movetoavatarcache - Move all cached avatars to the file based avatar cache
|
||||
Synopsis
|
||||
bin/console movetoavatarcache
|
||||
|
||||
Description
|
||||
bin/console movetoavatarcache
|
||||
Move all cached avatars to 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, array $argv = null)
|
||||
{
|
||||
parent::__construct($argv);
|
||||
|
||||
$this->dba = $dba;
|
||||
$this->baseurl = $baseurl;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
protected function doExecute()
|
||||
{
|
||||
$condition = ["`avatar` != ? AND `photo` LIKE ? AND `uid` = ? AND `uri-id` != ? AND NOT `uri-id` IS NULL",
|
||||
'', $this->baseurl->get() . '/photo/%', 0, 0];
|
||||
$total = $this->dba->count('contact', $condition);
|
||||
$contacts = $this->dba->select('contact', ['id', 'avatar', 'photo', 'uri-id', 'url', 'avatar'], $condition, ['order' => ['id' => true]]);
|
||||
$count = 0;
|
||||
while ($contact = $this->dba->fetch($contacts)) {
|
||||
echo ++$count . '/' . $total . "\t" . $contact['id'] . "\t" . $contact['url'] . "\t";
|
||||
$resourceid = Photo::ridFromURI($contact['photo']);
|
||||
if (empty($resourceid)) {
|
||||
echo $this->l10n->t('no resource') . "\n";
|
||||
continue;
|
||||
}
|
||||
echo '1';
|
||||
$photo = Photo::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
|
||||
if (empty($photo)) {
|
||||
echo $this->l10n->t('no photo') . "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
echo '2';
|
||||
$imgdata = Photo::getImageDataForPhoto($photo);
|
||||
if (empty($imgdata)) {
|
||||
echo $this->l10n->t('no image data') . "\n";
|
||||
continue;
|
||||
}
|
||||
echo '3';
|
||||
$image = new Image($imgdata, Images::getMimeTypeByData($imgdata));
|
||||
if (!$image->isValid()) {
|
||||
echo $this->l10n->t('invalid image') . "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
echo '4';
|
||||
$fields = Avatar::storeAvatarByImage($contact, $image);
|
||||
echo '5';
|
||||
Contact::update($fields, ['uri-id' => $contact['uri-id']]);
|
||||
echo '6';
|
||||
Photo::delete(['resource-id' => $resourceid]);
|
||||
echo ' '.$fields['photo'] . "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -72,11 +72,6 @@ class Avatar
|
|||
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();
|
||||
|
|
@ -91,6 +86,8 @@ class Avatar
|
|||
return $fields;
|
||||
}
|
||||
|
||||
$filename = self::getFilename($contact['url']);
|
||||
|
||||
$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);
|
||||
|
|
@ -100,6 +97,36 @@ class Avatar
|
|||
return $fields;
|
||||
}
|
||||
|
||||
public static function storeAvatarByImage(array $contact, Image $image): array
|
||||
{
|
||||
$fields = ['photo' => '', 'thumb' => '', 'micro' => ''];
|
||||
|
||||
if (!DI::config()->get('system', 'avatar_cache')) {
|
||||
self::deleteCache($contact);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
if (Network::isLocalLink($contact['avatar'])) {
|
||||
return $fields;
|
||||
}
|
||||
|
||||
$filename = self::getFilename($contact['url']);
|
||||
|
||||
$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);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
private static function getFilename(string $url)
|
||||
{
|
||||
$guid = Item::guidFromUri($url, parse_url($url, PHP_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) . '-';
|
||||
}
|
||||
|
||||
private static function storeAvatarCache(Image $image, string $filename, int $size): string
|
||||
{
|
||||
$image->scaleDown($size);
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ Commands:
|
|||
autoinstall Starts automatic installation of friendica based on values from htconfig.php
|
||||
lock Edit site locks
|
||||
maintenance Set maintenance mode for this node
|
||||
movetoavatarcache Move cached avatars to the file based avatar cache
|
||||
user User management
|
||||
php2po Generate a messages.po file from a strings.php file
|
||||
po2php Generate a strings.php file from a messages.po file
|
||||
|
|
@ -91,6 +92,7 @@ HELP;
|
|||
'globalcommunitysilence' => Friendica\Console\GlobalCommunitySilence::class,
|
||||
'lock' => Friendica\Console\Lock::class,
|
||||
'maintenance' => Friendica\Console\Maintenance::class,
|
||||
'movetoavatarcache' => Friendica\Console\MoveToAvatarCache::class,
|
||||
'php2po' => Friendica\Console\PhpToPo::class,
|
||||
'postupdate' => Friendica\Console\PostUpdate::class,
|
||||
'po2php' => Friendica\Console\PoToPhp::class,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue