AzuraCast/src/Controller/Api/Stations/Art/GetArtAction.php

80 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2019-10-11 01:22:02 +00:00
namespace App\Controller\Api\Stations\Art;
use App\Customization;
2019-10-11 01:22:02 +00:00
use App\Entity\Repository\StationMediaRepository;
use App\Entity\StationMedia;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Radio\Filesystem;
use OpenApi\Annotations as OA;
use Psr\Http\Message\ResponseInterface;
class GetArtAction
{
/**
2018-09-18 14:09:48 +00:00
* @OA\Get(path="/station/{station_id}/art/{media_id}",
* tags={"Stations: Media"},
* description="Returns the album art for a song, or a generic image.",
2018-09-18 14:09:48 +00:00
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
* @OA\Parameter(
* name="media_id",
* description="The station media unique ID",
* in="path",
2018-09-18 14:09:48 +00:00
* required=true,
* @OA\Schema(
* type="string"
2018-09-18 14:09:48 +00:00
* )
* ),
* @OA\Response(response=200, description="The requested album artwork"),
* @OA\Response(response=404, description="Image not found; generic filler image.")
* )
*
* @param ServerRequest $request
* @param Response $response
* @param Customization $customization
* @param Filesystem $filesystem
2019-10-11 01:22:02 +00:00
* @param StationMediaRepository $mediaRepo
* @param string $media_id
*
* @return ResponseInterface
*/
public function __invoke(
ServerRequest $request,
Response $response,
Customization $customization,
Filesystem $filesystem,
2019-10-11 01:22:02 +00:00
StationMediaRepository $mediaRepo,
$media_id
): ResponseInterface {
$defaultArtRedirect = $response->withRedirect($customization->getDefaultAlbumArtUrl(), 302);
$station = $request->getStation();
$fs = $filesystem->getForStation($station);
2019-10-11 01:22:02 +00:00
if (StationMedia::UNIQUE_ID_LENGTH === strlen($media_id)) {
$mediaPath = 'albumart://' . $media_id . '.jpg';
} else {
$media = $mediaRepo->find($media_id, $station);
if ($media instanceof StationMedia) {
$mediaPath = $media->getArtPath();
} else {
return $defaultArtRedirect;
2019-10-11 01:22:02 +00:00
}
}
2019-10-11 01:22:02 +00:00
if ($fs->has($mediaPath)) {
$file_meta = $fs->getMetadata($mediaPath);
$art = $fs->readStream($mediaPath);
if (is_resource($art)) {
return $response->withFile($art, 'image/jpeg')
->withCacheLifetime(Response::CACHE_ONE_YEAR)
->withHeader('Content-Length', $file_meta['size']);
}
}
return $defaultArtRedirect;
}
}