2017-11-04 02:13:41 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-10-11 01:22:02 +00:00
|
|
|
namespace App\Controller\Api\Stations\Art;
|
2017-11-04 02:13:41 +00:00
|
|
|
|
2020-11-27 01:26:46 +00:00
|
|
|
use App\Entity;
|
2021-03-31 16:42:24 +00:00
|
|
|
use App\Flysystem\StationFilesystems;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2019-04-07 08:40:55 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2019-08-07 04:33:55 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2017-11-04 02:13:41 +00:00
|
|
|
|
2019-10-10 02:13:04 +00:00
|
|
|
class GetArtAction
|
2017-11-04 02:13:41 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-09-18 14:09:48 +00:00
|
|
|
* @OA\Get(path="/station/{station_id}/art/{media_id}",
|
2017-11-04 02:13:41 +00:00
|
|
|
* 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(
|
2018-02-17 17:57:40 +00:00
|
|
|
* name="media_id",
|
2018-12-05 07:15:51 +00:00
|
|
|
* description="The station media unique ID",
|
2018-02-17 17:57:40 +00:00
|
|
|
* in="path",
|
2018-09-18 14:09:48 +00:00
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
2018-12-05 07:15:51 +00:00
|
|
|
* type="string"
|
2018-09-18 14:09:48 +00:00
|
|
|
* )
|
2018-02-17 17:57:40 +00:00
|
|
|
* ),
|
2018-12-24 08:47:45 +00:00
|
|
|
* @OA\Response(response=200, description="The requested album artwork"),
|
|
|
|
* @OA\Response(response=404, description="Image not found; generic filler image.")
|
2017-11-04 02:13:41 +00:00
|
|
|
* )
|
2019-08-07 04:33:55 +00:00
|
|
|
*
|
2019-08-09 15:00:21 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
* @param Response $response
|
2020-11-27 01:26:46 +00:00
|
|
|
* @param Entity\Repository\StationRepository $stationRepo
|
|
|
|
* @param Entity\Repository\StationMediaRepository $mediaRepo
|
2019-08-07 04:33:55 +00:00
|
|
|
* @param string $media_id
|
2017-11-04 02:13:41 +00:00
|
|
|
*/
|
2019-10-10 02:13:04 +00:00
|
|
|
public function __invoke(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2020-11-27 01:26:46 +00:00
|
|
|
Entity\Repository\StationRepository $stationRepo,
|
|
|
|
Entity\Repository\StationMediaRepository $mediaRepo,
|
|
|
|
string $media_id
|
2019-10-10 02:13:04 +00:00
|
|
|
): ResponseInterface {
|
2019-08-09 15:00:21 +00:00
|
|
|
$station = $request->getStation();
|
2021-03-31 16:42:24 +00:00
|
|
|
|
2021-06-08 06:40:49 +00:00
|
|
|
$fsMedia = (new StationFilesystems($station))->getMediaFilesystem();
|
2020-05-03 01:28:08 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
$defaultArtRedirect = $response->withRedirect((string)$stationRepo->getDefaultAlbumArtUrl($station), 302);
|
2017-11-04 02:13:41 +00:00
|
|
|
|
2019-10-24 19:37:27 +00:00
|
|
|
// If a timestamp delimiter is added, strip it automatically.
|
2021-06-08 06:40:49 +00:00
|
|
|
$media_id = explode('-', $media_id, 2)[0];
|
2019-10-24 19:37:27 +00:00
|
|
|
|
2020-11-27 01:26:46 +00:00
|
|
|
if (Entity\StationMedia::UNIQUE_ID_LENGTH === strlen($media_id)) {
|
2020-05-12 00:32:41 +00:00
|
|
|
$response = $response->withCacheLifetime(Response::CACHE_ONE_YEAR);
|
2020-11-27 01:26:46 +00:00
|
|
|
$mediaPath = Entity\StationMedia::getArtPath($media_id);
|
2019-10-11 01:22:02 +00:00
|
|
|
} else {
|
|
|
|
$media = $mediaRepo->find($media_id, $station);
|
2020-11-27 01:26:46 +00:00
|
|
|
if ($media instanceof Entity\StationMedia) {
|
|
|
|
$mediaPath = Entity\StationMedia::getArtPath($media->getUniqueId());
|
2019-10-11 01:50:45 +00:00
|
|
|
} else {
|
|
|
|
return $defaultArtRedirect;
|
2019-10-11 01:22:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 02:13:41 +00:00
|
|
|
|
2021-03-31 16:42:24 +00:00
|
|
|
if ($fsMedia->fileExists($mediaPath)) {
|
2021-04-05 23:31:29 +00:00
|
|
|
return $response->streamFilesystemFile($fsMedia, $mediaPath, null, 'inline');
|
2017-11-04 02:13:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 01:50:45 +00:00
|
|
|
return $defaultArtRedirect;
|
2017-11-04 02:13:41 +00:00
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|