implement torrent search features

This commit is contained in:
ghost 2023-10-10 04:07:59 +03:00
parent e9375f9127
commit 8ab4c0b9cf
7 changed files with 293 additions and 12 deletions

View file

@ -3,29 +3,146 @@
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService;
use App\Service\PageService;
use App\Service\TorrentService;
class SearchController extends AbstractController
{
#[Route(
'/{_locale}/search',
name: 'search_index'
name: 'search_index',
methods:
[
'GET'
]
)]
public function index(Request $request): Response
public function index(
Request $request,
UserService $userService,
PageService $pageService,
TorrentService $torrentService
): Response
{
$query = $request->query->get('query');
// Init user
$user = $userService->init(
$request->getClientIp()
);
return $this->render('default/search/index.html.twig', [
'query' => $query
]);
$page = $request->query->get('page') ? (int) $request->query->get('page') : 1;
switch ($request->query->get('type'))
{
case 'page':
break;
case 'torrent':
$torrents = [];
foreach ($torrentService->searchTorrents($request->query->get('query')) as $torrent)
{
// Read file
if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId()))
{
continue; // @TODO
}
// Generate keywords
$keywords = [];
$query = explode(' ', mb_strtolower($request->query->get('query')));
foreach (explode(',', $torrent->getKeywords()) as $keyword)
{
if (in_array($keyword, $query))
{
$keywords[] = urlencode($keyword);
}
}
$torrents[] =
[
'id' => $torrent->getId(),
'added' => $torrent->getAdded(),
'file' =>
[
'name' => $file->getName(),
'size' => $file->getSize(),
],
'scrape' =>
[
'seeders' => (int) $torrent->getSeeders(),
'peers' => (int) $torrent->getPeers(),
'leechers' => (int) $torrent->getLeechers(),
],
'user' =>
[
'id' => $torrent->getUserId(),
'identicon' => $userService->identicon(
$userService->getUser(
$torrent->getUserId()
)->getAddress()
)
],
'keywords' => $keywords,
'download' =>
[
'file' =>
[
'exist' => (bool) $torrentService->findTorrentDownloadFile(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentDownloadFilesTotalByTorrentId(
$torrent->getId()
)
],
'magnet' =>
[
'exist' => (bool) $torrentService->findTorrentDownloadMagnet(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentDownloadMagnetsTotalByTorrentId(
$torrent->getId()
)
]
],
'star' =>
[
'exist' => (bool) $torrentService->findTorrentStar(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentStarsTotalByTorrentId(
$torrent->getId()
)
],
];
}
return $this->render('default/search/torrent.html.twig', [
'query' => $request->query->get('query'),
'torrents' => $torrents
]);
break;
default:
throw $this->createNotFoundException();
}
}
public function module(string $query = ''): Response
public function module(
?string $query,
?string $type
): Response
{
return $this->render('default/search/module.html.twig', [
'query' => $query,
'type' => $type,
]);
}
}

View file

@ -6,7 +6,9 @@ use App\Repository\TorrentRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
// @TODO #[ORM\Index(columns: ['keywords'], name: 'keywords_idx', flags: ['fulltext'])]
#[ORM\Entity(repositoryClass: TorrentRepository::class)]
class Torrent
{
#[ORM\Id]

View file

@ -20,4 +20,21 @@ class TorrentRepository extends ServiceEntityRepository
{
parent::__construct($registry, Torrent::class);
}
public function searchByKeywords(
array $keywords
): ?array
{
$query = $this->createQueryBuilder('t');
foreach ($keywords as $keyword)
{
$query->orWhere('t.keywords LIKE :query')
->setParameter('query', "%{$keyword}%");
}
return $query->orderBy('t.id', 'ASC') // same as t.added
->getQuery()
->getResult();
}
}

View file

@ -246,11 +246,11 @@ class TorrentService
}
// Torrent
public function getTorrent(int $id): ?Torrent
public function getTorrent(int $torrentId): ?Torrent
{
return $this->entityManagerInterface
->getRepository(Torrent::class)
->find($id);
->find($torrentId);
}
public function addTorrent(
@ -285,6 +285,15 @@ class TorrentService
);
}
public function searchTorrents(string $query) : array
{
return $this->entityManagerInterface
->getRepository(Torrent::class)
->searchByKeywords(
explode(' ', $query)
);
}
public function setTorrentsApprovedByUserId(
int $userId,
bool $value