draft torrent info page with related features #11

This commit is contained in:
ghost 2023-10-07 01:27:11 +03:00
parent 21ffd8aa01
commit 387acb59b6
7 changed files with 349 additions and 5 deletions

55
src/Twig/AppExtension.php Normal file
View file

@ -0,0 +1,55 @@
<?php
namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
protected $container;
public function __construct(
ContainerInterface $container
)
{
$this->container = $container;
}
public function getFilters()
{
return
[
new TwigFilter(
'format_bytes',
[
$this,
'formatBytes'
]
),
];
}
public function formatBytes(
int $bytes,
int $precision = 2
) : string
{
$size = [
'B',
'Kb',
'Mb',
'Gb',
'Tb',
'Pb',
'Eb',
'Zb',
'Yb'
];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
}
}