mirror of
https://github.com/YGGverse/YGGtracker.git
synced 2026-03-31 17:15:38 +00:00
178 lines
No EOL
4.4 KiB
PHP
178 lines
No EOL
4.4 KiB
PHP
<?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 Symfony\Component\HttpKernel\KernelInterface;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
class TorrentService
|
|
{
|
|
private KernelInterface $kernelInterface;
|
|
private EntityManagerInterface $entityManagerInterface;
|
|
|
|
public function __construct(
|
|
KernelInterface $kernelInterface,
|
|
EntityManagerInterface $entityManagerInterface
|
|
)
|
|
{
|
|
$this->kernelInterface = $kernelInterface;
|
|
$this->entityManagerInterface = $entityManagerInterface;
|
|
}
|
|
|
|
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)
|
|
);
|
|
|
|
$filesystem = new Filesystem();
|
|
$filesystem->copy(
|
|
$filepath,
|
|
sprintf(
|
|
'%s/var/torrents/%s.torrent',
|
|
$this->kernelInterface->getProjectDir(),
|
|
implode('/', str_split($torrent->getId()))
|
|
)
|
|
);
|
|
|
|
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->entityManagerInterface->persist($torrent);
|
|
$this->entityManagerInterface->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->entityManagerInterface->persist($torrentLocales);
|
|
$this->entityManagerInterface->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->entityManagerInterface->persist($torrentSensitive);
|
|
$this->entityManagerInterface->flush();
|
|
|
|
return $torrentSensitive;
|
|
}
|
|
} |