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,
]);
}
}