1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Evoluta\DropzoneBundle\Form\Type; |
4
|
|
|
|
5
|
|
|
use Aws\AwsClient; |
6
|
|
|
use Evoluta\DropzoneBundle\Manager\AwsUploadPolicy; |
7
|
|
|
use Evoluta\DropzoneBundle\Manager\S3BrowserUploadManagerInterface; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
use Symfony\Component\Form\AbstractType; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
12
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
13
|
|
|
use Symfony\Component\Form\FormInterface; |
14
|
|
|
use Symfony\Component\Form\FormView; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class DropzoneS3FileType extends AbstractType |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
private $configurations; |
23
|
|
|
|
24
|
|
View Code Duplication |
public function __construct($endpoint, $accessKey, $secret, $bucket) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->configurations = array( |
27
|
|
|
'endpoint' => $endpoint, |
28
|
|
|
'accessKey' => $accessKey, |
29
|
|
|
'acl' => 'public-read', |
30
|
|
|
'successStatus' => 201, |
31
|
|
|
'policy' => null, |
32
|
|
|
'signature' => null, |
33
|
|
|
'maxFilesize' => 10, |
34
|
|
|
'directory' => '', |
35
|
|
|
'bucket' => $bucket, |
36
|
|
|
'secret' => $secret, |
37
|
|
|
'acceptedFiles' => null, |
38
|
|
|
'expireAt' => date('Y-m-d\TG:i:s\Z', strtotime('+1 hours')) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getBlockPrefix() |
43
|
|
|
{ |
44
|
|
|
return "dropzoneS3File"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getParent() |
48
|
|
|
{ |
49
|
|
|
return HiddenType::class; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function configureOptions(OptionsResolver $optionsResolver) |
53
|
|
|
{ |
54
|
|
|
$optionsResolver->setDefaults( |
55
|
|
|
$this->configurations |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
60
|
|
|
{ |
61
|
|
|
$endpoint = $this->getCorrectOption('endpoint', $this->configurations, $options); |
62
|
|
|
$accessKey = $this->getCorrectOption('accessKey', $this->configurations, $options); |
63
|
|
|
$bucket = $this->getCorrectOption('bucket', $this->configurations, $options); |
64
|
|
|
$expireAt = $this->getCorrectOption('expireAt', $this->configurations, $options); |
65
|
|
|
$acl = $this->getCorrectOption('acl', $this->configurations, $options); |
66
|
|
|
$successStatus = $this->getCorrectOption('successStatus', $this->configurations, $options); |
67
|
|
|
$secret = $this->getCorrectOption('secret', $this->configurations, $options); |
68
|
|
|
$acceptedFiles = $this->getCorrectOption('acceptedFiles', $this->configurations, $options); |
69
|
|
|
$maxFilesize = $this->getCorrectOption('maxFilesize', $this->configurations, $options); |
70
|
|
|
$directory = $this->getCorrectOption('directory', $this->configurations, $options); |
71
|
|
|
|
72
|
|
|
$policyObject = new AwsUploadPolicy($bucket, $secret, $acl, $expireAt, $successStatus); |
73
|
|
|
|
74
|
|
|
$view->vars = array_merge( |
75
|
|
|
$view->vars, |
76
|
|
|
array( |
77
|
|
|
"endpoint" => $endpoint, |
78
|
|
|
"accessKey" => $accessKey, |
79
|
|
|
"bucket" => $bucket, |
80
|
|
|
"acl" => $acl, |
81
|
|
|
"successStatus" => $successStatus, |
82
|
|
|
"policy" => $policyObject->getBase64Policy(), |
83
|
|
|
"signature" => $policyObject->getSignature(), |
84
|
|
|
"key" => uniqid().$view->vars['name'], |
85
|
|
|
"acceptedFiles" => $acceptedFiles, |
86
|
|
|
"maxFilesize" => $maxFilesize, |
87
|
|
|
"directory" => $directory |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getCorrectOption($key, $defaultOptions, $forcedOptions) |
94
|
|
|
{ |
95
|
|
|
if (array_key_exists($key, $forcedOptions)) { |
96
|
|
|
return $forcedOptions[$key]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (array_key_exists($key, $defaultOptions)) { |
100
|
|
|
return $defaultOptions[$key]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw new \Exception('No required '.$key.' option is defined!'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.