diff --git a/assets/components/download-option-tr.js b/assets/components/download-option-tr.js index c50ce95..318d981 100644 --- a/assets/components/download-option-tr.js +++ b/assets/components/download-option-tr.js @@ -117,6 +117,7 @@ export default class DownloadOptionTr extends HTMLTableRowElement { 'Accept': 'application/json', }, body: JSON.stringify({ + url: this.url, mediaType: this.mediaType, imdbId: this.imdbId, season: this.season, diff --git a/src/Download/Action/Handler/DownloadMediaHandler.php b/src/Download/Action/Handler/DownloadMediaHandler.php index 6c8b891..4b52755 100644 --- a/src/Download/Action/Handler/DownloadMediaHandler.php +++ b/src/Download/Action/Handler/DownloadMediaHandler.php @@ -2,6 +2,7 @@ namespace App\Download\Action\Handler; +use Aimeos\Map; use App\Base\Enum\MediaType; use App\Base\Service\Broadcaster; use App\Base\Service\MediaFiles; @@ -14,7 +15,6 @@ use App\Download\Framework\Entity\Download; use App\Download\Framework\Repository\DownloadRepository; use App\Download\Downloader\DownloaderInterface; use App\EventLog\Action\Command\AddEventLogCommand; -use App\Library\Dto\MediaFileDto; use App\Search\Action\Command\GetMediaInfoCommand; use App\Search\Action\Handler\GetMediaInfoHandler; use App\Search\Action\Result\GetMediaInfoResult; @@ -22,7 +22,10 @@ use App\Torrentio\Action\Command\GetMovieOptionsCommand; use App\Torrentio\Action\Command\GetTvShowOptionsCommand; use App\Torrentio\Action\Handler\GetMovieOptionsHandler; use App\Torrentio\Action\Handler\GetTvShowOptionsHandler; +use App\Torrentio\Action\Result\GetMovieOptionsResult; +use App\Torrentio\Action\Result\GetTvShowOptionsResult; use App\Torrentio\Result\TorrentioResult; +use App\User\Dto\UserPreferences; use App\User\Dto\UserPreferencesFactory; use App\User\Framework\Repository\UserRepository; use OneToMany\RichBundle\Contract\CommandInterface; @@ -76,26 +79,19 @@ readonly class DownloadMediaHandler implements HandlerInterface ) }; - $filter = $command->filter !== null - ? UserPreferencesFactory::createFromArray($command->filter) - : UserPreferencesFactory::createFromUser($user); - - $matchingOption = $this->downloadOptionEvaluator->evaluateOptions($downloadOptions->results, $filter); - - if ($matchingOption === null) { - $download->setProgress(100); - $download->setStatus('Failed'); - $this->downloadRepository->getEntityManager()->flush(); - $this->broadcaster->alert( - title:'Uh oh', - message: 'No matching download options found.', - type: 'warning', - mercureAlertTopic: $command->mercureAlertTopic - ); - throw new UnrecoverableMessageHandlingException('No matching download options found.', 404); + if (null === $command->url) { + $filter = $command->filter !== null + ? UserPreferencesFactory::createFromArray($command->filter) + : UserPreferencesFactory::createFromUser($user); + $matchingOption = $this->findMatchingDownloadOption($download, $downloadOptions, $filter, $command); + $download->setUrl($matchingOption->url); + } else { + $matchingOption = Map::from($downloadOptions->results) + ->filter(fn (TorrentioResult $result) => $result->url === $command->url) + ->first(); + $download->setUrl($command->url); } - $download->setUrl($matchingOption->url); $download->setTitle($media->media->title); $download->setFileName( $this->getFilename(MediaType::from($command->mediaType), $media, $matchingOption) @@ -146,6 +142,29 @@ readonly class DownloadMediaHandler implements HandlerInterface return new DownloadMediaResult(200, "Success."); } + private function findMatchingDownloadOption( + Download $download, + GetMovieOptionsResult|GetTvShowOptionsResult $downloadOptions, + UserPreferences $filter, + CommandInterface $command + ): ?TorrentioResult { + $matchingOption = $this->downloadOptionEvaluator->evaluateOptions($downloadOptions->results, $filter); + + if ($matchingOption === null) { + $download->setProgress(100); + $download->setStatus('Failed'); + $this->downloadRepository->getEntityManager()->flush(); + $this->broadcaster->alert( + title:'Uh oh', + message: 'No matching download options found.', + type: 'warning', + mercureAlertTopic: $command->mercureAlertTopic + ); + throw new UnrecoverableMessageHandlingException('No matching download options found.', 404); + } + return $matchingOption; + } + private function getFilepath(MediaType $mediaType, GetMediaInfoResult $media, TorrentioResult $option): ?string { return match ($mediaType) { diff --git a/src/Download/Framework/Controller/ApiController.php b/src/Download/Framework/Controller/ApiController.php index 896bd4c..0ddcfbc 100644 --- a/src/Download/Framework/Controller/ApiController.php +++ b/src/Download/Framework/Controller/ApiController.php @@ -60,7 +60,7 @@ class ApiController extends AbstractController )); try { - $this->bus->dispatch($input->toCommand()); + $handler->handle($input->toCommand()); } catch (\Throwable $exception) { return $this->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); }