Browse Source

replace bencode library to rhilip/bencode, fix files tree builder #11

main
ghost 1 year ago
parent
commit
86e1455c6b
  1. 1
      README.md
  2. 2
      composer.json
  3. 103
      composer.lock
  4. 4
      public/asset/default/css/common.css
  5. 8
      public/asset/default/css/framework.css
  6. 2
      src/Controller/PageController.php
  7. 45
      src/Controller/TorrentController.php
  8. 37
      src/Service/TorrentService.php
  9. 83
      templates/default/torrent/info.html.twig

1
README.md

@ -48,6 +48,7 @@ git checkout -b my-pr-branch-name @@ -48,6 +48,7 @@ git checkout -b my-pr-branch-name
* [SVG icons](https://icons.getbootstrap.com)
* [PHP Scrapper](https://github.com/medariox/scrapeer)
* [PHP Bencode Library](https://github.com/Rhilip/Bencode)
* [Identicons](https://github.com/dmester/jdenticon-php)
#### Feedback

2
composer.json

@ -9,7 +9,6 @@ @@ -9,7 +9,6 @@
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"christeredvartsen/php-bittorrent": "^2.0",
"doctrine/annotations": "^2.0",
"doctrine/doctrine-bundle": "^2.10",
"doctrine/doctrine-migrations-bundle": "^3.2",
@ -17,6 +16,7 @@ @@ -17,6 +16,7 @@
"jdenticon/jdenticon": "^1.0",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpdoc-parser": "^1.24",
"rhilip/bencode": "^2.3",
"symfony/asset": "6.3.*",
"symfony/console": "6.3.*",
"symfony/crowdin-translation-provider": "6.3.*",

103
composer.lock generated

@ -4,58 +4,8 @@ @@ -4,58 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "42f94769f35af5500e3ae6ce20dcb64e",
"content-hash": "d46bb514c4109b10e7327ef9f913c11a",
"packages": [
{
"name": "christeredvartsen/php-bittorrent",
"version": "v2.0.0",
"source": {
"type": "git",
"url": "https://github.com/christeredvartsen/php-bittorrent.git",
"reference": "1e4f17ef840cd56b10e9c507df0064048fc91778"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/christeredvartsen/php-bittorrent/zipball/1e4f17ef840cd56b10e9c507df0064048fc91778",
"reference": "1e4f17ef840cd56b10e9c507df0064048fc91778",
"shasum": ""
},
"require": {
"php": ">=7.2"
},
"require-dev": {
"phploc/phploc": "^5.0",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8.5"
},
"type": "library",
"autoload": {
"psr-4": {
"BitTorrent\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christer Edvartsen",
"email": "cogo@starzinger.net"
}
],
"description": "A set of components that can be used to interact with torrent files (read+write) and classes that can encode/decode to/from the BitTorrent format.",
"homepage": "https://github.com/christeredvartsen/php-bittorrent",
"keywords": [
"bittorrent",
"torrent"
],
"support": {
"issues": "https://github.com/christeredvartsen/php-bittorrent/issues",
"source": "https://github.com/christeredvartsen/php-bittorrent"
},
"time": "2020-01-21T19:12:01+00:00"
},
{
"name": "doctrine/annotations",
"version": "2.0.1",
@ -2184,6 +2134,57 @@ @@ -2184,6 +2134,57 @@
},
"time": "2021-07-14T16:46:02+00:00"
},
{
"name": "rhilip/bencode",
"version": "v2.3.3",
"source": {
"type": "git",
"url": "https://github.com/Rhilip/Bencode.git",
"reference": "fd37d13bb745352d40879dbbfa6da85af91e49f1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Rhilip/Bencode/zipball/fd37d13bb745352d40879dbbfa6da85af91e49f1",
"reference": "fd37d13bb745352d40879dbbfa6da85af91e49f1",
"shasum": ""
},
"require": {
"php": "^7.3|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"suggest": {
"php-64bit": "Running 64 bit is recommended to prevent integer overflow"
},
"type": "library",
"autoload": {
"psr-4": {
"Rhilip\\Bencode\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Rhilip",
"email": "rhilipruan@gmail.com"
}
],
"description": "A pure and simple PHP library for encoding and decoding Bencode data",
"keywords": [
"bencode",
"bittorrent",
"torrent"
],
"support": {
"issues": "https://github.com/Rhilip/Bencode/issues",
"source": "https://github.com/Rhilip/Bencode/tree/v2.3.3"
},
"time": "2023-05-27T01:47:00+00:00"
},
{
"name": "symfony/asset",
"version": "v6.3.0",

4
public/asset/default/css/common.css

@ -101,6 +101,10 @@ input[type="submit"] { @@ -101,6 +101,10 @@ input[type="submit"] {
cursor: pointer;
}
ul {
list-style: none;
}
td {
padding: 2px 0;
}

8
public/asset/default/css/framework.css

@ -245,6 +245,10 @@ a:visited.background-color-hover-night-light:hover { @@ -245,6 +245,10 @@ a:visited.background-color-hover-night-light:hover {
padding: 4px;
}
.padding-l-4-px {
padding-left: 4px;
}
.padding-t-4-px {
padding-top: 4px;
}
@ -273,6 +277,10 @@ a:visited.background-color-hover-night-light:hover { @@ -273,6 +277,10 @@ a:visited.background-color-hover-night-light:hover {
padding: 8px;
}
.padding-l-8-px {
padding-left: 8px;
}
.padding-t-8-px {
padding-top: 8px;
}

2
src/Controller/PageController.php

@ -189,7 +189,7 @@ class PageController extends AbstractController @@ -189,7 +189,7 @@ class PageController extends AbstractController
continue;
}
if (empty($torrentService->getTorrentFilenameByFilepath($file->getPathName())))
if (empty($torrentService->getTorrentInfoNameByFilepath($file->getPathName())))
{
$form['torrent']['error'][] = $translator->trans('Could not parse torrent file');

45
src/Controller/TorrentController.php

@ -40,28 +40,63 @@ class TorrentController extends AbstractController @@ -40,28 +40,63 @@ class TorrentController extends AbstractController
$request->getClientIp()
);
// Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('id')))
{
throw $this->createNotFoundException();
}
// Init file
try
{
$file = \Rhilip\Bencode\TorrentFile::load(
$torrentService->getStoragePathById(
$torrent->getId()
)
);
}
catch (ParseException $e)
{
throw $this->createNotFoundException();
}
/*
if (!$torrent = $torrentService->getTorrentLocales($request->get('id')))
{
throw $this->createNotFoundException();
}
*/
//print_r($file->getFileTree());exit;
return $this->render('default/torrent/info.html.twig', [
'torrent' =>
[
'id' => $torrent->getId(),
'added' => 0, // @TODO
'locales' => [], //$torrent->getLocales(),
'pages' => []
],
'file' => $torrentService->decodeTorrentById(
$torrent->getId()
),
'file' =>
[
'name' => $file->getName(),
'size' => $file->getSize(),
'count' => $file->getFileCount(),
'pieces' => $file->getPieceLength(),
'created' => $file->getCreationDate(),
'software' => $file->getCreatedBy(),
'protocol' => $file->getProtocol(),
'private' => $file->isPrivate(),
'source' => $file->getSource(),
'comment' => $file->getComment(),
'tree' => $file->getFileTree(),
'trackers' => $file->getAnnounceList(),
'hash' =>
[
'v1' => $file->getInfoHashV1(false),
'v2' => $file->getInfoHashV2(false)
],
'magnet' => $file->getMagnetLink()
],
'trackers' => explode('|', $this->getParameter('app.trackers')),
]);
}
@ -158,7 +193,7 @@ class TorrentController extends AbstractController @@ -158,7 +193,7 @@ class TorrentController extends AbstractController
$form['torrent']['error'][] = $translator->trans('Torrent file out of size limit');
}
if (empty($torrentService->getTorrentFilenameByFilepath($file->getPathName())))
if (empty($torrentService->getTorrentInfoNameByFilepath($file->getPathName())))
{
$form['torrent']['error'][] = $translator->trans('Could not parse torrent file');
}

37
src/Service/TorrentService.php

@ -29,38 +29,16 @@ class TorrentService @@ -29,38 +29,16 @@ class TorrentService
$this->entityManagerInterface = $entityManagerInterface;
}
public function decodeTorrentById(int $id): array
public function getStoragePathById(int $id): string
{
$decoder = new \BitTorrent\Decoder();
return $decoder->decodeFile(
sprintf(
return sprintf(
'%s/var/torrents/%s.torrent',
$this->kernelInterface->getProjectDir(),
implode('/', str_split($id))
)
);
}
public function decodeTorrentByFilepath(string $filepath): array
{
$decoder = new \BitTorrent\Decoder();
return $decoder->decodeFile($filepath);
}
public function getTorrentFilenameByFilepath(string $filepath): string
{
$data = $this->decodeTorrentByFilepath($filepath);
if (!empty($data['info']['name']))
{
return $data['info']['name'];
}
return $data['info']['name'];
}
/*
public function getTorrentKeywordsByFilepath(string $filepath): string
{
$data = $this->decodeTorrentByFilepath($filepath);
@ -82,6 +60,7 @@ class TorrentService @@ -82,6 +60,7 @@ class TorrentService
return '';
}
*/
public function getTorrent(int $id): ?Torrent
{
@ -100,17 +79,15 @@ class TorrentService @@ -100,17 +79,15 @@ class TorrentService
): ?Torrent
{
$torrent = $this->saveTorrent(
$this->getTorrentFilenameByFilepath($filepath),
$this->getTorrentInfoNameByFilepath($filepath),
$this->getTorrentKeywordsByFilepath($filepath)
);
$filesystem = new Filesystem();
$filesystem->copy(
$filepath,
sprintf(
'%s/var/torrents/%s.torrent',
$this->kernelInterface->getProjectDir(),
implode('/', str_split($torrent->getId()))
$this->getStoragePathById(
$torrent->getId()
)
);

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

@ -1,3 +1,24 @@ @@ -1,3 +1,24 @@
{% macro recursive_file_tree(tree) %}
{% import _self as self %}
{% for key, value in tree %}
{% if value is iterable %}
<div class="padding-l-8-px cursor-default">
<div class="padding-y-4-px">
{{ key }}
</div>
{{ self.recursive_file_tree(value) }}
</div>
{% else %}
<div class="padding-y-4-px padding-l-8-px background-color-hover-night-light cursor-default">
{{ key }}
<div class="float-right padding-x-8-px">
{{ value | format_bytes }}
</div>
</div>
{% endif %}
{% endfor %}
{% endmacro %}
{% from _self import recursive_file_tree %}
{% extends 'default/layout.html.twig' %}
{% block title %}{{ 'Torrent'|trans }} #{{ torrent.id }} - {{ name }}{% endblock %}
{% block main_content %}
@ -24,7 +45,7 @@ @@ -24,7 +45,7 @@
</div>
<table class="width-100">
<tbody>
{% if file.info.name is defined %}
{% if file.name %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Name'|trans }}
@ -32,11 +53,11 @@ @@ -32,11 +53,11 @@
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.info.name }}
{{ file.name }}
</td>
</tr>
{% endif %}
{% if file['creation date'] is defined %}
{% if file.created %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Created'|trans }}
@ -44,74 +65,70 @@ @@ -44,74 +65,70 @@
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file['creation date'] | format_date }}
{{ file.created | format_date }}
</td>
</tr>
{% endif %}
{% if file['created by'] is defined %}
{% if file.hash.v1 %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Generated'|trans }}
{{ 'Info hash v1'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file['created by'] }}
{{ file.hash.v1 }}
</td>
</tr>
{% endif %}
{% if file.encoding is defined %}
{% if file.hash.v2 %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Encoding'|trans }}
{{ 'Info hash v2'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.encoding }}
{{ file.hash.v2 }}
</td>
</tr>
{% endif %}
{% if file.comment is defined %}
{% if file['created by'] is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Comment'|trans }}
{{ 'Generated'|trans }}
</td>
</tr>
<tr>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.comment }}
{{ file['created by'] }}
</td>
</tr>
{% endif %}
{% if file.comment is defined %}
<tr>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Files'|trans }}
{{ 'Comment'|trans }}
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td class="padding-t-16-px font-size-12" colspan="2">
{{ file.comment }}
</td>
</tr>
{% endif %}
<tr>
<td class="padding-y-4-px font-size-12" colspan="2">
<pre>/..</pre>
<td class="padding-y-8-px border-bottom-default text-right" colspan="2">
{{ 'Files'|trans }}
</td>
</tr>
{% for info in file.info.files %}
{% for tree in file.tree %}
<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 class="padding-y-16-px" colspan="2">
{{ recursive_file_tree(tree) }}
</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 }}
@ -127,6 +144,7 @@ @@ -127,6 +144,7 @@
</td>
</tr>
{% endfor %}
{#
<tr>
<td class="padding-y-4-px font-size-12">
{{ file.announce }}
@ -141,14 +159,15 @@ @@ -141,14 +159,15 @@
{% endif %}
</td>
</tr>
{% for announces in file['announce-list'] %}
{% for announce in announces %}
#}
{% for announces in trackers %}
{% for tracker in announces %}
<tr>
<td class="padding-y-4-px font-size-12">
{{ announce }}
{{ tracker }}
</td>
<td class="padding-y-4-px text-right">
{% if announce not in trackers %}
{% if tracker 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"/>

Loading…
Cancel
Save