From a02d7685cc49b77b40e8ed3bc724ee4df8891fe6 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Fri, 22 Jun 2018 18:16:55 +0300 Subject: [PATCH] Twig extensions added. 'magnet' function implemented for magnet-links easy generation. 'readable_size' filter implemented to show human-readable torrent and file sizes. Templates changed to use new features. Public torrent trackers list implemented. It is stored now in the 'config/public_trackers.json' file and is injected using constructor. Some small cleanup in configs. --- config/public_trackers.json | 7 ++++ config/services.yaml | 10 ++--- src/Twig/HumanReadableSizeExtension.php | 52 +++++++++++++++++++++++++ src/Twig/MagnetExtension.php | 43 ++++++++++++++++++++ templates/torrent_list.html.twig | 4 +- templates/torrent_show.html.twig | 6 +-- 6 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 config/public_trackers.json create mode 100644 src/Twig/HumanReadableSizeExtension.php create mode 100644 src/Twig/MagnetExtension.php diff --git a/config/public_trackers.json b/config/public_trackers.json new file mode 100644 index 0000000..50138e1 --- /dev/null +++ b/config/public_trackers.json @@ -0,0 +1,7 @@ +[ + "udp://tracker.opentrackr.org:1337/announce", + "udp://tracker.open-internet.nl:6969/announce", + "udp://tracker.piratepublic.com:1337/announce", + "udp://9.rarbg.to:2710/announce", + "udp://tracker.zer0day.to:1337/announce" +] diff --git a/config/services.yaml b/config/services.yaml index 99d73aa..6aff2bd 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -2,6 +2,7 @@ # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration parameters: locale: 'en' + env(TRACKER_LIST_FILE): '%kernel.project_dir%/config/public_trackers.json' services: # default configuration for services in *this* file @@ -11,18 +12,13 @@ services: public: false # Allows optimizing the container by removing unused services; this also means # fetching services directly from the container via $container->get() won't work. # The best practice is to be explicit about your dependencies anyway. + bind: + $publicTrackers: '%env(json:file:resolve:TRACKER_LIST_FILE)%' - # makes classes in src/ available to be used as services - # this creates a service per class whose id is the fully-qualified class name App\: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests,Kernel.php}' - # controllers are imported separately to make sure services can be injected - # as action arguments even if you don't extend any base controller class App\Controller\: resource: '../src/Controller' tags: ['controller.service_arguments'] - - # add more service definitions when explicit configuration is needed - # please note that last definitions always *replace* previous ones diff --git a/src/Twig/HumanReadableSizeExtension.php b/src/Twig/HumanReadableSizeExtension.php new file mode 100644 index 0000000..212b418 --- /dev/null +++ b/src/Twig/HumanReadableSizeExtension.php @@ -0,0 +1,52 @@ +binaryPrefix = $useBinaryPrefix; + } + + public function getFilters(): array + { + return [ + new TwigFilter('readable_size', [$this, 'humanizeSize']), + ]; + } + + public function humanizeSize(int $bytes, int $decimals = 2, bool $forceBinary = false): string + { + $bytesString = (string) $bytes; + + $factor = (int) floor((strlen($bytesString) - 1) / 3); + + $isBinary = $forceBinary ?: $this->binaryPrefix; + $sizeDivider = $isBinary ? self::DIVIDER_BINARY : self::DIVIDER_COMMON; + + $maxSuffixIndex = count(self::SIZE_SUFFIXES) - 1; + + if ($maxSuffixIndex >= $factor) { + $suffixIndex = $factor; + } else { + $suffixIndex = $maxSuffixIndex; + } + + $suffix = self::SIZE_SUFFIXES[$suffixIndex]; + + return sprintf("%.{$decimals}f %s", $bytes / ($sizeDivider ** $suffixIndex), $suffix); + } +} \ No newline at end of file diff --git a/src/Twig/MagnetExtension.php b/src/Twig/MagnetExtension.php new file mode 100644 index 0000000..f935919 --- /dev/null +++ b/src/Twig/MagnetExtension.php @@ -0,0 +1,43 @@ +publicTrackers = $publicTrackers; + } + + + public function getFunctions(): array + { + return [ + new TwigFunction('magnet', [$this, 'createMagnet']), + ]; + } + + public function createMagnet(string $name, string $infoHash, bool $addPublicTrackers = true): string + { + $magnetUrl = sprintf(self::MAGNET_TEMPLATE, urlencode($infoHash), urlencode($name)); + + if ($addPublicTrackers) { + foreach ($this->publicTrackers as $tracker) { + $magnetUrl .= '&tr='.urlencode($tracker); + } + } + + return $magnetUrl; + } +} \ No newline at end of file diff --git a/templates/torrent_list.html.twig b/templates/torrent_list.html.twig index 414f0d8..dba94ad 100644 --- a/templates/torrent_list.html.twig +++ b/templates/torrent_list.html.twig @@ -17,10 +17,10 @@ {{ torrent.name }} - {{ (torrent.totalSize / 1024 / 1024) | round(2, 'ceil')}} MB + {{ torrent.totalSize | readable_size }} {{ torrent.discoveredOn | date('Y-m-d H:i:s')}} - 🔗 + 🔗 {% endfor %} diff --git a/templates/torrent_show.html.twig b/templates/torrent_show.html.twig index a781251..d26b248 100644 --- a/templates/torrent_show.html.twig +++ b/templates/torrent_show.html.twig @@ -10,12 +10,12 @@ Hash - {{ torrent.infoHashAsHex }} + {{ torrent.infoHashAsHex }} Size - {{ (torrent.totalSize / 1024 / 1024) | round(2, 'ceil')}} MB + {{ torrent.totalSize | readable_size }} Discovered @@ -35,7 +35,7 @@ {% for file in torrent.files | sort %} {{ file.path }} - {{ (file.size / 1024 / 1024) | round(2, 'ceil')}} MB + {{ file.size | readable_size }} {% endfor %}