From 3074b2d5f17a534dfad5fe81c5394175a21adff0 Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Mon, 19 May 2025 22:28:00 -0500 Subject: [PATCH] feat: download movie to dedicated directory --- .../Action/Handler/DownloadMediaHandler.php | 2 - .../Downloader/DownloaderInterface.php | 4 +- src/Download/Downloader/ProcessDownloader.php | 32 ++++++++++---- .../Framework/Controller/ApiController.php | 6 +-- src/Monitor/Service/MediaFiles.php | 42 +++++++++++++++++++ 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/Download/Action/Handler/DownloadMediaHandler.php b/src/Download/Action/Handler/DownloadMediaHandler.php index 4e27337..6207f51 100644 --- a/src/Download/Action/Handler/DownloadMediaHandler.php +++ b/src/Download/Action/Handler/DownloadMediaHandler.php @@ -37,8 +37,6 @@ readonly class DownloadMediaHandler implements HandlerInterface $download = $this->downloadRepository->find($command->downloadId); } - dump($download); - try { $this->downloadRepository->updateStatus($download->getId(), 'In Progress'); diff --git a/src/Download/Downloader/DownloaderInterface.php b/src/Download/Downloader/DownloaderInterface.php index 2a5722c..18b2554 100644 --- a/src/Download/Downloader/DownloaderInterface.php +++ b/src/Download/Downloader/DownloaderInterface.php @@ -10,11 +10,11 @@ use App\Message\DownloadTvShowMessage; interface DownloaderInterface { /** - * @param string $baseDir + * @param string $mediaType * @param string $title * @param string $url * @return void * Downloads the requested file. */ - public function download(string $baseDir, string $title, string $url, ?int $downloadId): void; + public function download(string $mediaType, string $title, string $url, ?int $downloadId): void; } diff --git a/src/Download/Downloader/ProcessDownloader.php b/src/Download/Downloader/ProcessDownloader.php index 5483626..b878a34 100644 --- a/src/Download/Downloader/ProcessDownloader.php +++ b/src/Download/Downloader/ProcessDownloader.php @@ -3,6 +3,7 @@ namespace App\Download\Downloader; use App\Download\Framework\Entity\Download; +use App\Monitor\Service\MediaFiles; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; @@ -11,25 +12,26 @@ class ProcessDownloader implements DownloaderInterface { public function __construct( private EntityManagerInterface $entityManager, + private MediaFiles $mediaFiles, ) {} /** * @inheritDoc */ - public function download(string $baseDir, string $title, string $url, ?int $downloadId): void + public function download(string $mediaType, string $title, string $url, ?int $downloadId): void { /** @var Download $downloadEntity */ $downloadEntity = $this->entityManager->getRepository(Download::class)->find($downloadId); $downloadEntity->setProgress(0); $this->entityManager->flush(); - $process = new Process([ - '/bin/sh', - '/var/www/bash/app/wget_download.sh', - $baseDir, - $title, + $downloadPreferences = $downloadEntity->getUser()->getDownloadPreferences(); + $path = $this->getDownloadPath($mediaType, $title, $downloadPreferences); + + $process = (new Process([ + 'wget', $url - ]); + ]))->setWorkingDirectory($path); $process->setTimeout(1800); // 30 min $process->setIdleTimeout(600); // 10 min @@ -61,4 +63,20 @@ class ProcessDownloader implements DownloaderInterface $this->entityManager->flush(); } + + public function getDownloadPath(string $mediaType, string $title, array $downloadPreferences): string + { + if ($mediaType === 'movies') { + if ((bool) $downloadPreferences['movie_folder']->getPreferenceValue() === true) { + return $this->mediaFiles->createMovieDirectory($title); + } + return $this->mediaFiles->getMoviesPath(); + } + + if ($mediaType === 'tvshows') { + return $this->mediaFiles->createTvShowDirectory($title); + } + + throw new \Exception("There is no download path for media type: $mediaType"); + } } \ No newline at end of file diff --git a/src/Monitor/Framework/Controller/ApiController.php b/src/Monitor/Framework/Controller/ApiController.php index a9cd133..0f0a0eb 100644 --- a/src/Monitor/Framework/Controller/ApiController.php +++ b/src/Monitor/Framework/Controller/ApiController.php @@ -17,8 +17,6 @@ class ApiController extends AbstractController public function __construct( #[Autowire(service: 'twig')] private readonly Environment $renderer, - private readonly HubInterface $hub, - private readonly Security $security, ) {} #[Route('/api/monitor', name: 'api_monitor', methods: ['POST'])] @@ -28,12 +26,12 @@ class ApiController extends AbstractController HubInterface $hub, ) { $command = $input->toCommand(); - $command->userId = $this->security->getUser()->getId(); + $command->userId = $this->getUser()->getId(); $response = $handler->handle($command); $hub->publish(new Update( 'alerts', - $this->renderer->render('Alert.stream.html.twig', [ + $this->renderer->render('broadcast/Alert.stream.html.twig', [ 'alert_id' => uniqid(), 'title' => 'Success', 'message' => "New monitor added for {$input->title}", diff --git a/src/Monitor/Service/MediaFiles.php b/src/Monitor/Service/MediaFiles.php index 9ce4c07..e5eb36f 100644 --- a/src/Monitor/Service/MediaFiles.php +++ b/src/Monitor/Service/MediaFiles.php @@ -32,6 +32,17 @@ class MediaFiles $this->filesystem = $filesystem; } + public function getPathByType(string $mediaType): string + { + if ('movies' === $mediaType) { + return $this->moviesPath; + } elseif ('tvshows' === $mediaType) { + return $this->tvShowsPath; + } + + throw new \Exception(sprintf('A path for media type %s does not exist.', $mediaType)); + } + public function getMoviesPath(): string { return $this->moviesPath; @@ -83,4 +94,35 @@ class MediaFiles return Map::from($results); } + + public function createMovieDirectory(string $path): string + { + $path = $this->moviesPath . DIRECTORY_SEPARATOR . $path; + + if (false === $this->filesystem->exists($path)) { + $this->filesystem->mkdir($path); + } + + return $path; + } + + public function createTvShowDirectory(string $path): string + { + $path = $this->tvShowsPath . DIRECTORY_SEPARATOR . $path; + + if (false === $this->filesystem->exists($path)) { + $this->filesystem->mkdir($path); + } + + return $path; + } + + public function createDirectory(string $path): string + { + if (false === $this->filesystem->exists($path)) { + $this->filesystem->mkdir($path); + } + + return $path; + } }