WIP: working direct URL download

This commit is contained in:
Brock H Caldwell
2026-03-22 23:48:35 -05:00
parent 740bef97b1
commit 291f5a7b7e
3 changed files with 40 additions and 20 deletions

View File

@@ -117,6 +117,7 @@ export default class DownloadOptionTr extends HTMLTableRowElement {
'Accept': 'application/json', 'Accept': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
url: this.url,
mediaType: this.mediaType, mediaType: this.mediaType,
imdbId: this.imdbId, imdbId: this.imdbId,
season: this.season, season: this.season,

View File

@@ -2,6 +2,7 @@
namespace App\Download\Action\Handler; namespace App\Download\Action\Handler;
use Aimeos\Map;
use App\Base\Enum\MediaType; use App\Base\Enum\MediaType;
use App\Base\Service\Broadcaster; use App\Base\Service\Broadcaster;
use App\Base\Service\MediaFiles; use App\Base\Service\MediaFiles;
@@ -14,7 +15,6 @@ use App\Download\Framework\Entity\Download;
use App\Download\Framework\Repository\DownloadRepository; use App\Download\Framework\Repository\DownloadRepository;
use App\Download\Downloader\DownloaderInterface; use App\Download\Downloader\DownloaderInterface;
use App\EventLog\Action\Command\AddEventLogCommand; use App\EventLog\Action\Command\AddEventLogCommand;
use App\Library\Dto\MediaFileDto;
use App\Search\Action\Command\GetMediaInfoCommand; use App\Search\Action\Command\GetMediaInfoCommand;
use App\Search\Action\Handler\GetMediaInfoHandler; use App\Search\Action\Handler\GetMediaInfoHandler;
use App\Search\Action\Result\GetMediaInfoResult; 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\Command\GetTvShowOptionsCommand;
use App\Torrentio\Action\Handler\GetMovieOptionsHandler; use App\Torrentio\Action\Handler\GetMovieOptionsHandler;
use App\Torrentio\Action\Handler\GetTvShowOptionsHandler; 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\Torrentio\Result\TorrentioResult;
use App\User\Dto\UserPreferences;
use App\User\Dto\UserPreferencesFactory; use App\User\Dto\UserPreferencesFactory;
use App\User\Framework\Repository\UserRepository; use App\User\Framework\Repository\UserRepository;
use OneToMany\RichBundle\Contract\CommandInterface; use OneToMany\RichBundle\Contract\CommandInterface;
@@ -76,26 +79,19 @@ readonly class DownloadMediaHandler implements HandlerInterface
) )
}; };
$filter = $command->filter !== null if (null === $command->url) {
? UserPreferencesFactory::createFromArray($command->filter) $filter = $command->filter !== null
: UserPreferencesFactory::createFromUser($user); ? UserPreferencesFactory::createFromArray($command->filter)
: UserPreferencesFactory::createFromUser($user);
$matchingOption = $this->downloadOptionEvaluator->evaluateOptions($downloadOptions->results, $filter); $matchingOption = $this->findMatchingDownloadOption($download, $downloadOptions, $filter, $command);
$download->setUrl($matchingOption->url);
if ($matchingOption === null) { } else {
$download->setProgress(100); $matchingOption = Map::from($downloadOptions->results)
$download->setStatus('Failed'); ->filter(fn (TorrentioResult $result) => $result->url === $command->url)
$this->downloadRepository->getEntityManager()->flush(); ->first();
$this->broadcaster->alert( $download->setUrl($command->url);
title:'Uh oh',
message: 'No matching download options found.',
type: 'warning',
mercureAlertTopic: $command->mercureAlertTopic
);
throw new UnrecoverableMessageHandlingException('No matching download options found.', 404);
} }
$download->setUrl($matchingOption->url);
$download->setTitle($media->media->title); $download->setTitle($media->media->title);
$download->setFileName( $download->setFileName(
$this->getFilename(MediaType::from($command->mediaType), $media, $matchingOption) $this->getFilename(MediaType::from($command->mediaType), $media, $matchingOption)
@@ -146,6 +142,29 @@ readonly class DownloadMediaHandler implements HandlerInterface
return new DownloadMediaResult(200, "Success."); 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 private function getFilepath(MediaType $mediaType, GetMediaInfoResult $media, TorrentioResult $option): ?string
{ {
return match ($mediaType) { return match ($mediaType) {

View File

@@ -60,7 +60,7 @@ class ApiController extends AbstractController
)); ));
try { try {
$this->bus->dispatch($input->toCommand()); $handler->handle($input->toCommand());
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
return $this->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); return $this->json(['error' => $exception->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR);
} }