vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishIOBundle/EventListener/StreamFileListener.php line 46

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishIOBundle\EventListener;
  7. use eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse;
  8. use eZ\Publish\Core\IO\IOServiceInterface;
  9. use eZ\Publish\Core\IO\Values\MissingBinaryFile;
  10. use eZ\Publish\Core\MVC\ConfigResolverInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. /**
  17.  * Listens for IO files requests, and streams them.
  18.  */
  19. class StreamFileListener implements EventSubscriberInterface
  20. {
  21.     /** @var IOServiceInterface */
  22.     private $ioService;
  23.     /** @var ConfigResolverInterface */
  24.     private $configResolver;
  25.     public function __construct(IOServiceInterface $ioServiceConfigResolverInterface $configResolver)
  26.     {
  27.         $this->ioService $ioService;
  28.         $this->configResolver $configResolver;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             KernelEvents::REQUEST => ['onKernelRequest'42],
  34.         ];
  35.     }
  36.     /**
  37.      * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
  38.      */
  39.     public function onKernelRequest(GetResponseEvent $event)
  40.     {
  41.         if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
  42.             return;
  43.         }
  44.         $request $event->getRequest();
  45.         $urlPrefix $this->configResolver->getParameter('io.url_prefix');
  46.         if (strpos($urlPrefix'://') !== false) {
  47.             $uri $request->getSchemeAndHttpHost() . $request->getPathInfo();
  48.         } else {
  49.             $uri $request->attributes->get('semanticPathinfo');
  50.         }
  51.         if (!$this->isIoUri($uri$urlPrefix)) {
  52.             return;
  53.         }
  54.         $binaryFile $this->ioService->loadBinaryFileByUri($uri);
  55.         if ($binaryFile instanceof MissingBinaryFile) {
  56.             throw new NotFoundHttpException("Could not find 'BinaryFile' with identifier '$uri'");
  57.         }
  58.         $event->setResponse(
  59.             new BinaryStreamResponse(
  60.                 $binaryFile,
  61.                 $this->ioService
  62.             )
  63.         );
  64.     }
  65.     /**
  66.      * Tests if $uri is an IO file uri root.
  67.      *
  68.      * @param string $uri
  69.      *
  70.      * @return bool
  71.      */
  72.     private function isIoUri($uri$urlPrefix)
  73.     {
  74.         return strpos(ltrim($uri'/'), $this->configResolver->getParameter('io.url_prefix')) === 0;
  75.     }
  76. }