2019-04-22 11:19:21 +00:00
|
|
|
<?php
|
|
|
|
namespace App\Controller\Api\Stations;
|
|
|
|
|
|
|
|
use App\Entity;
|
2019-09-10 16:40:31 +00:00
|
|
|
use App\Exception\ValidationException;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2019-10-29 20:58:55 +00:00
|
|
|
use App\Message\WritePlaylistFileMessage;
|
|
|
|
use App\MessageQueue;
|
2019-04-25 00:16:12 +00:00
|
|
|
use App\Radio\Adapters;
|
|
|
|
use App\Radio\Backend\Liquidsoap;
|
2019-04-22 11:19:21 +00:00
|
|
|
use App\Radio\Filesystem;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2019-09-04 18:00:51 +00:00
|
|
|
use InvalidArgumentException;
|
2019-04-22 11:19:21 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2019-04-24 13:58:04 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2019-04-24 15:14:58 +00:00
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
2019-04-22 11:19:21 +00:00
|
|
|
use Symfony\Component\Serializer\Serializer;
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
|
|
|
|
|
class FilesController extends AbstractStationApiCrudController
|
|
|
|
{
|
2019-12-07 12:19:49 +00:00
|
|
|
protected string $entityClass = Entity\StationMedia::class;
|
|
|
|
protected string $resourceRouteName = 'api:stations:file';
|
2019-04-22 11:19:21 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected Filesystem $filesystem;
|
2019-04-22 11:19:21 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected Adapters $adapters;
|
2019-04-25 00:16:12 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected MessageQueue $messageQueue;
|
2019-10-29 20:58:55 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected Entity\Repository\CustomFieldRepository $custom_fields_repo;
|
2019-09-30 22:06:02 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected Entity\Repository\SongRepository $song_repo;
|
2019-10-20 05:14:37 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected Entity\Repository\StationMediaRepository $media_repo;
|
2019-04-24 13:58:04 +00:00
|
|
|
|
2019-12-07 12:19:49 +00:00
|
|
|
protected Entity\Repository\StationPlaylistMediaRepository $playlist_media_repo;
|
2019-04-25 00:16:12 +00:00
|
|
|
|
2019-04-22 11:19:21 +00:00
|
|
|
public function __construct(
|
|
|
|
EntityManager $em,
|
|
|
|
Serializer $serializer,
|
|
|
|
ValidatorInterface $validator,
|
2019-04-25 00:16:12 +00:00
|
|
|
Filesystem $filesystem,
|
2019-09-30 22:06:02 +00:00
|
|
|
Adapters $adapters,
|
2019-10-29 20:58:55 +00:00
|
|
|
MessageQueue $messageQueue,
|
2019-09-30 22:06:02 +00:00
|
|
|
Entity\Repository\CustomFieldRepository $custom_fields_repo,
|
2019-10-20 05:14:37 +00:00
|
|
|
Entity\Repository\SongRepository $song_repo,
|
2019-09-30 22:06:02 +00:00
|
|
|
Entity\Repository\StationMediaRepository $media_repo,
|
|
|
|
Entity\Repository\StationPlaylistMediaRepository $playlist_media_repo
|
2019-04-22 11:19:21 +00:00
|
|
|
) {
|
|
|
|
parent::__construct($em, $serializer, $validator);
|
|
|
|
|
|
|
|
$this->filesystem = $filesystem;
|
2019-04-25 00:16:12 +00:00
|
|
|
$this->adapters = $adapters;
|
2019-10-29 20:58:55 +00:00
|
|
|
$this->messageQueue = $messageQueue;
|
2019-04-25 00:16:12 +00:00
|
|
|
|
2019-09-30 22:06:02 +00:00
|
|
|
$this->custom_fields_repo = $custom_fields_repo;
|
|
|
|
$this->media_repo = $media_repo;
|
2019-10-20 05:14:37 +00:00
|
|
|
$this->song_repo = $song_repo;
|
2019-09-30 22:06:02 +00:00
|
|
|
$this->playlist_media_repo = $playlist_media_repo;
|
2019-04-22 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 13:58:04 +00:00
|
|
|
/**
|
|
|
|
* @OA\Get(path="/station/{station_id}/files",
|
|
|
|
* tags={"Stations: Media"},
|
|
|
|
* description="List all current uploaded files.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\Response(response=200, description="Success",
|
|
|
|
* @OA\JsonContent(type="array", @OA\Items(ref="#/components/schemas/StationMedia"))
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=403, description="Access denied"),
|
|
|
|
* security={{"api_key": {}}},
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @OA\Post(path="/station/{station_id}/files",
|
|
|
|
* tags={"Stations: Media"},
|
|
|
|
* description="Upload a new file.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Api_UploadFile")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=200, description="Success",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/StationMedia")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=403, description="Access denied"),
|
|
|
|
* security={{"api_key": {}}},
|
|
|
|
* )
|
2019-08-07 04:33:55 +00:00
|
|
|
*
|
2019-08-09 15:00:21 +00:00
|
|
|
* @param ServerRequest $request
|
|
|
|
* @param Response $response
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2019-08-07 04:33:55 +00:00
|
|
|
* @return ResponseInterface
|
2019-04-24 13:58:04 +00:00
|
|
|
*/
|
2019-09-10 02:30:05 +00:00
|
|
|
public function createAction(ServerRequest $request, Response $response): ResponseInterface
|
2019-04-24 13:58:04 +00:00
|
|
|
{
|
|
|
|
$station = $this->_getStation($request);
|
|
|
|
|
|
|
|
$body = $request->getParsedBody();
|
|
|
|
|
|
|
|
// Convert the body into an UploadFile API entity first.
|
|
|
|
/** @var Entity\Api\UploadFile $api_record */
|
|
|
|
$api_record = $this->serializer->denormalize($request->getParsedBody(), Entity\Api\UploadFile::class, null, []);
|
|
|
|
|
|
|
|
// Validate the UploadFile API record.
|
|
|
|
$errors = $this->validator->validate($api_record);
|
|
|
|
if (count($errors) > 0) {
|
2019-09-10 16:40:31 +00:00
|
|
|
$e = new ValidationException((string)$errors);
|
2019-04-24 13:58:04 +00:00
|
|
|
$e->setDetailedErrors($errors);
|
|
|
|
throw $e;
|
|
|
|
}
|
2019-04-22 11:19:21 +00:00
|
|
|
|
2019-04-24 13:58:04 +00:00
|
|
|
// Write file to temp path.
|
2019-09-04 18:00:51 +00:00
|
|
|
$temp_path = $station->getRadioTempDir() . '/' . $api_record->getSanitizedFilename();
|
2019-04-24 13:58:04 +00:00
|
|
|
file_put_contents($temp_path, $api_record->getFileContents());
|
|
|
|
|
2019-04-26 16:46:27 +00:00
|
|
|
$sanitized_path = 'media://' . $api_record->getSanitizedPath();
|
|
|
|
|
2019-04-24 13:58:04 +00:00
|
|
|
// Process temp path as regular media record.
|
2019-04-26 16:46:27 +00:00
|
|
|
$record = $this->media_repo->uploadFile($station, $temp_path, $sanitized_path);
|
2019-04-24 13:58:04 +00:00
|
|
|
|
2020-02-22 20:15:02 +00:00
|
|
|
$return = $this->_viewRecord($record, $request);
|
2019-04-24 13:58:04 +00:00
|
|
|
|
2019-08-09 15:00:21 +00:00
|
|
|
return $response->withJson($return);
|
2019-04-24 13:58:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @OA\Get(path="/station/{station_id}/file/{id}",
|
|
|
|
* tags={"Stations: Media"},
|
|
|
|
* description="Retrieve details for a single file.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="Media ID",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(type="integer", format="int64")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=200, description="Success",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/StationMedia")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=403, description="Access denied"),
|
|
|
|
* security={{"api_key": {}}},
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @OA\Put(path="/station/{station_id}/file/{id}",
|
|
|
|
* tags={"Stations: Media"},
|
|
|
|
* description="Update details of a single file.",
|
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/StationMedia")
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="Media ID",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(type="integer", format="int64")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=200, description="Success",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Api_Status")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=403, description="Access denied"),
|
|
|
|
* security={{"api_key": {}}},
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @OA\Delete(path="/station/{station_id}/file/{id}",
|
|
|
|
* tags={"Stations: Media"},
|
|
|
|
* description="Delete a single file.",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/station_id_required"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="id",
|
|
|
|
* in="path",
|
|
|
|
* description="Media ID",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(type="integer", format="int64")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=200, description="Success",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Api_Status")
|
|
|
|
* ),
|
|
|
|
* @OA\Response(response=403, description="Access denied"),
|
|
|
|
* security={{"api_key": {}}},
|
|
|
|
* )
|
|
|
|
*/
|
2019-04-24 15:14:58 +00:00
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2019-04-24 15:14:58 +00:00
|
|
|
protected function _normalizeRecord($record, array $context = [])
|
|
|
|
{
|
|
|
|
$row = parent::_normalizeRecord($record, $context);
|
|
|
|
|
|
|
|
if ($record instanceof Entity\StationMedia) {
|
2019-09-30 22:06:02 +00:00
|
|
|
$row['custom_fields'] = $this->custom_fields_repo->getCustomFields($record);
|
2019-04-24 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2019-04-24 15:14:58 +00:00
|
|
|
protected function _denormalizeToRecord($data, $record = null, array $context = []): object
|
|
|
|
{
|
2019-04-25 00:16:12 +00:00
|
|
|
$custom_fields = $data['custom_fields'] ?? null;
|
|
|
|
$playlists = $data['playlists'] ?? null;
|
|
|
|
unset($data['custom_fields'], $data['playlists']);
|
|
|
|
|
|
|
|
$record = parent::_denormalizeToRecord($data, $record, array_merge($context, [
|
2019-04-24 15:14:58 +00:00
|
|
|
AbstractNormalizer::CALLBACKS => [
|
2019-09-04 18:00:51 +00:00
|
|
|
'path' => function ($new_value, $record) {
|
2019-04-25 00:16:12 +00:00
|
|
|
// Detect and handle a rename.
|
|
|
|
if (($record instanceof Entity\StationMedia) && $new_value !== $record->getPath()) {
|
2019-09-04 18:00:51 +00:00
|
|
|
$path_full = 'media://' . $new_value;
|
2019-04-25 00:16:12 +00:00
|
|
|
|
|
|
|
$fs = $this->filesystem->getForStation($record->getStation());
|
|
|
|
$fs->rename($record->getPathUri(), $path_full);
|
|
|
|
}
|
2019-04-24 15:14:58 +00:00
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
return $new_value;
|
2019-04-24 15:14:58 +00:00
|
|
|
},
|
2019-09-04 18:00:51 +00:00
|
|
|
],
|
2019-04-24 15:14:58 +00:00
|
|
|
]));
|
2019-04-25 00:16:12 +00:00
|
|
|
|
|
|
|
if ($record instanceof Entity\StationMedia) {
|
2019-11-08 22:59:16 +00:00
|
|
|
$this->em->persist($record);
|
|
|
|
$this->em->flush($record);
|
|
|
|
|
2019-10-20 05:14:37 +00:00
|
|
|
if ($this->media_repo->writeToFile($record)) {
|
|
|
|
$song_info = [
|
|
|
|
'title' => $record->getTitle(),
|
|
|
|
'artist' => $record->getArtist(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$song = $this->song_repo->getOrCreate($song_info);
|
|
|
|
$song->update($song_info);
|
|
|
|
$this->em->persist($song);
|
|
|
|
|
|
|
|
$record->setSong($song);
|
|
|
|
}
|
2019-10-24 19:37:27 +00:00
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
if (null !== $custom_fields) {
|
2019-09-30 22:06:02 +00:00
|
|
|
$this->custom_fields_repo->setCustomFields($record, $custom_fields);
|
2019-04-25 00:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $playlists) {
|
|
|
|
$station = $record->getStation();
|
|
|
|
|
|
|
|
/** @var Entity\StationPlaylist[] $playlists */
|
|
|
|
$affected_playlists = [];
|
|
|
|
|
|
|
|
// Remove existing playlists.
|
|
|
|
$media_playlists = $this->playlist_media_repo->clearPlaylistsFromMedia($record);
|
2019-09-04 18:00:51 +00:00
|
|
|
foreach ($media_playlists as $playlist_id => $playlist) {
|
2019-04-25 00:16:12 +00:00
|
|
|
if (!isset($affected_playlists[$playlist_id])) {
|
|
|
|
$affected_playlists[$playlist_id] = $playlist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new playlists.
|
2019-09-04 18:00:51 +00:00
|
|
|
foreach ($playlists as $new_playlist) {
|
2019-04-25 00:16:12 +00:00
|
|
|
if (is_array($new_playlist)) {
|
|
|
|
$playlist_id = $new_playlist['id'];
|
|
|
|
$playlist_weight = $new_playlist['weight'] ?? 0;
|
|
|
|
} else {
|
|
|
|
$playlist_id = $new_playlist;
|
|
|
|
$playlist_weight = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-06 21:19:32 +00:00
|
|
|
$playlist = $this->em->getRepository(Entity\StationPlaylist::class)->findOneBy([
|
2019-04-25 00:16:12 +00:00
|
|
|
'station_id' => $station->getId(),
|
2019-09-04 18:00:51 +00:00
|
|
|
'id' => (int)$playlist_id,
|
2019-04-25 00:16:12 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($playlist instanceof Entity\StationPlaylist) {
|
|
|
|
$affected_playlists[$playlist->getId()] = $playlist;
|
|
|
|
$this->playlist_media_repo->addMediaToPlaylist($record, $playlist, $playlist_weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle playlist changes.
|
|
|
|
$backend = $this->adapters->getBackendAdapter($station);
|
|
|
|
if ($backend instanceof Liquidsoap) {
|
2019-10-29 20:58:55 +00:00
|
|
|
foreach ($affected_playlists as $playlist_id => $playlist_row) {
|
|
|
|
// Instruct the message queue to start a new "write playlist to file" task.
|
|
|
|
$message = new WritePlaylistFileMessage;
|
|
|
|
$message->playlist_id = $playlist_id;
|
|
|
|
$this->messageQueue->produce($message);
|
2019-04-25 00:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $record;
|
2019-04-24 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2019-04-24 15:14:58 +00:00
|
|
|
protected function _deleteRecord($record): void
|
|
|
|
{
|
|
|
|
if (!($record instanceof Entity\StationMedia)) {
|
2019-09-04 18:00:51 +00:00
|
|
|
throw new InvalidArgumentException(sprintf('Record must be an instance of %s.', $this->entityClass));
|
2019-04-24 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
$station = $record->getStation();
|
|
|
|
|
|
|
|
/** @var Entity\StationPlaylist[] $playlists */
|
|
|
|
$affected_playlists = [];
|
|
|
|
|
|
|
|
$media_playlists = $this->playlist_media_repo->clearPlaylistsFromMedia($record);
|
2019-09-04 18:00:51 +00:00
|
|
|
foreach ($media_playlists as $playlist_id => $playlist) {
|
2019-04-25 00:16:12 +00:00
|
|
|
if (!isset($affected_playlists[$playlist_id])) {
|
|
|
|
$affected_playlists[$playlist_id] = $playlist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:14:58 +00:00
|
|
|
// Delete the media file off the filesystem.
|
2019-04-25 00:16:12 +00:00
|
|
|
$fs = $this->filesystem->getForStation($station);
|
2019-10-24 19:37:27 +00:00
|
|
|
|
2019-04-24 15:14:58 +00:00
|
|
|
$fs->delete($record->getPathUri());
|
2019-10-24 19:37:27 +00:00
|
|
|
$fs->delete($record->getArtPath());
|
2019-04-24 15:14:58 +00:00
|
|
|
|
2019-04-25 00:16:12 +00:00
|
|
|
// Write new PLS playlist configuration.
|
|
|
|
$backend = $this->adapters->getBackendAdapter($station);
|
|
|
|
if ($backend instanceof Liquidsoap) {
|
2019-10-29 20:58:55 +00:00
|
|
|
foreach ($affected_playlists as $playlist_id => $playlist_row) {
|
|
|
|
// Instruct the message queue to start a new "write playlist to file" task.
|
|
|
|
$message = new WritePlaylistFileMessage;
|
|
|
|
$message->playlist_id = $playlist_id;
|
|
|
|
$this->messageQueue->produce($message);
|
2019-04-25 00:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:14:58 +00:00
|
|
|
parent::_deleteRecord($record);
|
|
|
|
}
|
2019-04-22 11:19:21 +00:00
|
|
|
}
|