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