src/Controller/ImageController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use App\Entity\Payment;
  6. use App\Service\PaymentService;
  7. use App\Form\PaymentType;
  8. use App\Library\Utils\Other\Other;
  9. use App\Service\Image\ImageService;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  12. class ImageController extends BaseAbstractController
  13. {
  14.     public function list(Request $requestPaymentService $paymentService): Response
  15.     {
  16.         $payments $paymentService->getBaseService()->getAll();
  17.         // return $this->redirectToRoute("login");
  18.         return $this->render('admin/payment/payments.html.twig', [
  19.             'payments' => $payments,
  20.         ]);
  21.     }
  22.     public function edit(Request $requestPaymentService $paymentServiceEntityManagerInterface $em): Response
  23.     {
  24.         $id $request->get('id');
  25.         /**
  26.          * @var Payment|null $payment
  27.          */
  28.         $payment $id $paymentService->getBaseService()->get($id) : null;
  29.         if (!$payment) {
  30.             $payment = (new Payment());
  31.         }
  32.         $form $this->createForm(PaymentType::class, $payment);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             $payment->setCreatedBy($this->getUser());
  36.             $em->persist($payment);
  37.             $em->flush();
  38.             return $this->redirectToRoute('moderator_payments');
  39.         }
  40.         // return $this->redirectToRoute("login");
  41.         return $this->render('admin/payment/payment_edit.html.twig', [
  42.             'form' => $form->createView(),
  43.             'payment' => $payment,
  44.         ]);
  45.     }
  46.     public function view(Request $requestImageService $imageServiceEntityManagerInterface $em): Response
  47.     {
  48.         $id $request->get('id');
  49.         /**
  50.          * @var Image|null $image
  51.          */
  52.         $image $id $imageService->getBaseService()->get($id) : null;
  53.          try {
  54.             $projectDir $this->getParameter('kernel.project_dir');
  55.             $filePath $imageService->getRealPath($image);
  56.             // Security: prevent directory traversal
  57.             // if (str_contains($filename, '..') || str_contains($filename, '/')) {
  58.             //     throw $this->createNotFoundException('Invalid filename');
  59.             // }
  60.             // Check if file exists and is readable
  61.             if (!file_exists($filePath) || !is_readable($filePath)) {
  62.                 throw $this->createNotFoundException('Image not found');
  63.             }
  64.             // Check if it's actually an image
  65.             $mimeType mime_content_type($filePath);
  66.             if (!str_starts_with($mimeType'image/')) {
  67.                 throw $this->createNotFoundException('File is not an image');
  68.             }
  69.             $response = new BinaryFileResponse($filePath);
  70.             $response->headers->set('Content-Type'$mimeType);
  71.             $response->setCache([
  72.                 'max_age' => 3600,
  73.                 's_maxage' => 3600,
  74.                 'public' => true,
  75.             ]);
  76.             return $response;
  77.         } catch (\Exception $e) {
  78.             // Log error
  79.             // $this->logger->error('Image serving failed: ' . $e->getMessage());
  80.             // Return a default image or error response
  81.             return new Response('Image not found'404);
  82.         }
  83.     }
  84. }