1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Provider; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Model\AbstractMedia; |
6
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
use Symfony\Component\Validator\Constraints as Constraint; |
9
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
10
|
|
|
|
11
|
|
|
class FileProvider extends AbstractProvider implements ProviderInterface, DownloadableProviderInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $fileConstraintOptions = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $fileConstraintOptions |
20
|
|
|
*/ |
21
|
15 |
|
public function __construct(array $fileConstraintOptions = []) |
22
|
|
|
{ |
23
|
15 |
|
$this->fileConstraintOptions = $fileConstraintOptions; |
24
|
15 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param FormMapper $formMapper |
28
|
|
|
*/ |
29
|
4 |
|
public function buildProviderCreateForm(FormMapper $formMapper) |
30
|
|
|
{ |
31
|
4 |
|
$this->addRequiredFileField($formMapper, 'binaryContent', 'file'); |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param FormMapper $formMapper |
36
|
|
|
*/ |
37
|
2 |
|
public function buildProviderEditFormBefore(FormMapper $formMapper) |
38
|
|
|
{ |
39
|
2 |
|
$this->addFileField($formMapper, 'binaryContent', 'file'); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param AbstractMedia $media |
44
|
|
|
* @param bool $providerReferenceUpdated |
45
|
|
|
*/ |
46
|
2 |
|
public function update(AbstractMedia $media, $providerReferenceUpdated) |
47
|
|
|
{ |
48
|
2 |
|
if (!is_null($media->getBinaryContent())) { |
49
|
2 |
|
if (empty($media->getImage())) { |
50
|
2 |
|
$this->setFileImage($media); |
51
|
|
|
} |
52
|
2 |
|
$filename = $this->handleFileUpload($media); |
53
|
2 |
|
if (!empty($filename)) { |
54
|
2 |
|
$media->setProviderReference($filename); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
parent::update($media, $providerReferenceUpdated); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
12 |
|
public function getIcon(): string |
65
|
|
|
{ |
66
|
12 |
|
return 'fa fa-file'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
13 |
|
public function getName(): string |
73
|
|
|
{ |
74
|
13 |
|
return 'file'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
4 |
|
public function getType(): string |
81
|
|
|
{ |
82
|
4 |
|
return AbstractProvider::TYPE_FILE; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param AbstractMedia $media |
87
|
|
|
*/ |
88
|
2 |
|
protected function setFileImage(AbstractMedia $media) |
89
|
|
|
{ |
90
|
|
|
/** |
91
|
|
|
* @var UploadedFile $file |
92
|
|
|
*/ |
93
|
2 |
|
$file = $media->getBinaryContent(); |
94
|
2 |
|
if (empty($file)) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$imageFilename = $this->getImageByExtension($file->getClientOriginalExtension()); |
99
|
2 |
|
$media->setImageContent( |
100
|
2 |
|
new UploadedFile( |
101
|
2 |
|
$this->getImageLocation($imageFilename), |
102
|
2 |
|
$imageFilename |
103
|
|
|
) |
104
|
|
|
); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param FormMapper $formMapper |
109
|
|
|
* @param string $name |
110
|
|
|
* @param string $label |
111
|
|
|
* @param array $options |
112
|
|
|
*/ |
113
|
2 |
|
public function addFileField(FormMapper $formMapper, $name, $label, $options = []) |
114
|
|
|
{ |
115
|
2 |
|
$this->doAddFileField($formMapper, $name, $label, false, $this->getFileFieldConstraints($options)); |
116
|
2 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param FormMapper $formMapper |
120
|
|
|
* @param string $name |
121
|
|
|
* @param string $label |
122
|
|
|
* @param array $options |
123
|
|
|
*/ |
124
|
4 |
|
public function addRequiredFileField(FormMapper $formMapper, $name, $label, $options = []) |
125
|
|
|
{ |
126
|
4 |
|
$this->doAddFileField($formMapper, $name, $label, true, $this->getFileFieldConstraints($options)); |
127
|
4 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $options |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
4 |
|
private function getFileFieldConstraints(array $options): array |
134
|
|
|
{ |
135
|
|
|
return [ |
136
|
4 |
|
new Constraint\File($this->getFileConstraintOptions($options)), |
137
|
4 |
|
new Constraint\Callback([$this, 'validateExtension']), |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array $options |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
4 |
|
protected function getFileConstraintOptions(array $options = []): array |
146
|
|
|
{ |
147
|
4 |
|
$merged = array_merge($this->fileConstraintOptions, $options); |
148
|
4 |
|
unset($merged['extensions']); |
149
|
|
|
|
150
|
4 |
|
return $merged; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $object |
155
|
|
|
* @param ExecutionContextInterface $context |
156
|
|
|
*/ |
157
|
4 |
|
public function validateExtension($object, ExecutionContextInterface $context) |
158
|
|
|
{ |
159
|
4 |
|
if ($object instanceof UploadedFile && isset($this->fileConstraintOptions['extensions'])) { |
160
|
3 |
|
if (!in_array($object->getClientOriginalExtension(), $this->fileConstraintOptions['extensions'])) { |
161
|
1 |
|
$context->addViolation( |
162
|
1 |
|
sprintf( |
163
|
1 |
|
'It\'s not allowed to upload a file with extension "%s"', |
164
|
1 |
|
$object->getClientOriginalExtension() |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
} |
169
|
4 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $extension |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
3 |
|
protected function getImageByExtension($extension): string |
176
|
|
|
{ |
177
|
3 |
|
if (in_array($extension, $this->getArchiveExtensions())) { |
178
|
1 |
|
return 'archive.png'; |
179
|
|
|
} |
180
|
3 |
|
if (in_array($extension, $this->getAudioExtensions())) { |
181
|
1 |
|
return 'audio.png'; |
182
|
|
|
} |
183
|
3 |
|
if (in_array($extension, $this->getCodeExtensions())) { |
184
|
1 |
|
return 'code.png'; |
185
|
|
|
} |
186
|
3 |
|
if (in_array($extension, $this->getSpreadsheetExtensions())) { |
187
|
1 |
|
return 'excel.png'; |
188
|
|
|
} |
189
|
3 |
|
if (in_array($extension, $this->getImageExtensions())) { |
190
|
1 |
|
return 'image.png'; |
191
|
|
|
} |
192
|
3 |
|
if (in_array($extension, $this->getMovieExtensions())) { |
193
|
1 |
|
return 'movie.png'; |
194
|
|
|
} |
195
|
3 |
|
if (in_array($extension, $this->getPdfExtensions())) { |
196
|
1 |
|
return 'pdf.png'; |
197
|
|
|
} |
198
|
3 |
|
if (in_array($extension, $this->getPresentationExtensions())) { |
199
|
1 |
|
return 'powerpoint.png'; |
200
|
|
|
} |
201
|
3 |
|
if (in_array($extension, $this->getTextExtensions())) { |
202
|
3 |
|
return 'text.png'; |
203
|
|
|
} |
204
|
1 |
|
if (in_array($extension, $this->getWordExtensions())) { |
205
|
1 |
|
return 'word.png'; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
return 'default.png'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string[] |
213
|
|
|
*/ |
214
|
3 |
|
protected function getArchiveExtensions(): array |
215
|
|
|
{ |
216
|
3 |
|
return ['zip', 'rar', 'tar', 'gz']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return string[] |
221
|
|
|
*/ |
222
|
3 |
|
protected function getAudioExtensions(): array |
223
|
|
|
{ |
224
|
3 |
|
return ['wav', 'mp3', 'flac', 'aac', 'aiff', 'm4a', 'ogg', 'oga', 'wma']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string[] |
229
|
|
|
*/ |
230
|
3 |
|
protected function getCodeExtensions(): array |
231
|
|
|
{ |
232
|
3 |
|
return ['php', 'html', 'css', 'js', 'vb', 'phar', 'py', 'jar', 'json', 'yml']; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return string[] |
237
|
|
|
*/ |
238
|
3 |
|
protected function getSpreadsheetExtensions(): array |
239
|
|
|
{ |
240
|
3 |
|
return ['xls', 'xlt', 'xlm', 'xlsx', 'xlsm', 'xltx', 'xltm']; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return string[] |
245
|
|
|
*/ |
246
|
3 |
|
protected function getImageExtensions(): array |
247
|
|
|
{ |
248
|
3 |
|
return ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'ai', 'psd']; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return string[] |
253
|
|
|
*/ |
254
|
3 |
|
protected function getMovieExtensions(): array |
255
|
|
|
{ |
256
|
3 |
|
return ['mp4', 'avi', 'mkv', 'mpg', 'mpeg']; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return string[] |
261
|
|
|
*/ |
262
|
3 |
|
protected function getPdfExtensions(): array |
263
|
|
|
{ |
264
|
3 |
|
return ['pdf']; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return string[] |
269
|
|
|
*/ |
270
|
3 |
|
protected function getPresentationExtensions(): array |
271
|
|
|
{ |
272
|
3 |
|
return ['ppt', 'pot', 'pos', 'pps', 'pptx', 'pptm', 'potx', 'potm', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm']; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return string[] |
277
|
|
|
*/ |
278
|
3 |
|
protected function getTextExtensions(): array |
279
|
|
|
{ |
280
|
3 |
|
return ['txt', 'csv']; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return string[] |
285
|
|
|
*/ |
286
|
1 |
|
protected function getWordExtensions(): array |
287
|
|
|
{ |
288
|
1 |
|
return ['doc', 'dot', 'wbk', 'docx', 'docm', 'dotx', 'dotm', 'docb']; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param $imageFilename |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
2 |
|
protected function getImageLocation($imageFilename): string |
296
|
|
|
{ |
297
|
2 |
|
$file = $this->getFileLocator()->locate('@MediaMonksSonataMediaBundle/Resources/image/file/'.$imageFilename); |
298
|
2 |
|
if (is_array($file)) { |
299
|
|
|
$file = current($file); |
300
|
|
|
} |
301
|
|
|
|
302
|
2 |
|
return $file; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|