60 lines
2.0 KiB
PHP
60 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Download\Framework\Controller;
|
|
|
|
use App\Download\Action\Input\DownloadMediaInput;
|
|
use App\Download\Framework\Repository\DownloadRepository;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Mercure\HubInterface;
|
|
use Symfony\Component\Mercure\Update;
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
class ApiController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private DownloadRepository $downloadRepository,
|
|
private MessageBusInterface $bus,
|
|
private readonly HubInterface $hub,
|
|
) {}
|
|
|
|
#[Route('/api/download', name: 'api_download', methods: ['POST'])]
|
|
public function download(
|
|
Request $request,
|
|
DownloadMediaInput $input,
|
|
): Response {
|
|
$download = $this->downloadRepository->insert(
|
|
$this->getUser(),
|
|
$input->url,
|
|
$input->title,
|
|
$input->filename,
|
|
$input->imdbId,
|
|
$input->mediaType,
|
|
"",
|
|
);
|
|
$this->downloadRepository->getEntityManager()->persist($download);
|
|
$this->downloadRepository->getEntityManager()->flush();
|
|
$input->downloadId = $download->getId();
|
|
$input->userId = $this->getUser()->getId();
|
|
|
|
try {
|
|
$this->bus->dispatch($input->toCommand());
|
|
} catch (\Throwable $exception) {
|
|
return $this->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
$this->hub->publish(new Update(
|
|
$request->getSession()->get('mercure_alert_topic'),
|
|
$this->renderView('broadcast/Alert.stream.html.twig', [
|
|
'alert_id' => uniqid(),
|
|
'title' => 'Success',
|
|
'message' => '"' . $input->title . '" added to Queue',
|
|
])
|
|
));
|
|
|
|
return $this->json(['status' => 200, 'message' => 'Added to Queue']);
|
|
}
|
|
}
|