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

View file

@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService;
use App\Service\TorrentService;
use App\Service\TimeService;
class TorrentController extends AbstractController
{
@ -28,8 +29,10 @@ class TorrentController extends AbstractController
)]
public function info(
Request $request,
TranslatorInterface $translator,
UserService $userService,
TorrentService $torrentService
TorrentService $torrentService,
TimeService $timeService
): Response
{
// Init user
@ -37,8 +40,29 @@ class TorrentController extends AbstractController
$request->getClientIp()
);
if (!$torrent = $torrentService->getTorrent($request->get('id')))
{
throw $this->createNotFoundException();
}
/*
if (!$torrent = $torrentService->getTorrentLocales($request->get('id')))
{
throw $this->createNotFoundException();
}
*/
return $this->render('default/torrent/info.html.twig', [
'title' => 'test'
'torrent' =>
[
'id' => $torrent->getId(),
'locales' => [], //$torrent->getLocales(),
'pages' => []
],
'file' => $torrentService->decodeTorrentById(
$torrent->getId()
),
'trackers' => explode('|', $this->getParameter('app.trackers')),
]);
}

View file

@ -20,4 +20,14 @@ class TorrentRepository extends ServiceEntityRepository
{
parent::__construct($registry, Torrent::class);
}
public function findOneByIdField(int $id): ?Torrent
{
return $this->createQueryBuilder('t')
->where('t.id = :id')
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult()
;
}
}

View file

@ -29,6 +29,19 @@ class TorrentService
$this->entityManagerInterface = $entityManagerInterface;
}
public function decodeTorrentById(int $id): array
{
$decoder = new \BitTorrent\Decoder();
return $decoder->decodeFile(
sprintf(
'%s/var/torrents/%s.torrent',
$this->kernelInterface->getProjectDir(),
implode('/', str_split($id))
)
);
}
public function decodeTorrentByFilepath(string $filepath): array
{
$decoder = new \BitTorrent\Decoder();
@ -70,6 +83,13 @@ class TorrentService
return '';
}
public function getTorrent(int $id): ?Torrent
{
return $this->entityManagerInterface
->getRepository(Torrent::class)
->findOneByIdField($id);
}
public function submit(
string $filepath,
int $userId,

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];
}
}