<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Payment;
use App\Service\PaymentService;
use App\Form\PaymentType;
use App\Library\Utils\Other\Other;
use App\Service\Image\ImageService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ImageController extends BaseAbstractController
{
public function list(Request $request, PaymentService $paymentService): Response
{
$payments = $paymentService->getBaseService()->getAll();
// return $this->redirectToRoute("login");
return $this->render('admin/payment/payments.html.twig', [
'payments' => $payments,
]);
}
public function edit(Request $request, PaymentService $paymentService, EntityManagerInterface $em): Response
{
$id = $request->get('id');
/**
* @var Payment|null $payment
*/
$payment = $id ? $paymentService->getBaseService()->get($id) : null;
if (!$payment) {
$payment = (new Payment());
}
$form = $this->createForm(PaymentType::class, $payment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$payment->setCreatedBy($this->getUser());
$em->persist($payment);
$em->flush();
return $this->redirectToRoute('moderator_payments');
}
// return $this->redirectToRoute("login");
return $this->render('admin/payment/payment_edit.html.twig', [
'form' => $form->createView(),
'payment' => $payment,
]);
}
public function view(Request $request, ImageService $imageService, EntityManagerInterface $em): Response
{
$id = $request->get('id');
/**
* @var Image|null $image
*/
$image = $id ? $imageService->getBaseService()->get($id) : null;
try {
$projectDir = $this->getParameter('kernel.project_dir');
$filePath = $imageService->getRealPath($image);
// Security: prevent directory traversal
// if (str_contains($filename, '..') || str_contains($filename, '/')) {
// throw $this->createNotFoundException('Invalid filename');
// }
// Check if file exists and is readable
if (!file_exists($filePath) || !is_readable($filePath)) {
throw $this->createNotFoundException('Image not found');
}
// Check if it's actually an image
$mimeType = mime_content_type($filePath);
if (!str_starts_with($mimeType, 'image/')) {
throw $this->createNotFoundException('File is not an image');
}
$response = new BinaryFileResponse($filePath);
$response->headers->set('Content-Type', $mimeType);
$response->setCache([
'max_age' => 3600,
's_maxage' => 3600,
'public' => true,
]);
return $response;
} catch (\Exception $e) {
// Log error
// $this->logger->error('Image serving failed: ' . $e->getMessage());
// Return a default image or error response
return new Response('Image not found', 404);
}
}
}