terrarium-web/src/Controller/EventController.php

65 lines
2.0 KiB
PHP

<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Filesystem\Filesystem;
use App\Motion\SnapshotRepository;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class EventController extends AbstractController
{
/**
* @Route("/events/{date}", name="events")
*/
public function events(SnapshotRepository $snapshotRepository, string $date = null): Response
{
$date = $date ?? date('Y-m-d');
$snapshots = $snapshotRepository->find([
'order' => 'DESC',
]);
$days = [];
foreach ($snapshots as $snapshot) {
$day = $snapshot->getDate()->format('d/m');
if (!in_array($day, $days)) {
$days[$snapshot->getDate()->format('Y-m-d')] = $day;
}
}
return $this->render('events.html.twig', [
'snapshots' => $snapshots,
'days' => $days,
'date' => $date,
]);
}
/**
* @Route("/download_snapshot", name="download_snapshot")
*/
public function downloadSnapshot(SnapshotRepository $snapshotRepository, Request $request): BinaryFileResponse
{
$src = ltrim($request->query->get('src'), '/');
$snapshots = $snapshotRepository->find();
foreach ($snapshots as $snapshot) {
if ($snapshot->getMovie() === $src) {
return new BinaryFileResponse($src, 200, [
'Content-Type' => 'application/octect-stream',
'Content-Length' => filesize($src),
'Content-Disposition' => sprintf('attachment; filename="%s"', basename($src)),
'Content-Transfer-Encoding' => 'binary',
]);
}
}
throw $this->createNotFoundException();
}
}