Skip to content

Commit

Permalink
EZP-28871: upload_max_filesize setting should be taken into account i…
Browse files Browse the repository at this point in the history
…n the validation of uploaded files (ezsystems#218)

* EZP-28871: upload_max_filesize setting should be taken into account in the validation of uploaded files

* fixup! EZP-28871: upload_max_filesize setting should be taken into account in the validation of uploaded files
  • Loading branch information
adamwojs authored and Łukasz Serwatka committed Mar 12, 2018
1 parent d059304 commit d4091bf
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/Form/Type/FieldType/BinaryBaseFieldType.php
Expand Up @@ -9,7 +9,10 @@
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Parent Form Type for binary file based field types.
Expand All @@ -32,12 +35,50 @@ public function buildForm(FormBuilderInterface $builder, array $options)
[
'label' => /** @Desc("File") */ 'content.field_type.binary_base.file',
'required' => $options['required'],
'constraints' => [
new Assert\File([
'maxSize' => $this->getMaxUploadSize(),
]),
],
]
);
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['max_upload_size'] = $this->getMaxUploadSize();
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['translation_domain' => 'ezrepoforms_fieldtype']);
}

private function getMaxUploadSize()
{
static $value = null;
if ($value === null) {
return $this->str2bytes(ini_get('upload_max_filesize'));
}

return $value;
}

private function str2bytes($str)
{
$str = strtoupper(trim($str));

$value = substr($str, 0, -1);
$unit = substr($str, -1);
switch ($unit) {
case 'G':
$value *= 1024;
case 'M':
$value *= 1024;
case 'K':
$value *= 1024;
}

return (int) $value;
}
}

0 comments on commit d4091bf

Please sign in to comment.