diff --git a/components/Avatar/Avatar.php b/components/Avatar/Avatar.php index f378d1d6f8..cc60aa59f6 100644 --- a/components/Avatar/Avatar.php +++ b/components/Avatar/Avatar.php @@ -29,6 +29,8 @@ use App\Core\Modules\Component; use App\Core\Router\RouteLoader; use App\Core\Router\Router; use App\Util\Common; +use Component\Attachment\Entity\Attachment; +use Component\Attachment\Entity\AttachmentThumbnail; use Component\Avatar\Controller as C; use Component\Avatar\Exception\NoAvatarException; use Symfony\Component\HttpFoundation\Request; @@ -41,8 +43,8 @@ class Avatar extends Component public function onAddRoute(RouteLoader $r): bool { - $r->connect('avatar_actor', '/actor/{actor_id<\d+>}/avatar/{size?full}', [Controller\Avatar::class, 'avatar_view']); - $r->connect('avatar_default', '/avatar/default/{size?full}', [Controller\Avatar::class, 'default_avatar_view']); + $r->connect('avatar_actor', '/actor/{actor_id<\d+>}/avatar/{size?medium}', [Controller\Avatar::class, 'avatar_view']); + $r->connect('avatar_default', '/avatar/default/{size?medium}', [Controller\Avatar::class, 'default_avatar_view']); $r->connect('avatar_settings', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']); return Event::next; } @@ -102,7 +104,7 @@ class Avatar extends Component /** * Get the cached avatar associated with the given Actor id, or the current user if not given */ - public static function getUrl(int $actor_id, string $size = 'full', int $type = Router::ABSOLUTE_PATH): string + public static function getUrl(int $actor_id, string $size = 'medium', int $type = Router::ABSOLUTE_PATH): string { try { return self::getAvatar($actor_id)->getUrl($size, $type); @@ -111,11 +113,12 @@ class Avatar extends Component } } - public static function getDimensions(int $actor_id, string $size = 'full') + public static function getDimensions(int $actor_id, string $size = 'medium') { try { - $attachment = self::getAvatar($actor_id)->getAttachment(); - return ['width' => $attachment->getWidth(), 'height' => $attachment->getHeight()]; + $avatar = self::getAvatar($actor_id); + $a = $size === 'full' ? $avatar->getAttachment() : $avatar->getAttachmentThumbnail($size); + return ['width' => $a->getWidth(), 'height' => $a->getHeight()]; } catch (NoAvatarException) { return ['width' => Common::config('thumbnail', 'small'), 'height' => Common::config('thumbnail', 'small')]; } @@ -127,7 +130,7 @@ class Avatar extends Component * Returns the avatar file's hash, mimetype, title and path. * Ensures exactly one cached value exists */ - public static function getAvatarFileInfo(int $actor_id, string $size = 'full'): array + public static function getAvatarFileInfo(int $actor_id, string $size = 'medium'): array { $res = Cache::get( "avatar-file-info-{$actor_id}-{$size}", @@ -151,8 +154,12 @@ class Avatar extends Component 'title' => 'default_avatar.svg', ]; } else { - $res = $res[0]; // A user must always only have one avatar. - $res['filepath'] = DB::findOneBy('attachment', ['id' => $res['id']])->getPath(); + $res = $res[0]; // A user must always only have one avatar. + if ($size === 'full') { + $res['filepath'] = Attachment::getByPK(['id' => $res['id']])->getPath(); + } else { + $res['filepath'] = AttachmentThumbnail::getOrCreate(Attachment::getByPK(['id' => $res['id']]), $size)->getPath(); + } return $res; } } diff --git a/components/Avatar/Controller/Avatar.php b/components/Avatar/Controller/Avatar.php index dbd4573b65..d51d2b5449 100644 --- a/components/Avatar/Controller/Avatar.php +++ b/components/Avatar/Controller/Avatar.php @@ -57,13 +57,8 @@ class Avatar extends Controller */ public function avatar_view(Request $request, int $actor_id, string $size): Response { - switch ($size) { - case 'full': - $res = \Component\Avatar\Avatar::getAvatarFileInfo($actor_id); - return M::sendFile($res['filepath'], $res['mimetype'], $res['title']); - default: - throw new Exception('Not implemented'); - } + $res = \Component\Avatar\Avatar::getAvatarFileInfo($actor_id, $size); + return M::sendFile($res['filepath'], $res['mimetype'], $res['title']); } /** diff --git a/components/Avatar/Entity/Avatar.php b/components/Avatar/Entity/Avatar.php index 3b9bd4bbf2..88f8b0dd05 100644 --- a/components/Avatar/Entity/Avatar.php +++ b/components/Avatar/Entity/Avatar.php @@ -29,6 +29,7 @@ use App\Core\Entity; use App\Core\Router\Router; use App\Util\Common; use Component\Attachment\Entity\Attachment; +use Component\Attachment\Entity\AttachmentThumbnail; use DateTimeInterface; /** @@ -115,7 +116,7 @@ class Avatar extends Entity private ?Attachment $attachment = null; - public function getUrl(string $size = 'full', int $type = Router::ABSOLUTE_PATH): string + public function getUrl(string $size = 'medium', int $type = Router::ABSOLUTE_PATH): string { $actor_id = $this->getActorId(); return Cache::get("avatar-url-{$actor_id}-{$size}-{$type}", fn () => Router::url('avatar_actor', ['actor_id' => $actor_id, 'size' => $size], $type)); @@ -123,10 +124,15 @@ class Avatar extends Entity public function getAttachment(): Attachment { - $this->attachment ??= DB::findOneBy('attachment', ['id' => $this->attachment_id]); + $this->attachment ??= DB::findOneBy('attachment', ['id' => $this->getAttachmentId()]); return $this->attachment; } + public function getAttachmentThumbnail(string $size): ?AttachmentThumbnail + { + return AttachmentThumbnail::getOrCreate($this->getAttachment(), $size); + } + public static function getFilePathStatic(string $filename): string { return Common::config('avatar', 'dir') . $filename; diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index 180bccd195..d6368cd1e1 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -288,12 +288,12 @@ class Actor extends Entity } } - public function getAvatarUrl(string $size = 'full') + public function getAvatarUrl(string $size = 'medium') { return Avatar::getUrl($this->getId(), $size); } - public function getAvatarDimensions(string $size = 'full') + public function getAvatarDimensions(string $size = 'medium') { return Avatar::getDimensions($this->getId(), $size); } diff --git a/src/Entity/Note.php b/src/Entity/Note.php index f96ee7d004..5ca45bfb3e 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -252,7 +252,7 @@ class Note extends Entity return Actor::getFullnameById($this->actor_id); } - public function getActorAvatarUrl(string $size = 'full'): string + public function getActorAvatarUrl(string $size = 'medium'): string { return Avatar::getUrl($this->getActorId(), $size); }