src/Service/Listeners/RequestListener.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Service\Listeners;
  3. use http\Message;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Serializer\SerializerInterface;
  8. class RequestListener
  9. {
  10.     private $maxSize;
  11.     private $serializer;
  12.     public function __construct(int $maxSizeSerializerInterface $serializer)
  13.     {
  14.         $this->maxSize $maxSize;
  15.         $this->serializer $serializer;
  16.     }
  17.     private function formatBytes($bytes, ?int $decimals 2): string {
  18.         // Define units in order of size
  19.         $units = ['B''K''M''G'];
  20.         // Check if the size is 0
  21.         if ($bytes == 0) {
  22.             return '0 B';
  23.         }
  24.         // Calculate the appropriate unit
  25.         $factor floor((strlen($bytes) - 1) / 3);
  26.         $formattedSize $bytes pow(1024$factor);
  27.         // Return formatted size with the appropriate unit
  28.         return round($formattedSize$decimals) . '' $units[$factor];
  29.     }
  30.     private function convertToBytes($value) {
  31.         // Trim whitespace from the value
  32.         $value trim($value);
  33.         // Get the last character, which represents the unit (e.g., K, M, G)
  34.         $unit strtoupper(substr($value, -1));
  35.         // Convert the numeric part to an integer
  36.         $value = (int) $value;
  37.         // Determine the multiplier based on the unit and convert to bytes
  38.         switch ($unit) {
  39.             case 'G':
  40.                 $value *= 1024 1024 1024// Convert gigabytes to bytes
  41.                 break;
  42.             case 'M':
  43.                 $value *= 1024 1024// Convert megabytes to bytes
  44.                 break;
  45.             case 'K':
  46.                 $value *= 1024// Convert kilobytes to bytes
  47.                 break;
  48.         }
  49.         // Return the value in bytes
  50.         return $value;
  51.     }
  52.     public function onKernelRequest(RequestEvent $event)
  53.     {
  54.         if (!$event->isMasterRequest() || $event->getRequest()->getMethod() !== 'POST') {
  55.             return;
  56.         }
  57.         $contentLength $_SERVER['CONTENT_LENGTH'] ?? 0;
  58.         if ($contentLength $this->maxSize) {
  59.             $maxFileSize ini_get('upload_max_filesize');
  60.             $sendFile $this->formatBytes($contentLength);
  61.             // JSON formatında hata mesajı hazırlıyoruz
  62.             $data = [
  63.                 // "message" => "The file size limit has been exceeded. Please upload a smaller file. allowed $maxFileSize, tryed $sendFile ",
  64.                 "message" => "The file size limit has been exceeded. Please upload a smaller file. Allowed: $maxFileSize, but attempted: $sendFile",
  65.                 "severity" => "warning"
  66.             ];
  67.             $json $this->serializer->serialize($data'json', [
  68.                 'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS
  69.             ]);
  70.             // 413 (Request Entity Too Large) hata kodu ile JSON yanıtı döndürüyoruz
  71.             $response = new JsonResponse$jsonResponse::HTTP_REQUEST_ENTITY_TOO_LARGE, [], true);
  72.             $event->setResponse($response);
  73.         }
  74.     }
  75. }