From f919a7ed859d51b5829c2a7e7731306a350c64d2 Mon Sep 17 00:00:00 2001 From: ghost Date: Sun, 8 Oct 2023 04:25:38 +0300 Subject: [PATCH] add torrent file validation --- src/Controller/TorrentController.php | 2 +- src/Service/TorrentService.php | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index f720fdd..2fdd907 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -45,7 +45,7 @@ class TorrentController extends AbstractController } // Read file - if (!$file = $torrentService->readTorrentFileById($torrent->getId())) + if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId())) { throw $this->createNotFoundException(); } diff --git a/src/Service/TorrentService.php b/src/Service/TorrentService.php index 1f68a3a..9a6762f 100644 --- a/src/Service/TorrentService.php +++ b/src/Service/TorrentService.php @@ -32,14 +32,22 @@ class TorrentService // Tools public function readTorrentFileByFilepath( string $filepath - ): \Rhilip\Bencode\TorrentFile + ): ?\Rhilip\Bencode\TorrentFile { - return \Rhilip\Bencode\TorrentFile::load( - $filepath - ); + try + { + return \Rhilip\Bencode\TorrentFile::load( + $filepath + ); + } + + catch (\Rhilip\Bencode\ParseException $error) + { + return null; + } } - public function readTorrentFileById( + public function readTorrentFileByTorrentId( int $id ): \Rhilip\Bencode\TorrentFile {