1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
15
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
17
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
20
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
21
|
|
|
|
22
|
|
|
class MediaController extends Controller |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param MediaInterface $media |
26
|
|
|
* |
27
|
|
|
* @return MediaProviderInterface |
28
|
|
|
*/ |
29
|
|
|
public function getProvider(MediaInterface $media) |
30
|
|
|
{ |
31
|
|
|
return $this->get('sonata.media.pool')->getProvider($media->getProviderName()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $id |
36
|
|
|
* |
37
|
|
|
* @return MediaInterface |
38
|
|
|
*/ |
39
|
|
|
public function getMedia($id) |
40
|
|
|
{ |
41
|
|
|
return $this->get('sonata.media.manager.media')->find($id); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws NotFoundHttpException |
46
|
|
|
* |
47
|
|
|
* @param string $id |
48
|
|
|
* @param string $format |
49
|
|
|
* |
50
|
|
|
* @return Response |
51
|
|
|
*/ |
52
|
|
|
public function downloadAction($id, $format = 'reference') |
53
|
|
|
{ |
54
|
|
|
$media = $this->getMedia($id); |
55
|
|
|
|
56
|
|
|
if (!$media) { |
57
|
|
|
throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$this->get('sonata.media.pool')->getDownloadSecurity($media)->isGranted($media, $this->getCurrentRequest())) { |
61
|
|
|
throw new AccessDeniedException(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$response = $this->getProvider($media)->getDownloadResponse($media, $format, $this->get('sonata.media.pool')->getDownloadMode($media)); |
65
|
|
|
|
66
|
|
|
if ($response instanceof BinaryFileResponse) { |
67
|
|
|
$response->prepare($this->getCurrentRequest()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $response; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws NotFoundHttpException |
75
|
|
|
* |
76
|
|
|
* @param string $id |
77
|
|
|
* @param string $format |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function viewAction($id, $format = 'reference') |
82
|
|
|
{ |
83
|
|
|
$media = $this->getMedia($id); |
84
|
|
|
|
85
|
|
|
if (!$media) { |
86
|
|
|
throw new NotFoundHttpException(sprintf('unable to find the media with the id : %s', $id)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!$this->get('sonata.media.pool')->getDownloadSecurity($media)->isGranted($media, $this->getCurrentRequest())) { |
90
|
|
|
throw new AccessDeniedException(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->render('SonataMediaBundle:Media:view.html.twig', array( |
94
|
|
|
'media' => $media, |
95
|
|
|
'formats' => $this->get('sonata.media.pool')->getFormatNamesByContext($media->getContext()), |
96
|
|
|
'format' => $format, |
97
|
|
|
)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* This action applies a given filter to a given image, |
102
|
|
|
* optionally saves the image and |
103
|
|
|
* outputs it to the browser at the same time. |
104
|
|
|
* |
105
|
|
|
* @param string $path |
106
|
|
|
* @param string $filter |
107
|
|
|
* |
108
|
|
|
* @return Response |
109
|
|
|
*/ |
110
|
|
|
public function liipImagineFilterAction($path, $filter) |
111
|
|
|
{ |
112
|
|
|
if (!preg_match('@([^/]*)/(.*)/([0-9]*)_([a-z_A-Z]*).jpg@', $path, $matches)) { |
113
|
|
|
throw new NotFoundHttpException(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$targetPath = $this->get('liip_imagine.cache.manager')->resolve($this->getCurrentRequest(), $path, $filter); |
117
|
|
|
|
118
|
|
|
if ($targetPath instanceof Response) { |
119
|
|
|
return $targetPath; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// get the file |
123
|
|
|
$media = $this->getMedia($matches[3]); |
124
|
|
|
if (!$media) { |
125
|
|
|
throw new NotFoundHttpException(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$provider = $this->getProvider($media); |
129
|
|
|
$file = $provider->getReferenceFile($media); |
130
|
|
|
|
131
|
|
|
// load the file content from the abstracted file system |
132
|
|
|
$tmpFile = sprintf('%s.%s', tempnam(sys_get_temp_dir(), 'sonata_media_liip_imagine'), $media->getExtension()); |
133
|
|
|
file_put_contents($tmpFile, $file->getContent()); |
134
|
|
|
|
135
|
|
|
$image = $this->get('liip_imagine')->open($tmpFile); |
136
|
|
|
|
137
|
|
|
$response = $this->get('liip_imagine.filter.manager')->get($this->getCurrentRequest(), $filter, $image, $path); |
138
|
|
|
|
139
|
|
|
if ($targetPath) { |
140
|
|
|
$response = $this->get('liip_imagine.cache.manager')->store($response, $targetPath, $filter); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $response; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* NEXT_MAJOR: Remove this method when bumping Symfony requirement to 2.8+. |
148
|
|
|
* Inject the Symfony\Component\HttpFoundation\Request into the actions instead. |
149
|
|
|
* |
150
|
|
|
* @return Request |
151
|
|
|
*/ |
152
|
|
|
private function getCurrentRequest() |
153
|
|
|
{ |
154
|
|
|
if ($this->has('request_stack')) { |
155
|
|
|
return $this->get('request_stack')->getCurrentRequest(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->get('request'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|