From b4c6a98d49a515096b33a0e7315bb7fb961bd02e Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 24 Oct 2023 01:48:26 +0300 Subject: [PATCH] fix announcement generation for wanted torrents #27 --- src/Controller/TorrentController.php | 64 ++++++++++++---------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 1c09ccc..5e97ac0 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -2418,25 +2418,40 @@ class TorrentController extends AbstractController ?\Rhilip\Bencode\TorrentFile $file, bool $yggdrasil, string $regex = '/^0{0,1}[2-3][a-f0-9]{0,2}:/' ): ?\Rhilip\Bencode\TorrentFile { + // Init trackers registry + $allTrackers = []; + // Get app trackers $appTrackers = explode('|', $this->getParameter('app.trackers')); + // Append app trackers + foreach ($appTrackers as $appTracker) + { + $allTrackers[] = $appTracker; + } + // Get original file announcements $announceList = $file->getAnnounceList(); - // Append app trackers - foreach ($appTrackers as $appTracker) + // Append original file announcements + foreach ($announceList as $announce) { - // Append application trackers - $announceList[0][] = $appTracker; + if (is_array($announce)) + { + foreach ($announce as $value) + { + $allTrackers[] = $value; + } + } - // Append application re-trackers - $announceList[1][] = $appTracker; + else + { + $allTrackers[] = $value; + } } - // Remove duplicated - $announceList[0] = array_unique($announceList[0]); - $announceList[1] = array_unique($announceList[1]); + // Remove duplicates + $allTrackers = array_unique($allTrackers); // Yggdrasil-only mode if ($yggdrasil) @@ -2450,45 +2465,20 @@ class TorrentController extends AbstractController } // Remove non-Yggdrasil trackers from announcement list - foreach ($announceList[0] as $key => $value) + foreach ($allTrackers as $key => $value) { // trackers if (!preg_match($regex, str_replace(['[',']'], false, parse_url($value, PHP_URL_HOST)))) { - unset($announceList[0][$key]); - } - } - - // Remove non-Yggdrasil re-trackers from announcement list - foreach ($announceList[1] as $key => $value) - { - // trackers - if (!preg_match($regex, str_replace(['[',']'], false, parse_url($value, PHP_URL_HOST)))) - { - unset($announceList[1][$key]); + unset($allTrackers[$key]); } } } - // Remove duplicates in list - $values = []; - - foreach ($announceList[0] as $value) - { - $values[] = $value; - } - - foreach ($announceList[1] as $value) - { - $values[] = $value; - } - - $values = array_unique($values); - // Format announce list $trackers = []; - foreach ($values as $value) + foreach ($allTrackers as $value) { $trackers[] = [$value]; }