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