1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MediaBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MediaBundle\Entity\Media; |
6
|
|
|
use Alpixel\Bundle\MediaBundle\Exception\InvalidMimeTypeException; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\UploadException; |
12
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
|
17
|
|
|
class MediaController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @Route("/media/upload/wysiwyg", name="upload_wysiwyg") |
21
|
|
|
* |
22
|
|
|
* @Method({"POST"}) |
23
|
|
|
* |
24
|
|
|
* @param Request $request |
25
|
|
|
* |
26
|
|
|
* @return Response |
27
|
|
|
*/ |
28
|
|
|
public function uploadFilesWysiwygAction(Request $request) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$template = 'AlpixelMediaBundle:admin:blocks/upload_wysiwyg.html.twig'; |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$file = $request->files->get("upload"); |
34
|
|
|
if ($file !== null) { |
35
|
|
|
$media = $this->get('alpixel_media.manager')->upload($file, $request->get('folder'), null); |
36
|
|
|
|
37
|
|
|
return $this->render( |
38
|
|
|
$template, |
39
|
|
|
[ |
40
|
|
|
'file_uploaded' => $media, |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
} else { |
44
|
|
|
if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower( |
45
|
|
|
$_SERVER['REQUEST_METHOD'] |
46
|
|
|
) == 'post' |
47
|
|
|
) { |
48
|
|
|
throw new UploadException( |
49
|
|
|
sprintf("Votre fichier dépasse la limite maximale de %s", ini_get('post_max_size')) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} catch (\Exception $e) { |
54
|
|
|
return $this->render( |
55
|
|
|
$template, |
56
|
|
|
[ |
57
|
|
|
'error' => $e->getMessage(), |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @Route("/media/upload", name="upload") |
65
|
|
|
* @Route("/media/upload/{uploadConfiguration}", name="upload_configuration") |
66
|
|
|
* |
67
|
|
|
* @Method({"POST"}) |
68
|
|
|
* |
69
|
|
|
* @param Request $request |
70
|
|
|
* |
71
|
|
|
* @return JsonResponse |
72
|
|
|
*/ |
73
|
|
|
public function uploadAction(Request $request, $uploadConfiguration = null) |
74
|
|
|
{ |
75
|
|
|
$returnData = []; |
76
|
|
|
|
77
|
|
|
if ($request->get('lifetime') !== null) { |
78
|
|
|
$lifetime = new \DateTime($request->get('lifetime')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (empty($lifetime) || $lifetime == new \DateTime('now')) { |
82
|
|
|
$lifetime = new \DateTime('+6 hours'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($uploadConfiguration !== null && |
86
|
|
|
!array_key_exists($uploadConfiguration, $this->getParameter("alpixel_media.upload_configurations")) |
87
|
|
|
) { |
88
|
|
|
throw new \InvalidArgumentException(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$mediaPreview = $this->get('twig.extension.media_preview_extension'); |
92
|
|
|
foreach ($request->files as $files) { |
93
|
|
|
if (!is_array($files)) { |
94
|
|
|
$files = [$files]; |
95
|
|
|
} |
96
|
|
|
foreach ($files as $file) { |
97
|
|
|
/** @var UploadedFile $file */ |
98
|
|
|
try { |
99
|
|
|
$media = $this->get('alpixel_media.manager')->upload( |
100
|
|
|
$file, |
101
|
|
|
$request->get('folder'), |
102
|
|
|
$lifetime, |
103
|
|
|
$uploadConfiguration |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$path = $mediaPreview->generatePathFromSecretKey($media->getSecretKey()); |
107
|
|
|
$returnData[] = [ |
108
|
|
|
'id' => $media->getSecretKey(), |
109
|
|
|
'path' => $path, |
110
|
|
|
'name' => $media->getName(), |
111
|
|
|
'error' => false, |
112
|
|
|
]; |
113
|
|
|
} catch (InvalidMimeTypeException $e) { |
114
|
|
|
$returnData[] = [ |
115
|
|
|
'name' => $file->getClientOriginalName(), |
116
|
|
|
'error' => true, |
117
|
|
|
'errorMessage' => $this->get('translator')->trans( |
118
|
|
|
"Le format de fichier %format% n'est pas autorisé", |
119
|
|
|
[ |
120
|
|
|
'%format%' => $file->getClientMimeType(), |
121
|
|
|
] |
122
|
|
|
), |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new JsonResponse($returnData); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @Route("/media/download/{id}-{name}", name="media_download_public") |
133
|
|
|
* @Route("/media/download/{filter}/{id}-{name}", name="media_download_public_filters") |
134
|
|
|
* @Route("/media/download/{secretKey}/{filter}", name="media_download_private") |
135
|
|
|
* |
136
|
|
|
* @Method({"GET"}) |
137
|
|
|
* @param Media $media |
138
|
|
|
* @param string $filter |
139
|
|
|
* @return Response |
140
|
|
|
*/ |
141
|
|
|
public function downloadMediaAction(Media $media, $filter = null) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$response = new Response(); |
144
|
|
|
$response->setContent(file_get_contents($this->get('alpixel_media.manager')->getAbsolutePath($media))); |
145
|
|
|
$response->headers->set('Content-Type', 'application/force-download'); |
146
|
|
|
$response->headers->set('Content-disposition', 'filename='.$media->getName()); |
147
|
|
|
|
148
|
|
|
return $response; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @Route("/media/{id}-{name}", name="media_show_public") |
153
|
|
|
* @Route("/media/{filter}/{id}-{name}", name="media_show_public_filters") |
154
|
|
|
* @Route("/media/{secretKey}/{filter}", name="media_show_private") |
155
|
|
|
* |
156
|
|
|
* @Method({"GET"}) |
157
|
|
|
*/ |
158
|
|
|
public function showMediaAction(Request $request, Media $media, $filter = null) |
159
|
|
|
{ |
160
|
|
|
$response = new Response(); |
161
|
|
|
$lastModified = new \DateTime('now'); |
162
|
|
|
|
163
|
|
|
//Checking if it is an image or not |
164
|
|
|
$src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
165
|
|
|
$isImage = @getimagesize($src); |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
if ($isImage) { |
169
|
|
|
$response->headers->set('Content-disposition', 'inline;filename='.$media->getName()); |
170
|
|
|
if (!empty($filter) && $isImage) { |
171
|
|
|
$src = $this->get('alpixel_media.manager')->getAbsolutePath($media, $filter); |
172
|
|
|
$dataManager = $this->get('liip_imagine.data.manager'); // the data manager service |
173
|
|
|
$filterManager = $this->get('liip_imagine.filter.manager'); // the filter manager service |
174
|
|
|
$uploadDir = $this->get('alpixel_media.manager')->getUploadDir($filter); |
175
|
|
|
|
176
|
|
|
if (!is_file($src)) { |
177
|
|
|
$fs = new Filesystem(); |
178
|
|
|
if (!$fs->exists($uploadDir.$media->getFolder())) { |
179
|
|
|
$fs->mkdir($uploadDir.$media->getFolder()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$path = 'upload/'.$media->getUri(); |
183
|
|
|
|
184
|
|
|
// find the image and determine its type |
185
|
|
|
$image = $dataManager->find($filter, $path); |
186
|
|
|
|
187
|
|
|
// run the filter |
188
|
|
|
$responseData = $filterManager->applyFilter($image, $filter); |
189
|
|
|
$data = $responseData->getContent(); |
190
|
|
|
file_put_contents($uploadDir.$media->getUri(), $data); |
191
|
|
|
} else { |
192
|
|
|
$data = file_get_contents($src); |
193
|
|
|
$lastModified->setTimestamp(filemtime($src)); |
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
$src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
197
|
|
|
$lastModified->setTimestamp(filemtime($src)); |
198
|
|
|
$data = file_get_contents($src); |
199
|
|
|
} |
200
|
|
|
} else { |
201
|
|
|
$lastModified->setTimestamp(filemtime($src)); |
202
|
|
|
$data = file_get_contents($src); |
203
|
|
|
$response->headers->set('Content-disposition', 'attachment;filename='.basename($media->getUri())); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($data === false) { |
207
|
|
|
throw $this->createNotFoundException(); |
208
|
|
|
} |
209
|
|
|
} catch (\Exception $e) { |
210
|
|
|
throw $this->createNotFoundException($e->getMessage()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$response->setLastModified($lastModified); |
214
|
|
|
$response->setPublic(); |
215
|
|
|
$response->headers->set('Content-Type', $media->getMime()); |
216
|
|
|
|
217
|
|
|
if ($response->isNotModified($request)) { |
218
|
|
|
return $response; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$response->setContent($data); |
222
|
|
|
|
223
|
|
|
return $response; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: