/
FixFileUploadListener.php
87 lines (72 loc) · 2.56 KB
/
FixFileUploadListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Core\EventListener;
use Symfony\Component\Form\Events;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\TemporaryStorage;
/**
* Moves uploaded files to a temporary location
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class FixFileUploadListener implements EventSubscriberInterface
{
private $storage;
public function __construct(TemporaryStorage $storage)
{
$this->storage = $storage;
}
public static function getSubscribedEvents()
{
return Events::onBindClientData;
}
public function onBindClientData(FilterDataEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if (null === $data) {
$data = array();
}
if (!is_array($data)) {
throw new UnexpectedTypeException($data, 'array');
}
$data = array_replace(array(
'file' => '',
'token' => '',
'name' => '',
'originalName' => '',
), $data);
// Newly uploaded file
if ($data['file'] instanceof UploadedFile && $data['file']->isValid()) {
$data['token'] = (string)rand(100000, 999999);
$directory = $this->storage->getTempDir($data['token']);
$data['file']->move($directory);
$data['name'] = $data['file']->getName();
$data['originalName'] = $data['file']->getOriginalName();
}
// Existing uploaded file
if (!$data['file'] && $data['token'] && $data['name']) {
$path = $this->storage->getTempDir($data['token']) . DIRECTORY_SEPARATOR . $data['name'];
if (file_exists($path)) {
$data['file'] = new UploadedFile($path, $data['originalName'], null, null, null, true);
}
}
// Clear other fields if we still don't have a file, but keep
// possible existing files of the field
if (!$data['file']) {
$data = $form->getNormData();
}
$event->setData($data);
}
}