[COMPONENT][Avatar][Controller] Implement multiple dimensions
This commit is contained in:
parent
2a902d6a7e
commit
de148c1f78
@ -29,6 +29,8 @@ use App\Core\Modules\Component;
|
|||||||
use App\Core\Router\RouteLoader;
|
use App\Core\Router\RouteLoader;
|
||||||
use App\Core\Router\Router;
|
use App\Core\Router\Router;
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
|
use Component\Attachment\Entity\Attachment;
|
||||||
|
use Component\Attachment\Entity\AttachmentThumbnail;
|
||||||
use Component\Avatar\Controller as C;
|
use Component\Avatar\Controller as C;
|
||||||
use Component\Avatar\Exception\NoAvatarException;
|
use Component\Avatar\Exception\NoAvatarException;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@ -41,8 +43,8 @@ class Avatar extends Component
|
|||||||
|
|
||||||
public function onAddRoute(RouteLoader $r): bool
|
public function onAddRoute(RouteLoader $r): bool
|
||||||
{
|
{
|
||||||
$r->connect('avatar_actor', '/actor/{actor_id<\d+>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'avatar_view']);
|
$r->connect('avatar_actor', '/actor/{actor_id<\d+>}/avatar/{size<full|big|medium|small>?medium}', [Controller\Avatar::class, 'avatar_view']);
|
||||||
$r->connect('avatar_default', '/avatar/default/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'default_avatar_view']);
|
$r->connect('avatar_default', '/avatar/default/{size<full|big|medium|small>?medium}', [Controller\Avatar::class, 'default_avatar_view']);
|
||||||
$r->connect('avatar_settings', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
|
$r->connect('avatar_settings', '/settings/avatar', [Controller\Avatar::class, 'settings_avatar']);
|
||||||
return Event::next;
|
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
|
* 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 {
|
try {
|
||||||
return self::getAvatar($actor_id)->getUrl($size, $type);
|
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 {
|
try {
|
||||||
$attachment = self::getAvatar($actor_id)->getAttachment();
|
$avatar = self::getAvatar($actor_id);
|
||||||
return ['width' => $attachment->getWidth(), 'height' => $attachment->getHeight()];
|
$a = $size === 'full' ? $avatar->getAttachment() : $avatar->getAttachmentThumbnail($size);
|
||||||
|
return ['width' => $a->getWidth(), 'height' => $a->getHeight()];
|
||||||
} catch (NoAvatarException) {
|
} catch (NoAvatarException) {
|
||||||
return ['width' => Common::config('thumbnail', 'small'), 'height' => Common::config('thumbnail', 'small')];
|
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.
|
* Returns the avatar file's hash, mimetype, title and path.
|
||||||
* Ensures exactly one cached value exists
|
* 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(
|
$res = Cache::get(
|
||||||
"avatar-file-info-{$actor_id}-{$size}",
|
"avatar-file-info-{$actor_id}-{$size}",
|
||||||
@ -152,7 +155,11 @@ class Avatar extends Component
|
|||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
$res = $res[0]; // A user must always only have one avatar.
|
$res = $res[0]; // A user must always only have one avatar.
|
||||||
$res['filepath'] = DB::findOneBy('attachment', ['id' => $res['id']])->getPath();
|
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;
|
return $res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,13 +57,8 @@ class Avatar extends Controller
|
|||||||
*/
|
*/
|
||||||
public function avatar_view(Request $request, int $actor_id, string $size): Response
|
public function avatar_view(Request $request, int $actor_id, string $size): Response
|
||||||
{
|
{
|
||||||
switch ($size) {
|
$res = \Component\Avatar\Avatar::getAvatarFileInfo($actor_id, $size);
|
||||||
case 'full':
|
|
||||||
$res = \Component\Avatar\Avatar::getAvatarFileInfo($actor_id);
|
|
||||||
return M::sendFile($res['filepath'], $res['mimetype'], $res['title']);
|
return M::sendFile($res['filepath'], $res['mimetype'], $res['title']);
|
||||||
default:
|
|
||||||
throw new Exception('Not implemented');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,6 +29,7 @@ use App\Core\Entity;
|
|||||||
use App\Core\Router\Router;
|
use App\Core\Router\Router;
|
||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
use Component\Attachment\Entity\Attachment;
|
use Component\Attachment\Entity\Attachment;
|
||||||
|
use Component\Attachment\Entity\AttachmentThumbnail;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -115,7 +116,7 @@ class Avatar extends Entity
|
|||||||
|
|
||||||
private ?Attachment $attachment = null;
|
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();
|
$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));
|
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
|
public function getAttachment(): Attachment
|
||||||
{
|
{
|
||||||
$this->attachment ??= DB::findOneBy('attachment', ['id' => $this->attachment_id]);
|
$this->attachment ??= DB::findOneBy('attachment', ['id' => $this->getAttachmentId()]);
|
||||||
return $this->attachment;
|
return $this->attachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAttachmentThumbnail(string $size): ?AttachmentThumbnail
|
||||||
|
{
|
||||||
|
return AttachmentThumbnail::getOrCreate($this->getAttachment(), $size);
|
||||||
|
}
|
||||||
|
|
||||||
public static function getFilePathStatic(string $filename): string
|
public static function getFilePathStatic(string $filename): string
|
||||||
{
|
{
|
||||||
return Common::config('avatar', 'dir') . $filename;
|
return Common::config('avatar', 'dir') . $filename;
|
||||||
|
@ -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);
|
return Avatar::getUrl($this->getId(), $size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAvatarDimensions(string $size = 'full')
|
public function getAvatarDimensions(string $size = 'medium')
|
||||||
{
|
{
|
||||||
return Avatar::getDimensions($this->getId(), $size);
|
return Avatar::getDimensions($this->getId(), $size);
|
||||||
}
|
}
|
||||||
|
@ -252,7 +252,7 @@ class Note extends Entity
|
|||||||
return Actor::getFullnameById($this->actor_id);
|
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);
|
return Avatar::getUrl($this->getActorId(), $size);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user