src/App/EventSubscriber/PartialUploadSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Annotation\PartialUpload;
  4. use App\Model\Upload;
  5. use App\Traits\Autowired\Manager\UploadManagerTrait;
  6. use App\Traits\Autowired\Vendor\RequestStackTrait;
  7. use App\Traits\Autowired\Vendor\SerializerSubscriberTrait;
  8. use GollumSF\RestBundle\Annotation\Serialize;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class PartialUploadSubscriber implements EventSubscriberInterface
  15. {
  16.     use UploadManagerTrait;
  17.     use SerializerSubscriberTrait;
  18.     use RequestStackTrait;
  19.     
  20.     public static function getSubscribedEvents() {
  21.         return [
  22.             KernelEvents::CONTROLLER_ARGUMENTS => [
  23.                 ['onKernelControllerArguments', -20],
  24.             ]
  25.         ];
  26.     }
  27.     
  28.     public function onKernelControllerArguments(ControllerArgumentsEvent $event)
  29.     {
  30.         $request $event->getRequest();
  31.         /** @var PartialUpload $annotation */
  32.         $annotation $request->attributes->get('_' PartialUpload::ALIAS_NAME);
  33.         if ($annotation) {
  34.             
  35.             /** @var Upload $upload */
  36.             $upload $request->attributes->get($annotation->getName());
  37.             if (!$upload) {
  38.                 throw new BadRequestHttpException(sprintf('%s parameter not found'$annotation->getName()));
  39.             }
  40.             $maxSize null;
  41.             if ($annotation->getMaxSize()) {
  42.                 $maxSize is_string($annotation->getMaxSize()) ? $this->human2byte($annotation->getMaxSize()) : $annotation->getMaxSize();
  43.             }
  44.             
  45.             if (!$upload->isEndOfFile()) {
  46.                 $upload $this->uploadManager->partialUpload($upload$maxSize);
  47.                 $request->attributes->set($annotation->getName(), $upload);
  48.                 $event->setController([ $this'partialControllerAction' ]);
  49.             } else {
  50.                 $upload $this->uploadManager->finalUpload($upload$maxSize$annotation->getMimeTypes());
  51.                 $request->attributes->set($annotation->getName(), $upload);
  52.             }
  53.         }
  54.     }
  55.     
  56.     public function partialControllerAction(): Upload {
  57.         $request $this->requestStack->getCurrentRequest();
  58.         /** @var PartialUpload $annotation */
  59.         /** @var Serialize $serialize */
  60.         $annotation $request->attributes->get('_' PartialUpload::ALIAS_NAME);
  61.         $serialize $request->attributes->get('_' Serialize::ALIAS_NAME);
  62.         $serialize->setCode(Response::HTTP_OK);
  63.         return $request->attributes->get($annotation->getName());
  64.     }
  65.     protected function human2byte($value) {
  66.         return preg_replace_callback('/^\s*(\d+)\s*(?:([kmgt]?)b?)?\s*$/i', function ($m) {
  67.             switch (strtolower($m[2])) {
  68.                 case 't'$m[1] *= 1024;
  69.                 case 'g'$m[1] *= 1024;
  70.                 case 'm'$m[1] *= 1024;
  71.                 case 'k'$m[1] *= 1024;
  72.             }
  73.             return $m[1];
  74.         }, $value);
  75.     }
  76. }