implement torrent/magnet download feature

This commit is contained in:
ghost 2023-10-09 00:30:13 +03:00
parent 5b0a7bcb69
commit 62ad522286
7 changed files with 754 additions and 221 deletions

View file

@ -59,15 +59,38 @@ class TorrentController extends AbstractController
'added' => $torrent->getAdded(),
'locales' => $torrentService->findLastTorrentLocales($torrent->getId()),
'sensitive' => $torrentService->findLastTorrentSensitive($torrent->getId())->isValue(),
'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()
)
]
],
'bookmark' =>
[
'active' => (bool) $torrentService->findTorrentBookmark(
'exist' => (bool) $torrentService->findTorrentBookmark(
$torrent->getId(),
$user->getId()
),
'total' => $torrentService->findTorrentBookmarksTotalByTorrentId(
'total' => $torrentService->findTorrentBookmarksTotalByTorrentId(
$torrent->getId()
),
)
],
'pages' => []
],
@ -235,62 +258,6 @@ class TorrentController extends AbstractController
);
}
// Torrent bookmark
#[Route(
'/{_locale}/torrent/{torrentId}/bookmark/toggle',
name: 'torrent_bookmark_toggle',
requirements:
[
'torrentId' => '\d+',
],
methods:
[
'GET'
]
)]
public function toggleBookmark(
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 torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
// Update
$torrentService->toggleTorrentBookmark(
$torrent->getId(),
$user->getId(),
time()
);
// Redirect to info page created
return $this->redirectToRoute(
'torrent_info',
[
'_locale' => $request->get('_locale'),
'torrentId' => $torrent->getId()
]
);
}
// Torrent locales
#[Route(
'/{_locale}/torrent/{torrentId}/edit/locales/{torrentLocalesId}',
@ -888,4 +855,221 @@ class TorrentController extends AbstractController
]
);
}
// Torrent bookmark
#[Route(
'/{_locale}/torrent/{torrentId}/bookmark/toggle',
name: 'torrent_bookmark_toggle',
requirements:
[
'torrentId' => '\d+',
],
methods:
[
'GET'
]
)]
public function toggleBookmark(
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 torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
// Update
$torrentService->toggleTorrentBookmark(
$torrent->getId(),
$user->getId(),
time()
);
// Redirect to info page created
return $this->redirectToRoute(
'torrent_info',
[
'_locale' => $request->get('_locale'),
'torrentId' => $torrent->getId()
]
);
}
// Torrent download file
#[Route(
'/{_locale}/torrent/{torrentId}/download/file',
name: 'torrent_download_file',
requirements:
[
'torrentId' => '\d+',
],
methods:
[
'GET'
]
)]
public function downloadFile(
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 torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId()))
{
// @TODO
throw new \Exception(
$translator->trans('File not found')
);
}
// Register download
$torrentService->registerTorrentDownloadFile(
$torrent->getId(),
$user->getId(),
time()
);
// Filter trackers
$file->setAnnounceList(
[
explode('|', $this->getParameter('app.trackers'))
]
);
$data = $file->dumpToString();
// Set headers
$response = new Response();
$response->headers->set(
'Content-type',
'application/x-bittorrent'
);
$response->headers->set(
'Content-length',
strlen($data)
);
$response->headers->set(
'Content-Disposition',
sprintf(
'attachment; filename="%s.%s.%s.torrent";',
$this->getParameter('app.name'),
$torrent->getId(),
mb_strtolower(
$file->getName()
)
)
);
$response->sendHeaders();
// Return file content
return $response->setContent($data);
}
// Torrent download magnet
#[Route(
'/{_locale}/torrent/{torrentId}/download/magnet',
name: 'torrent_download_magnet',
requirements:
[
'torrentId' => '\d+',
],
methods:
[
'GET'
]
)]
public function getMagnet(
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 torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{
throw $this->createNotFoundException();
}
if (!$file = $torrentService->readTorrentFileByTorrentId($torrent->getId()))
{
// @TODO
throw new \Exception(
$translator->trans('File not found')
);
}
// Register download
$torrentService->registerTorrentDownloadMagnet(
$torrent->getId(),
$user->getId(),
time()
);
// Filter trackers
$file->setAnnounceList(
[
explode('|', $this->getParameter('app.trackers'))
]
);
// Return magnet link
return $this->redirect(
$file->getMagnetLink()
);
}
}