2014-09-07 00:33:46 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Controllers;
|
|
|
|
|
|
|
|
final class UserAvatarController extends AbstractController
|
|
|
|
{
|
|
|
|
private $userService;
|
|
|
|
private $fileService;
|
|
|
|
private $httpHelper;
|
|
|
|
private $thumbnailService;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\Services\UserService $userService,
|
|
|
|
\Szurubooru\Services\FileService $fileService,
|
|
|
|
\Szurubooru\Helpers\HttpHelper $httpHelper,
|
|
|
|
\Szurubooru\Services\ThumbnailService $thumbnailService)
|
|
|
|
{
|
|
|
|
$this->userService = $userService;
|
|
|
|
$this->fileService = $fileService;
|
|
|
|
$this->httpHelper = $httpHelper;
|
|
|
|
$this->thumbnailService = $thumbnailService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerRoutes(\Szurubooru\Router $router)
|
|
|
|
{
|
|
|
|
$router->get('/api/users/:userName/avatar/:size', [$this, 'getAvatarByName']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAvatarByName($userName, $size)
|
|
|
|
{
|
|
|
|
$user = $this->userService->getByName($userName);
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
switch ($user->getAvatarStyle())
|
2014-09-07 00:33:46 +02:00
|
|
|
{
|
|
|
|
case \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR:
|
2014-09-13 23:58:13 +02:00
|
|
|
$hash = md5(strtolower(trim($user->getEmail() ? $user->getEmail() : $user->getId() . $user->getName())));
|
2014-09-07 00:33:46 +02:00
|
|
|
$url = 'https://www.gravatar.com/avatar/' . $hash . '?d=retro&s=' . $size;
|
|
|
|
$this->serveFromUrl($url);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case \Szurubooru\Entities\User::AVATAR_STYLE_BLANK:
|
2014-09-20 12:45:56 +02:00
|
|
|
$this->serveFromFile($this->getBlankAvatarSourcePath(), $size);
|
2014-09-07 00:33:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL:
|
2014-09-20 12:45:56 +02:00
|
|
|
$this->serveFromFile($user->getCustomAvatarSourceContentPath(), $size);
|
2014-09-07 00:33:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-09-20 12:45:56 +02:00
|
|
|
$this->serveFromFile($this->getBlankAvatarSourcePath(), $size);
|
2014-09-07 00:33:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function serveFromUrl($url)
|
|
|
|
{
|
2014-09-08 22:02:45 +02:00
|
|
|
$this->httpHelper->redirect($url);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function serveFromFile($file, $size)
|
|
|
|
{
|
|
|
|
if (!$this->fileService->exists($file))
|
2014-09-20 12:45:56 +02:00
|
|
|
$file = $this->getBlankAvatarSourcePath();
|
2014-09-07 00:33:46 +02:00
|
|
|
|
2014-09-09 17:49:19 +02:00
|
|
|
$sizedFile = $this->thumbnailService->getOrGenerate($file, $size, $size);
|
2014-09-07 00:33:46 +02:00
|
|
|
$this->fileService->serve($sizedFile);
|
|
|
|
}
|
2014-09-20 12:45:56 +02:00
|
|
|
|
|
|
|
private function getBlankAvatarSourcePath()
|
|
|
|
{
|
|
|
|
return 'avatars' . DIRECTORY_SEPARATOR . 'blank.png';
|
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|