137 lines
4.3 KiB
PHP
137 lines
4.3 KiB
PHP
<?php
|
|
namespace Seria\Torrents;
|
|
|
|
use RuntimeException;
|
|
use Index\Routing\Route;
|
|
use Index\Routing\RouteHandler;
|
|
use Sasae\SasaeEnvironment;
|
|
use Seria\Auth\AuthInfo;
|
|
use Seria\Users\UsersContext;
|
|
|
|
class TorrentListRouting extends RouteHandler {
|
|
public function __construct(
|
|
private AuthInfo $authInfo,
|
|
private TorrentsContext $torrentsCtx,
|
|
private UsersContext $usersCtx,
|
|
private ?SasaeEnvironment $templating
|
|
) {}
|
|
|
|
#[Route('GET', '/available')]
|
|
public function getAvailable($response, $request) {
|
|
$users = $this->usersCtx->getUsers();
|
|
$peers = $this->torrentsCtx->getPeers();
|
|
|
|
if($request->hasParam('name')) {
|
|
$userName = (string)$request->getParam('name');
|
|
|
|
try {
|
|
$userInfo = $users->getUser($userName, 'name');
|
|
} catch(RuntimeException $ex) {
|
|
return 404;
|
|
}
|
|
} else $userInfo = null;
|
|
|
|
$url = '/available?';
|
|
if($userInfo !== null)
|
|
$url .= 'name=' . $userInfo->getName() . '&';
|
|
|
|
$startAt = -1;
|
|
$take = 20;
|
|
|
|
if($request->hasParam('start'))
|
|
$startAt = (int)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$torrents = [];
|
|
$torrentInfos = $this->torrentsCtx->getTorrents()->getTorrents(
|
|
public: $this->authInfo->isLoggedIn() ? null : true,
|
|
approved: true,
|
|
userInfo: $userInfo,
|
|
startAt: $startAt,
|
|
take: $take
|
|
);
|
|
foreach($torrentInfos as $torrentInfo)
|
|
$torrents[] = [
|
|
'info' => $torrentInfo,
|
|
'user' => $torrentInfo->hasUser() ? $users->getUser($torrentInfo->getUserId(), 'id') : null,
|
|
'complete_peers' => $peers->countCompletePeers($torrentInfo),
|
|
'incomplete_peers' => $peers->countIncompletePeers($torrentInfo),
|
|
];
|
|
|
|
return $this->templating->render('available', [
|
|
'page_url' => $url,
|
|
'torrents' => $torrents,
|
|
'filter_user' => $userInfo,
|
|
]);
|
|
}
|
|
|
|
#[Route('GET', '/pending')]
|
|
public function getPending($response, $request) {
|
|
if(!$this->authInfo->isLoggedIn())
|
|
return 403;
|
|
if(!$this->authInfo->getUserInfo()->canApproveTorrents())
|
|
return 403;
|
|
|
|
$users = $this->usersCtx->getUsers();
|
|
$peers = $this->torrentsCtx->getPeers();
|
|
|
|
$url = '/pending?';
|
|
$startAt = -1;
|
|
$take = 20;
|
|
|
|
if($request->hasParam('start'))
|
|
$startAt = (int)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
$torrents = [];
|
|
$torrentInfos = $this->torrentsCtx->getTorrents()->getTorrents(
|
|
approved: false,
|
|
startAt: $startAt,
|
|
take: $take
|
|
);
|
|
foreach($torrentInfos as $torrentInfo)
|
|
$torrents[] = [
|
|
'info' => $torrentInfo,
|
|
'user' => $torrentInfo->hasUser() ? $users->getUser($torrentInfo->getUserId(), 'id') : null,
|
|
'complete_peers' => $peers->countCompletePeers($torrentInfo),
|
|
'incomplete_peers' => $peers->countIncompletePeers($torrentInfo),
|
|
];
|
|
|
|
return $this->templating->render('pending', [
|
|
'page_url' => $url,
|
|
'torrents' => $torrents,
|
|
]);
|
|
}
|
|
|
|
#[Route('GET', '/available.php')]
|
|
public function getAvailablePHP($response, $request): void {
|
|
$query = [];
|
|
|
|
$name = (string)$request->getParam('name');
|
|
if($name !== '') $query['name'] = $name;
|
|
|
|
$start = (int)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT);
|
|
if($start > 0) $query['start'] = $start;
|
|
|
|
$url = '/available';
|
|
$query = http_build_query($query, encoding_type: PHP_QUERY_RFC3986);
|
|
if($query !== '')
|
|
$url .= '?' . $query;
|
|
|
|
$response->redirect($url, true);
|
|
}
|
|
|
|
#[Route('GET', '/pending.php')]
|
|
public function getPendingPHP($response, $request): void {
|
|
$query = [];
|
|
|
|
$start = (int)$request->getParam('start', FILTER_SANITIZE_NUMBER_INT);
|
|
if($start > 0) $query['start'] = $start;
|
|
|
|
$url = '/pending';
|
|
$query = http_build_query($query, encoding_type: PHP_QUERY_RFC3986);
|
|
if($query !== '')
|
|
$url .= '?' . $query;
|
|
|
|
$response->redirect($url, true);
|
|
}
|
|
}
|