draft torrent submit form #11

This commit is contained in:
ghost 2023-10-06 03:28:36 +03:00
parent b1fe274ae8
commit 8df29ef605
12 changed files with 810 additions and 6 deletions

View file

@ -0,0 +1,181 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService;
use App\Service\TorrentService;
class TorrentController extends AbstractController
{
#[Route(
'/{_locale}/torrent/{id}',
name: 'torrent_info',
requirements:
[
'id' => '\d+'
],
methods:
[
'GET'
]
)]
public function info(
Request $request,
UserService $userService,
TorrentService $torrentService
): Response
{
// Init user
$user = $userService->init(
$request->getClientIp()
);
return $this->render('default/torrent/info.html.twig', [
'title' => 'test'
]);
}
#[Route(
'/{_locale}/submit/torrent',
name: 'torrent_submit',
methods:
[
'GET',
'POST'
]
)]
public function submit(
Request $request,
TranslatorInterface $translator,
UserService $userService,
TorrentService $torrentService
): Response
{
// Init user
$user = $userService->init(
$request->getClientIp()
);
if (!$user->isStatus())
{
// @TODO
throw new \Exception(
$translator->trans('Access denied')
);
}
// Init form
$form =
[
'locales' =>
[
'error' => [],
'attribute' =>
[
'value' => $request->get('locales') ? $request->get('locales') : [$request->get('_locale')],
'placeholder' => $translator->trans('Content language')
]
],
'torrent' =>
[
'error' => [],
'attribute' =>
[
'value' => null, // is local file, there is no values passed
'placeholder' => $translator->trans('Select torrent file')
]
],
'sensitive' =>
[
'error' => [],
'attribute' =>
[
'value' => $request->get('sensitive'),
'placeholder' => $translator->trans('Apply sensitive filters for this publication'),
]
]
];
// Process request
if ($request->isMethod('post'))
{
/// Locales
$locales = [];
if ($request->get('locales'))
{
foreach ((array) $request->get('locales') as $locale)
{
if (in_array($locale, explode('|', $this->getParameter('app.locales'))))
{
$locales[] = $locale;
}
}
}
//// At least one valid locale required
if (!$locales)
{
$form['locales']['error'][] = $translator->trans('At least one locale required');
}
/// Torrent
if ($file = $request->files->get('torrent'))
{
//// Validate torrent file
if (filesize($file->getPathName()) > $this->getParameter('app.torrent.size.max'))
{
$form['torrent']['error'][] = $translator->trans('Torrent file out of size limit');
}
if (empty($torrentService->getTorrentFilenameByFilepath($file->getPathName())))
{
$form['torrent']['error'][] = $translator->trans('Could not parse torrent file');
}
}
else
{
$form['torrent']['error'][] = $translator->trans('Torrent file required');
}
// Request is valid
if (empty($form['torrent']['error']) && empty($form['locales']['error']))
{
// Save data
$torrent = $torrentService->submit(
$file->getPathName(),
$user->getId(),
time(),
(array) $locales,
(bool) $request->get('sensitive'),
$user->isApproved()
);
// Redirect to info page created
return $this->redirectToRoute(
'torrent_info',
[
'_locale' => $request->get('_locale'),
'id' => $torrent->getId()
]
);
}
}
// Render form template
return $this->render(
'default/torrent/submit.html.twig',
[
'locales' => explode('|', $this->getParameter('app.locales')),
'form' => $form,
]
);
}
}

103
src/Entity/Torrent.php Normal file
View file

@ -0,0 +1,103 @@
<?php
namespace App\Entity;
use App\Repository\TorrentRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TorrentRepository::class)]
class Torrent
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $filename = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $keywords = null;
#[ORM\Column(nullable: true)]
private ?int $seeders = null;
#[ORM\Column(nullable: true)]
private ?int $peers = null;
#[ORM\Column(nullable: true)]
private ?int $leechers = null;
public function getId(): ?int
{
return $this->id;
}
public function setId(string $id): static
{
$this->id = $id;
return $this;
}
public function getFilename(): ?string
{
return $this->filename;
}
public function setFilename(string $filename): static
{
$this->filename = $filename;
return $this;
}
public function getKeywords(): ?string
{
return $this->keywords;
}
public function setKeywords(?string $keywords): static
{
$this->keywords = $keywords;
return $this;
}
public function getSeeders(): ?int
{
return $this->seeders;
}
public function setSeeders(?int $seeders): static
{
$this->seeders = $seeders;
return $this;
}
public function getPeers(): ?int
{
return $this->peers;
}
public function setPeers(?int $peers): static
{
$this->peers = $peers;
return $this;
}
public function getLeechers(): ?int
{
return $this->leechers;
}
public function setLeechers(?int $leechers): static
{
$this->leechers = $leechers;
return $this;
}
}

View file

@ -0,0 +1,103 @@
<?php
namespace App\Entity;
use App\Repository\TorrentLocalesRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TorrentLocalesRepository::class)]
class TorrentLocales
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?int $torrentId = null;
#[ORM\Column]
private ?int $userId = null;
#[ORM\Column]
private ?int $added = null;
#[ORM\Column(type: Types::ARRAY)]
private array $value = [];
#[ORM\Column]
private ?bool $approved = null;
public function getId(): ?int
{
return $this->id;
}
public function setId(string $id): static
{
$this->id = $id;
return $this;
}
public function getTorrentId(): ?int
{
return $this->torrentId;
}
public function setTorrentId(int $torrentId): static
{
$this->torrentId = $torrentId;
return $this;
}
public function getUserId(): ?int
{
return $this->userId;
}
public function setUserId(int $userId): static
{
$this->userId = $userId;
return $this;
}
public function getAdded(): ?int
{
return $this->added;
}
public function setAdded(int $added): static
{
$this->added = $added;
return $this;
}
public function getValue(): array
{
return $this->value;
}
public function setValue(array $value): static
{
$this->value = $value;
return $this;
}
public function isApproved(): ?bool
{
return $this->approved;
}
public function setApproved(bool $approved): static
{
$this->approved = $approved;
return $this;
}
}

View file

@ -0,0 +1,103 @@
<?php
namespace App\Entity;
use App\Repository\TorrentSensitiveRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TorrentSensitiveRepository::class)]
class TorrentSensitive
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?int $torrentId = null;
#[ORM\Column]
private ?int $userId = null;
#[ORM\Column]
private ?int $added = null;
#[ORM\Column]
private ?bool $value = null;
#[ORM\Column]
private ?bool $approved = null;
public function getId(): ?int
{
return $this->id;
}
public function setId(string $id): static
{
$this->id = $id;
return $this;
}
public function getTorrentId(): ?int
{
return $this->torrentId;
}
public function setTorrentId(int $torrentId): static
{
$this->torrentId = $torrentId;
return $this;
}
public function getUserId(): ?int
{
return $this->userId;
}
public function setUserId(int $userId): static
{
$this->userId = $userId;
return $this;
}
public function getAdded(): ?int
{
return $this->added;
}
public function setAdded(int $added): static
{
$this->added = $added;
return $this;
}
public function isValue(): ?bool
{
return $this->value;
}
public function setValue(bool $value): static
{
$this->value = $value;
return $this;
}
public function isApproved(): ?bool
{
return $this->approved;
}
public function setApproved(bool $approved): static
{
$this->approved = $approved;
return $this;
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace App\Repository;
use App\Entity\TorrentLocales;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<TorrentLocales>
*
* @method TorrentLocales|null find($id, $lockMode = null, $lockVersion = null)
* @method TorrentLocales|null findOneBy(array $criteria, array $orderBy = null)
* @method TorrentLocales[] findAll()
* @method TorrentLocales[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TorrentLocalesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TorrentLocales::class);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace App\Repository;
use App\Entity\Torrent;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Torrent>
*
* @method Torrent|null find($id, $lockMode = null, $lockVersion = null)
* @method Torrent|null findOneBy(array $criteria, array $orderBy = null)
* @method Torrent[] findAll()
* @method Torrent[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TorrentRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Torrent::class);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace App\Repository;
use App\Entity\TorrentSensitive;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<TorrentSensitive>
*
* @method TorrentSensitive|null find($id, $lockMode = null, $lockVersion = null)
* @method TorrentSensitive|null findOneBy(array $criteria, array $orderBy = null)
* @method TorrentSensitive[] findAll()
* @method TorrentSensitive[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TorrentSensitiveRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TorrentSensitive::class);
}
}

View file

@ -0,0 +1,163 @@
<?php
namespace App\Service;
use App\Entity\Torrent;
use App\Entity\TorrentLocales;
use App\Entity\TorrentSensitive;
use App\Repository\TorrentRepository;
use App\Repository\TorrentLocalesRepository;
use App\Repository\TorrentSensitiveRepository;
use Doctrine\ORM\EntityManagerInterface;
class TorrentService
{
private EntityManagerInterface $entityManager;
private ParameterBagInterface $parameterBagInterface;
public function __construct(
EntityManagerInterface $entityManager,
)
{
$this->entityManager = $entityManager;
}
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);
if (!empty($data['info']['name']))
{
return mb_strtolower(
preg_replace(
'/[\s]+/',
' ',
preg_replace(
'/[\W]+/',
' ',
$data['info']['name']
)
)
);
}
return '';
}
public function submit(
string $filepath,
int $userId,
int $added,
array $locales,
bool $sensitive,
bool $approved
): ?Torrent
{
$torrent = $this->saveTorrent(
$this->getTorrentFilenameByFilepath($filepath),
$this->getTorrentKeywordsByFilepath($filepath)
);
if (!empty($locales))
{
$this->saveTorrentLocales(
$torrent->getId(),
$userId,
$added,
$locales,
$approved
);
}
$this->saveTorrentSensitive(
$torrent->getId(),
$userId,
$added,
$sensitive,
$approved
);
return $torrent;
}
public function saveTorrent(
string $filepath,
string $keywords
): ?Torrent
{
$torrent = new Torrent();
$torrent->setFilename($filepath);
$torrent->setKeywords($keywords);
$this->entityManager->persist($torrent);
$this->entityManager->flush();
return $torrent;
}
public function saveTorrentLocales(
int $torrentId,
int $userId,
int $added,
array $value,
bool $approved
): ?TorrentLocales
{
$torrentLocales = new TorrentLocales();
$torrentLocales->setTorrentId($torrentId);
$torrentLocales->setUserId($userId);
$torrentLocales->setAdded($added);
$torrentLocales->setValue($value);
$torrentLocales->setApproved($approved);
$this->entityManager->persist($torrentLocales);
$this->entityManager->flush();
return $torrentLocales;
}
public function saveTorrentSensitive(
int $torrentId,
int $userId,
int $added,
bool $value,
bool $approved
): ?TorrentSensitive
{
$torrentSensitive = new TorrentSensitive();
$torrentSensitive->setTorrentId($torrentId);
$torrentSensitive->setUserId($userId);
$torrentSensitive->setAdded($added);
$torrentSensitive->setValue($value);
$torrentSensitive->setApproved($approved);
$this->entityManager->persist($torrentSensitive);
$this->entityManager->flush();
return $torrentSensitive;
}
}