From 13f974cc59e33c3bb0250b15758a367a9f92dcb3 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 29 Jan 2019 19:48:15 +0300 Subject: [PATCH 1/3] Removed annotations router config. --- config/routes/annotations.yaml | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 config/routes/annotations.yaml diff --git a/config/routes/annotations.yaml b/config/routes/annotations.yaml deleted file mode 100644 index d49a502..0000000 --- a/config/routes/annotations.yaml +++ /dev/null @@ -1,3 +0,0 @@ -controllers: - resource: ../../src/Controller/ - type: annotation From 47a671c493140bfc5edc9f03cd25dd090933aa6f Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 29 Jan 2019 20:09:58 +0300 Subject: [PATCH 2/3] MagnetExtension for Twig refactored: MagnetGenerator extracted to separate service. --- src/Magnet/MagnetGenerator.php | 36 ++++++++++++++++++++++++++++++++ src/Twig/MagnetExtension.php | 33 ++++++++++------------------- templates/torrent_list.html.twig | 2 +- templates/torrent_show.html.twig | 2 +- 4 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 src/Magnet/MagnetGenerator.php diff --git a/src/Magnet/MagnetGenerator.php b/src/Magnet/MagnetGenerator.php new file mode 100644 index 0000000..3482971 --- /dev/null +++ b/src/Magnet/MagnetGenerator.php @@ -0,0 +1,36 @@ +publicTrackers = $publicTrackers; + } + + public function generate(string $infoHash, ?string $name = null, bool $withTrackers = true): string + { + $url = sprintf(self::MAGNET_TEMPLATE, urlencode($infoHash)); + + if (null !== $name) { + $url .= '&dn='.urlencode($name); + } + + if ($withTrackers) { + foreach ($this->publicTrackers as $tracker) { + $url .= '&tr='.urlencode($tracker); + } + } + + return $url; + } +} \ No newline at end of file diff --git a/src/Twig/MagnetExtension.php b/src/Twig/MagnetExtension.php index f935919..2d7bff3 100644 --- a/src/Twig/MagnetExtension.php +++ b/src/Twig/MagnetExtension.php @@ -2,42 +2,31 @@ namespace App\Twig; +use App\Magnet\MagnetGenerator; use Twig\Extension\AbstractExtension; -use Twig\TwigFunction; +use Twig\{TwigFilter, TwigFunction}; class MagnetExtension extends AbstractExtension { - private const MAGNET_TEMPLATE = 'magnet:?xt=urn:btih:%s&dn=%s'; + /** @var MagnetGenerator */ + private $magnetGenerator; - /** @var string[] Array of public trackers which will be added to the magnet link */ - private $publicTrackers = []; - - /** - * @param string[] $publicTrackers - */ - public function __construct(array $publicTrackers = []) + public function __construct(MagnetGenerator $magnetGenerator) { - $this->publicTrackers = $publicTrackers; + $this->magnetGenerator = $magnetGenerator; } - public function getFunctions(): array { return [ - new TwigFunction('magnet', [$this, 'createMagnet']), + new TwigFunction('magnet', [$this->magnetGenerator, 'generate']), ]; } - public function createMagnet(string $name, string $infoHash, bool $addPublicTrackers = true): string + public function getFilters() { - $magnetUrl = sprintf(self::MAGNET_TEMPLATE, urlencode($infoHash), urlencode($name)); - - if ($addPublicTrackers) { - foreach ($this->publicTrackers as $tracker) { - $magnetUrl .= '&tr='.urlencode($tracker); - } - } - - return $magnetUrl; + return [ + new TwigFilter('magnet', [$this->magnetGenerator, 'generate']), + ]; } } \ No newline at end of file diff --git a/templates/torrent_list.html.twig b/templates/torrent_list.html.twig index 1654d4d..6a6fe01 100644 --- a/templates/torrent_list.html.twig +++ b/templates/torrent_list.html.twig @@ -19,7 +19,7 @@ {# @var torrent \App\Magnetico\Entity\Torrent #} {% for torrent in torrents %} - 🔗 + 🔗 {{ torrent.name }} {{ torrent.totalSize | readable_size }} {{ torrent.discoveredOn | date('Y-m-d H:i:s')}} diff --git a/templates/torrent_show.html.twig b/templates/torrent_show.html.twig index 3c49c47..eaa6964 100644 --- a/templates/torrent_show.html.twig +++ b/templates/torrent_show.html.twig @@ -10,7 +10,7 @@ Hash - {{ torrent.infoHash }} + {{ torrent.infoHash }} From 03a10aa2314ccc85b5f50883ba2322d07d086841 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 29 Jan 2019 20:32:16 +0300 Subject: [PATCH 3/3] MagnetRedirectController added (for working magnet-links in the Telegram bot). --- config/routes.yaml | 9 +++++++++ src/Controller/MagnetRedirectController.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/Controller/MagnetRedirectController.php diff --git a/config/routes.yaml b/config/routes.yaml index 15131fa..6b1edf1 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -16,6 +16,15 @@ torrents_show: method: GET id: '\d+' +# Mainly for Telegram bot +magnet_redirect: + path: /magnet/{infoHash} + controller: App\Controller\MagnetRedirectController::redirect + requirements: + method: GET + # SHA-1 hash + infoHash: '[0-9a-fA-F]{40}' + user_register: path: /register/{inviteCode} controller: App\Controller\UserController::register diff --git a/src/Controller/MagnetRedirectController.php b/src/Controller/MagnetRedirectController.php new file mode 100644 index 0000000..d3d0117 --- /dev/null +++ b/src/Controller/MagnetRedirectController.php @@ -0,0 +1,14 @@ +generate($infoHash), RedirectResponse::HTTP_TEMPORARY_REDIRECT); + } +}