Skip to content

Commit

Permalink
remove pointless methods, fix file download via api app-build
Browse files Browse the repository at this point in the history
  • Loading branch information
pkly committed Feb 17, 2024
1 parent c81670e commit abbf11c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 71 deletions.
7 changes: 6 additions & 1 deletion src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Service\MascotService;
use App\Service\QBitTorrentService;
use App\Service\SplashTitleService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -40,7 +41,8 @@ public function getRssFinds(
public function downloadRss(
QBitTorrentService $service,
Result $result,
ResultRepository $repository
ResultRepository $repository,
EntityManagerInterface $em
): Response {
if (!$service->addTorrent($result)) {
return new Response(status: Response::HTTP_FORBIDDEN);
Expand All @@ -51,6 +53,7 @@ public function downloadRss(

while ($attempts > 0) {
$attempts--;
$em->clear();

if (null === ($result = $repository->find($result->getId()))) {
return new Response(status: Response::HTTP_NOT_FOUND);
Expand All @@ -59,6 +62,8 @@ public function downloadRss(
if (null !== $result->getSeenAt()) {
return new Response();
}

sleep(1);
}

return new Response(status: Response::HTTP_TOO_MANY_REQUESTS);
Expand Down
73 changes: 3 additions & 70 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

namespace App\Controller;

use App\Entity\MascotGroup;
use App\Entity\Rss\Result;
use App\Enum\SessionOptions;
use App\Repository\MascotGroupRepository;
use App\Repository\Rss\ResultRepository;
use App\Service\MascotService;
use App\Service\QBitTorrentService;
use App\Service\SplashTitleService;
use App\Traits\EntityManagerTrait;
use App\Traits\SessionTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -24,13 +18,9 @@

class DefaultController extends AbstractController
{
use SessionTrait;
use EntityManagerTrait;

public const int MASCOT_UPDATE_IN_SECONDS = 60;

public function __construct(
private readonly ResultRepository $resultRepository,
private readonly HttpClientInterface $client
) {
}
Expand All @@ -45,48 +35,9 @@ public function clearCaches(
}

#[Route('/', name: 'front.index')]
public function index(
string $BASE_TEMPLATE,
MascotService $mascotService,
SplashTitleService $splashTitleService,
MascotGroupRepository $mascotGroupRepository
): Response {
/** @var MascotGroup|null $group */
$group = $this->getSession()?->get(SessionOptions::MASCOT_GROUP->value, $mascotGroupRepository->getDefault()) ?? $mascotGroupRepository->getDefault();
// since we cannot really cache the SplInfo, we'll just fetch the last count
$lastUpdate = $this->getSession()?->get(SessionOptions::LAST_MASCOT_UPDATE->value);
$counter = $this->getSession()?->get(SessionOptions::MASCOT_COUNTER->value, 0) ?? 0;

$mascot = $mascotService->getMascot(
$counter,
$group?->getDirectories() ?? []
);

if (null === $lastUpdate) {
$this->getSession()?->set(SessionOptions::LAST_MASCOT_UPDATE->value, $lastUpdate = time());
}

if ($lastUpdate + self::MASCOT_UPDATE_IN_SECONDS < time()) {
$counter++;

if ($counter >= $mascotService->getLastMascotCount()) {
$counter = 0; // reset counter
}

$this->getSession()?->set(SessionOptions::MASCOT_COUNTER->value, $counter);
$this->getSession()?->set(SessionOptions::LAST_MASCOT_UPDATE->value, time());
}

return $this->render(
'homepage.html.twig',
[
'body_title' => $splashTitleService->getTitle(),
'mascot' => $mascot,
'mascot_groups' => $mascotGroupRepository->findAll(),
'mascot_group' => $group,
'rss_results' => $this->resultRepository->findBy(['seenAt' => null], ['id' => 'ASC'], 10),
]
);
public function index(): Response
{
return $this->render('homepage.html.twig');
}

#[Route('/recache-mascots', name: 'front.recache_mascots')]
Expand Down Expand Up @@ -127,24 +78,6 @@ public function proxyWithApi(
return $this->redirectToRoute('front.index');
}

#[Route('/mark-all-as-seen', name: 'front.mark_all_as_seen')]
public function setAllAsSeen(): RedirectResponse
{
$this->resultRepository->setAllAsSeen();

return $this->redirectToRoute('front.index');
}

#[Route('/set-mascot-group/{group}', name: 'front.set_mascot_group')]
public function setMascotGroup(
MascotGroup|null $group
): RedirectResponse {
$this->getSession()?->set(SessionOptions::MASCOT_GROUP->value, $group);
$this->getSession()?->set(SessionOptions::MASCOT_COUNTER->value, 0);

return $this->redirectToRoute('front.index');
}

/**
* Generate a download.
*/
Expand Down

0 comments on commit abbf11c

Please sign in to comment.