Browse Source

draft torrent info page with related features #11

main
ghost 1 year ago
parent
commit
387acb59b6
  1. 6
      config/services.yaml
  2. 14
      public/asset/default/css/framework.css
  3. 28
      src/Controller/TorrentController.php
  4. 10
      src/Repository/TorrentRepository.php
  5. 20
      src/Service/TorrentService.php
  6. 55
      src/Twig/AppExtension.php
  7. 221
      templates/default/torrent/info.html.twig

6
config/services.yaml

@ -34,3 +34,9 @@ services:
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones
App\Twig\AppExtension:
arguments:
- '@service_container'
tags:
- { name: twig.extension}

14
public/asset/default/css/framework.css

@ -99,6 +99,12 @@ a.label-green:hover {
background-color: #65916d; background-color: #65916d;
} }
.button {
border: transparent 1px solid;
border-radius: 3px;
padding: 8px;
}
.button-green { .button-green {
color: #fff; color: #fff;
background-color: #65916d; background-color: #65916d;
@ -206,8 +212,12 @@ a:visited.background-color-hover-night-light:hover {
cursor: help; cursor: help;
} }
.font-width-normal { .font-weight-normal {
font-weight: normal; font-weight: normal
}
.font-weight-200 {
font-weight: 200
} }
.font-size-10 { .font-size-10 {

28
src/Controller/TorrentController.php

@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService; use App\Service\UserService;
use App\Service\TorrentService; use App\Service\TorrentService;
use App\Service\TimeService;
class TorrentController extends AbstractController class TorrentController extends AbstractController
{ {
@ -28,8 +29,10 @@ class TorrentController extends AbstractController
)] )]
public function info( public function info(
Request $request, Request $request,
TranslatorInterface $translator,
UserService $userService, UserService $userService,
TorrentService $torrentService TorrentService $torrentService,
TimeService $timeService
): Response ): Response
{ {
// Init user // Init user
@ -37,8 +40,29 @@ class TorrentController extends AbstractController
$request->getClientIp() $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', [ 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')),
]); ]);
} }

10
src/Repository/TorrentRepository.php

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

20
src/Service/TorrentService.php

@ -29,6 +29,19 @@ class TorrentService
$this->entityManagerInterface = $entityManagerInterface; $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 public function decodeTorrentByFilepath(string $filepath): array
{ {
$decoder = new \BitTorrent\Decoder(); $decoder = new \BitTorrent\Decoder();
@ -70,6 +83,13 @@ class TorrentService
return ''; return '';
} }
public function getTorrent(int $id): ?Torrent
{
return $this->entityManagerInterface
->getRepository(Torrent::class)
->findOneByIdField($id);
}
public function submit( public function submit(
string $filepath, string $filepath,
int $userId, int $userId,

55
src/Twig/AppExtension.php

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

221
templates/default/torrent/info.html.twig

@ -1,2 +1,221 @@
{% extends 'default/layout.html.twig' %} {% extends 'default/layout.html.twig' %}
{% block title %}{{ title }} - {{ name }}{% endblock %} {% block title %}{{ 'Torrent'|trans }} #{{ torrent.id }} - {{ name }}{% endblock %}
{% block main_content %}
<div class="padding-24-px margin-y-8-px border-radius-3-px background-color-night">
<div class="padding-b-16-px">
<h1>{{ 'Torrent'|trans }} #{{ torrent.id }}</h1>
<a class="float-right margin-l-8-px" href="#" title="{{ 'Bookmark'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
</a>
{#
<a class="float-right margin-l-8-px" href="#" title="{{ 'Torrent'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8zm15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8.5 4.5a.5.5 0 0 0-1 0v5.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V4.5z"/>
</svg>
</a>
<a class="float-right margin-l-8-px" href="#" title="{{ 'Magnet'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M8 1a7 7 0 0 0-7 7v3h4V8a3 3 0 0 1 6 0v3h4V8a7 7 0 0 0-7-7Zm7 11h-4v3h4v-3ZM5 12H1v3h4v-3ZM0 8a8 8 0 1 1 16 0v8h-6V8a2 2 0 1 0-4 0v8H0V8Z"/>
</svg>
</a>
#}
</div>
<table class="width-100">
<tbody>
{% if file.info.name is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Name'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.info.name }}
</td>
</tr>
{% endif %}
{% if file['creation date'] is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Created'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file['creation date'] | format_date }}
</td>
</tr>
{% endif %}
{% if file['created by'] is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Generated'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file['created by'] }}
</td>
</tr>
{% endif %}
{% if file.encoding is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Encoding'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.encoding }}
</td>
</tr>
{% endif %}
{% if file.comment is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Comment'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.comment }}
</td>
</tr>
{% endif %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Files'|trans }}
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td class="padding-y-4-px font-size-12" colspan="2">
<pre>/..</pre>
</td>
</tr>
{% for info in file.info.files %}
<tr>
<td class="padding-y-4-px font-size-12">
{% for path in info.path %}
<pre>../{{ path }}</pre>
{% endfor %}
</td>
<td class="padding-y-4-px font-size-12 text-right">
<pre>{{ info.length | format_bytes }}</pre>
</td>
</tr>
{% endfor %}
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Trackers'|trans }}
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
{% for tracker in trackers %}
<tr>
<td class="padding-y-4-px font-size-12" colspan="2">
{{ tracker }}
</td>
</tr>
{% endfor %}
<tr>
<td class="padding-y-4-px font-size-12">
{{ file.announce }}
</td>
<td class="padding-y-4-px text-right">
{% if file.announce not in trackers %}
<span title="{{ 'Blocked'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" fill="currentColor" viewBox="0 0 16 16">
<path d="M1.5 1.5A.5.5 0 0 1 2 1h12a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.128.334L10 8.692V13.5a.5.5 0 0 1-.342.474l-3 1A.5.5 0 0 1 6 14.5V8.692L1.628 3.834A.5.5 0 0 1 1.5 3.5v-2z"/>
</svg>
</svg>
{% endif %}
</td>
</tr>
{% for announces in file['announce-list'] %}
{% for announce in announces %}
<tr>
<td class="padding-y-4-px font-size-12">
{{ announce }}
</td>
<td class="padding-y-4-px text-right">
{% if announce not in trackers %}
<span title="{{ 'Blocked'|trans }}">
<svg xmlns="http://www.w3.org/2000/svg" width="13" height="13" fill="currentColor" viewBox="0 0 16 16">
<path d="M1.5 1.5A.5.5 0 0 1 2 1h12a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.128.334L10 8.692V13.5a.5.5 0 0 1-.342.474l-3 1A.5.5 0 0 1 6 14.5V8.692L1.628 3.834A.5.5 0 0 1 1.5 3.5v-2z"/>
</svg>
</svg>
{% endif %}
</td>
</tr>
{% endfor %}
{% endfor %}
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Locales'|trans }}
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
{% if torrent.locales %}
<tr>
<td class="text-right" colspan="2">
<a class="button button-green" href="#">
{{'Edit'|trans }}
</a>
</td>
</tr>
{% else %}
<tr>
<td class="text-right" colspan="2">
<a class="button button-green" href="#">
{{'Add'|trans }}
</a>
</td>
</tr>
{% endif %}
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Pages'|trans }}
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
{% if torrent.pages %}
<tr>
<td class="text-right" colspan="2">
<a class="button button-green" href="#">
{{'Edit'|trans }}
</a>
</td>
</tr>
{% else %}
<tr>
<td class="text-right" colspan="2">
<a class="button button-green" href="#">
{{'Add'|trans }}
</a>
</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
{% endblock %}
Loading…
Cancel
Save