From f8fd85d33d18224ee260b7e68d32010b759e70ab Mon Sep 17 00:00:00 2001 From: Buster Neece Date: Wed, 9 Nov 2022 18:13:35 -0600 Subject: [PATCH] Create and use new "current art" endpoint. --- config/routes/api.php | 11 ++++++++-- ...ingAction.php => NowPlayingController.php} | 21 +++++++++++++++++-- .../Frontend/PublicPages/PlayerAction.php | 11 +++++----- templates/frontend/public/index.phtml | 2 +- 4 files changed, 34 insertions(+), 11 deletions(-) rename src/Controller/Api/{NowPlayingAction.php => NowPlayingController.php} (88%) diff --git a/config/routes/api.php b/config/routes/api.php index 5088d654f..32da3247f 100644 --- a/config/routes/api.php +++ b/config/routes/api.php @@ -76,8 +76,15 @@ return static function (RouteCollectorProxy $app) { } ); - $group->get('/nowplaying[/{station_id}]', Controller\Api\NowPlayingAction::class) - ->setName('api:nowplaying:index'); + $group->get( + '/nowplaying[/{station_id}]', + Controller\Api\NowPlayingController::class . ':getAction' + )->setName('api:nowplaying:index'); + + $group->get( + '/nowplaying/{station_id}/art[/{timestamp}.jpg]', + Controller\Api\NowPlayingController::class . ':getArtAction' + )->setName('api:nowplaying:art'); $group->get('/stations', Controller\Api\Stations\IndexController::class . ':listAction') ->setName('api:stations:list') diff --git a/src/Controller/Api/NowPlayingAction.php b/src/Controller/Api/NowPlayingController.php similarity index 88% rename from src/Controller/Api/NowPlayingAction.php rename to src/Controller/Api/NowPlayingController.php index 3ba98b554..899b9bf58 100644 --- a/src/Controller/Api/NowPlayingAction.php +++ b/src/Controller/Api/NowPlayingController.php @@ -49,7 +49,7 @@ use Psr\SimpleCache\CacheInterface; ] ) ] -final class NowPlayingAction +final class NowPlayingController { public function __construct( private readonly EntityManagerInterface $em, @@ -57,7 +57,7 @@ final class NowPlayingAction ) { } - public function __invoke( + public function getAction( ServerRequest $request, Response $response, ?string $station_id = null @@ -83,6 +83,23 @@ final class NowPlayingAction ); } + public function getArtAction( + ServerRequest $request, + Response $response, + string $station_id, + ?string $timestamp = null + ): ResponseInterface { + $np = $this->getForStation($station_id, $request->getRouter()); + + $currentArt = $np?->now_playing?->song?->art; + if (null !== $currentArt) { + return $response->withRedirect((string)$currentArt, 302); + } + + return $response->withStatus(404) + ->withJson(Entity\Api\Error::notFound()); + } + private function getForStation( string $station, RouterInterface $router diff --git a/src/Controller/Frontend/PublicPages/PlayerAction.php b/src/Controller/Frontend/PublicPages/PlayerAction.php index 9f69e57cb..ee8807fa7 100644 --- a/src/Controller/Frontend/PublicPages/PlayerAction.php +++ b/src/Controller/Frontend/PublicPages/PlayerAction.php @@ -7,7 +7,6 @@ namespace App\Controller\Frontend\PublicPages; use App\Entity; use App\Exception\StationNotFoundException; use App\Http\Response; -use App\Http\Router; use App\Http\ServerRequest; use Psr\Http\Message\ResponseInterface; @@ -16,7 +15,6 @@ final class PlayerAction public function __construct( private readonly Entity\ApiGenerator\NowPlayingApiGenerator $npApiGenerator, private readonly Entity\Repository\CustomFieldRepository $customFieldRepo, - private readonly Entity\Repository\StationRepository $stationRepo, ) { } @@ -41,9 +39,6 @@ final class PlayerAction $np = $this->npApiGenerator->currentOrEmpty($station); $np->resolveUrls($baseUrl); - $defaultAlbumArtUri = $this->stationRepo->getDefaultAlbumArtUrl($station); - $defaultAlbumArt = Router::resolveUri($baseUrl, $defaultAlbumArtUri, true); - // Build Vue props. $customization = $request->getCustomization(); $router = $request->getRouter(); @@ -108,7 +103,11 @@ final class PlayerAction [ 'station' => $station, 'props' => $props, - 'defaultAlbumArt' => $defaultAlbumArt, + 'nowPlayingArtUri' => $router->named( + routeName: 'api:nowplaying:art', + routeParams: ['station_id' => $station->getShortName(), 'timestamp' => time()], + absolute: true + ), ] ); } diff --git a/templates/frontend/public/index.phtml b/templates/frontend/public/index.phtml index e6c3faf7b..29b7bbb31 100644 --- a/templates/frontend/public/index.phtml +++ b/templates/frontend/public/index.phtml @@ -38,7 +38,7 @@ $this->push('head'); - +