|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\EventSubscriber; |
|
6
|
|
|
|
|
7
|
|
|
use ApiPlatform\Core\EventListener\EventPriorities; |
|
8
|
|
|
use ApiPlatform\Core\Util\RequestAttributesExtractor; |
|
9
|
|
|
use Chamilo\CoreBundle\Entity\AbstractResource; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\ResourceFile; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
15
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
16
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
|
17
|
|
|
|
|
18
|
|
|
class ResolveResourceFileContentUrlSubscriber implements EventSubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private $storage; |
|
21
|
|
|
private $generator; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(StorageInterface $storage, UrlGeneratorInterface $generator) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->storage = $storage; |
|
26
|
|
|
$this->generator = $generator; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function getSubscribedEvents(): array |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
KernelEvents::VIEW => ['onPreSerialize', EventPriorities::PRE_SERIALIZE], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function onPreSerialize(ViewEvent $event): void |
|
37
|
|
|
{ |
|
38
|
|
|
$controllerResult = $event->getControllerResult(); |
|
39
|
|
|
$request = $event->getRequest(); |
|
40
|
|
|
|
|
41
|
|
|
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
$attributes = RequestAttributesExtractor::extractAttributes($request); |
|
45
|
|
|
|
|
46
|
|
|
if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || |
|
47
|
|
|
//!\is_a($attributes['resource_class'], ResourceFile::class, true) |
|
48
|
|
|
!\is_a($attributes['resource_class'], AbstractResource::class, true) |
|
49
|
|
|
) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
$mediaObjects = $controllerResult; |
|
53
|
|
|
|
|
54
|
|
|
if (!is_iterable($mediaObjects)) { |
|
55
|
|
|
$mediaObjects = [$mediaObjects]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach ($mediaObjects as $mediaObject) { |
|
59
|
|
|
if (!$mediaObject instanceof AbstractResource) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$params = [ |
|
64
|
|
|
'id' => $mediaObject->getResourceNode()->getId(), |
|
65
|
|
|
'tool' => $mediaObject->getResourceNode()->getResourceType()->getTool()->getName(), |
|
66
|
|
|
'type' => $mediaObject->getResourceNode()->getResourceType()->getName(), |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
$mediaObject->contentUrl = $this->generator->generate('chamilo_core_resource_view_file', $params); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|