Create and use new "current art" endpoint.

This commit is contained in:
Buster Neece 2022-11-09 18:13:35 -06:00
parent 94217eb4b3
commit f8fd85d33d
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
4 changed files with 34 additions and 11 deletions

View File

@ -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')

View File

@ -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

View File

@ -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
),
]
);
}

View File

@ -38,7 +38,7 @@ $this->push('head');
<meta property="og:title" content="<?= $this->e($station->getName() ?? '') ?>">
<meta property="og:url" content="<?= $this->e($station->getUrl() ?? '') ?>">
<meta property="og:image" content="<?= $defaultAlbumArt ?>">
<meta property="og:image" content="<?= $nowPlayingArtUri ?>">
<meta property="twitter:card" content="player">
<meta property="twitter:player" content="<?= $router->named(