From 63850e48fdae0b9b4a929f6f573fc25cacabb30d Mon Sep 17 00:00:00 2001 From: Brock H Caldwell Date: Sat, 7 Jun 2025 22:19:05 -0500 Subject: [PATCH] feat: shows existing files for already downloaded media --- config/services.yaml | 2 +- src/Controller/TorrentioController.php | 6 ++--- src/Monitor/Service/MediaFiles.php | 25 +++++++++++++++++++ .../Action/Handler/GetMovieOptionsHandler.php | 6 ++++- .../Action/Result/GetMovieOptionsResult.php | 1 + templates/torrentio/movies.html.twig | 9 +++++++ 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 6426eb4..04e2842 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -8,7 +8,7 @@ parameters: media.base_path: '/var/download' media.default_movies_dir: movies media.default_tvshows_dir: tvshows - media.movies_path: '%env(default:media.default_movies_dir:MOVIES_PATH)%' + media.movies_path: '/var/download/%env(default:media.default_movies_dir:MOVIES_PATH)%' media.tvshows_path: '/var/download/%env(default:media.default_tvshows_dir:TVSHOWS_PATH)%' # Mercure diff --git a/src/Controller/TorrentioController.php b/src/Controller/TorrentioController.php index 2d927b7..ad40ee7 100644 --- a/src/Controller/TorrentioController.php +++ b/src/Controller/TorrentioController.php @@ -52,13 +52,13 @@ final class TorrentioController extends AbstractController $input->episode, ); -// return $cache->get($cacheId, function (ItemInterface $item) use ($input) { -// $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); + return $cache->get($cacheId, function (ItemInterface $item) use ($input) { + $item->expiresAt(Carbon::now()->addHour()->setMinute(0)->setSecond(0)); $results = $this->getTvShowOptionsHandler->handle($input->toCommand()); return $this->render('torrentio/tvshows.html.twig', [ 'results' => $results, ]); -// }); + }); } #[Route('/torrentio/tvshows/clear/{tmdbId}/{imdbId}/{season?}/{episode?}', name: 'app_clear_torrentio_tvshows')] diff --git a/src/Monitor/Service/MediaFiles.php b/src/Monitor/Service/MediaFiles.php index 51236b3..53e2f43 100644 --- a/src/Monitor/Service/MediaFiles.php +++ b/src/Monitor/Service/MediaFiles.php @@ -158,4 +158,29 @@ class MediaFiles return false; } + + public function movieExists(string $title) + { + $filepath = $this->moviesPath . DIRECTORY_SEPARATOR . $title; + $directoryExists = $this->filesystem->exists($filepath); + + if (false === $directoryExists) { + return false; + } + + if (false === $this->finder->in($filepath)->files()->hasResults()) { + return false; + } + + $files = Map::from($this->finder->in($filepath)->files())->filter(function (SplFileInfo $file) { + $validExtensions = ['mkv', 'mp4', 'mpeg']; + return in_array($file->getExtension(), $validExtensions); + })->values(); + + if (false === $files->isEmpty()) { + return $files[0]; + } + + return false; + } } diff --git a/src/Torrentio/Action/Handler/GetMovieOptionsHandler.php b/src/Torrentio/Action/Handler/GetMovieOptionsHandler.php index 942628c..51b9398 100644 --- a/src/Torrentio/Action/Handler/GetMovieOptionsHandler.php +++ b/src/Torrentio/Action/Handler/GetMovieOptionsHandler.php @@ -2,6 +2,7 @@ namespace App\Torrentio\Action\Handler; +use App\Monitor\Service\MediaFiles; use App\Tmdb\Tmdb; use App\Torrentio\Action\Result\GetMovieOptionsResult; use App\Torrentio\Client\Torrentio; @@ -14,12 +15,15 @@ class GetMovieOptionsHandler implements HandlerInterface public function __construct( private readonly Tmdb $tmdb, private readonly Torrentio $torrentio, + private readonly MediaFiles $mediaFiles ) {} public function handle(CommandInterface $command): ResultInterface { + $media = $this->tmdb->mediaDetails($command->imdbId, 'movies'); return new GetMovieOptionsResult( - media: $this->tmdb->mediaDetails($command->imdbId, 'movies'), + media: $media, + file: $this->mediaFiles->movieExists($media->title), results: $this->torrentio->search($command->imdbId, 'movies'), ); } diff --git a/src/Torrentio/Action/Result/GetMovieOptionsResult.php b/src/Torrentio/Action/Result/GetMovieOptionsResult.php index 6d63c9a..12201b7 100644 --- a/src/Torrentio/Action/Result/GetMovieOptionsResult.php +++ b/src/Torrentio/Action/Result/GetMovieOptionsResult.php @@ -9,6 +9,7 @@ class GetMovieOptionsResult implements ResultInterface { public function __construct( public TmdbResult $media, + public bool|\SplFileInfo $file, public array $results ) {} } diff --git a/templates/torrentio/movies.html.twig b/templates/torrentio/movies.html.twig index 26203f0..5b8c34e 100644 --- a/templates/torrentio/movies.html.twig +++ b/templates/torrentio/movies.html.twig @@ -1,4 +1,13 @@
+ {% if results.file != false %} +
+

Existing file(s) for this movie:

+
    +
  • {{ results.file.realPath|strip_media_path }} — {{ results.file.size|filesize }}
  • +
+
+ {% endif %} +
{{ include('torrentio/partial/option-table.html.twig', {controller: 'movie-results'}) }}