4
0
mirror of https://github.com/AzuraCast/AzuraCast.git synced 2024-06-12 12:24:33 +00:00
AzuraCast/src/Controller/Api/Stations/ArtController.php

77 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Controller\Api\Stations;
use App\Customization;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Radio\Filesystem;
use OpenApi\Annotations as OA;
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Stream;
2019-04-22 11:19:21 +00:00
class ArtController
{
/** @var Customization */
protected $customization;
/** @var Filesystem */
protected $filesystem;
/**
2018-08-19 10:40:05 +00:00
* @param Customization $customization
* @param Filesystem $filesystem
*/
public function __construct(Customization $customization, Filesystem $filesystem)
{
$this->customization = $customization;
$this->filesystem = $filesystem;
}
/**
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 string|int $station_id
* @param string $media_id
*
* @return ResponseInterface
*/
public function __invoke(ServerRequest $request, Response $response, $station_id, $media_id): ResponseInterface
{
$station = $request->getStation();
$filesystem = $this->filesystem->getForStation($station);
$media_path = 'albumart://'.$media_id.'.jpg';
if ($filesystem->has($media_path)) {
$file_meta = $filesystem->getMetadata($media_path);
$art = $filesystem->readStream($media_path);
if (is_resource($art)) {
return $response->withFile($art)
->withCacheLifetime(Response::CACHE_ONE_YEAR)
->withHeader('Content-Type', 'image/jpeg')
->withHeader('Content-Length', $file_meta['size']);
}
}
return $response->withRedirect($this->customization->getDefaultAlbumArtUrl(), 302);
}
}