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.

This commit is contained in:
Alexey Skobkin 2018-06-22 18:16:55 +03:00
parent 888f02687e
commit a02d7685cc
6 changed files with 110 additions and 12 deletions

View file

@ -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"
]

View file

@ -2,6 +2,7 @@
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters: parameters:
locale: 'en' locale: 'en'
env(TRACKER_LIST_FILE): '%kernel.project_dir%/config/public_trackers.json'
services: services:
# default configuration for services in *this* file # 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 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. # fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway. # 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\: App\:
resource: '../src/*' resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}' 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\: App\Controller\:
resource: '../src/Controller' resource: '../src/Controller'
tags: ['controller.service_arguments'] tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

View file

@ -0,0 +1,52 @@
<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class HumanReadableSizeExtension extends AbstractExtension
{
// Can't really exceed 'EB' on 64-bit platform but let it go
private const SIZE_SUFFIXES = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
private const DIVIDER_BINARY = 1024;
private const DIVIDER_COMMON = 1000;
/** @var bool Whether or not to use binary prefixes/suffixes */
private $binaryPrefix = false;
public function __construct(bool $useBinaryPrefix = false)
{
$this->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);
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class MagnetExtension extends AbstractExtension
{
private const MAGNET_TEMPLATE = 'magnet:?xt=urn:btih:%s&dn=%s';
/** @var string[] Array of public trackers which will be added to the magnet link */
private $publicTrackers = [];
/**
* @param string[] $publicTrackers
*/
public function __construct(array $publicTrackers = [])
{
$this->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;
}
}

View file

@ -17,10 +17,10 @@
<td> <td>
<a href="{{ path('torrent_show', {'id': torrent.id}) }}">{{ torrent.name }}</a> <a href="{{ path('torrent_show', {'id': torrent.id}) }}">{{ torrent.name }}</a>
</td> </td>
<td>{{ (torrent.totalSize / 1024 / 1024) | round(2, 'ceil')}} MB</td> <td>{{ torrent.totalSize | readable_size }}</td>
<td>{{ torrent.discoveredOn | date('Y-m-d H:i:s')}}</td> <td>{{ torrent.discoveredOn | date('Y-m-d H:i:s')}}</td>
<td> <td>
<a href="magnet:?xt=urn:btih:{{ torrent.infoHashAsHex }}&dn={{ torrent.name | url_encode }}">&#128279;</a> <a href="{{ magnet(torrent.name, torrent.infoHashAsHex) }}">&#128279;</a>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -10,12 +10,12 @@
<tr> <tr>
<td>Hash</td> <td>Hash</td>
<td> <td>
<a href="magnet:?xt=urn:btih:{{ torrent.infoHashAsHex }}&dn={{ torrent.name | url_encode }}">{{ torrent.infoHashAsHex }}</a> <a href="{{ magnet(torrent.name, torrent.infoHashAsHex) }}">{{ torrent.infoHashAsHex }}</a>
</td> </td>
</tr> </tr>
<tr> <tr>
<td>Size</td> <td>Size</td>
<td>{{ (torrent.totalSize / 1024 / 1024) | round(2, 'ceil')}} MB</td> <td>{{ torrent.totalSize | readable_size }}</td>
</tr> </tr>
<tr> <tr>
<td>Discovered</td> <td>Discovered</td>
@ -35,7 +35,7 @@
{% for file in torrent.files | sort %} {% for file in torrent.files | sort %}
<tr> <tr>
<td>{{ file.path }}</td> <td>{{ file.path }}</td>
<td>{{ (file.size / 1024 / 1024) | round(2, 'ceil')}} MB</td> <td>{{ file.size | readable_size }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>