From fbf95c50fee812168bcd8523d8117e2cec6e0af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Fri, 27 Nov 2015 10:23:02 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20validation=20du=20type=20et?= =?UTF-8?q?=20de=20la=20taille=20c=C3=B4t=C3=A9=20serveur.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Form/Type/FileUploadType.php | 16 ++++++++-- Validation/FileMimeType.php | 44 ++++++++++++++++++++++++++++ Validation/FileMimeTypeValidator.php | 37 +++++++++++++++++++++++ Validation/FileSize.php | 37 +++++++++++++++++++++++ Validation/FileSizeValidator.php | 42 ++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 Validation/FileMimeType.php create mode 100644 Validation/FileMimeTypeValidator.php create mode 100644 Validation/FileSize.php create mode 100644 Validation/FileSizeValidator.php diff --git a/Form/Type/FileUploadType.php b/Form/Type/FileUploadType.php index 10f18c32..1c8c0b41 100644 --- a/Form/Type/FileUploadType.php +++ b/Form/Type/FileUploadType.php @@ -12,6 +12,8 @@ namespace Irstea\FileUploadBundle\Form\Type; use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer; use Irstea\FileUploadBundle\Model\FileManagerInterface; +use Irstea\FileUploadBundle\Validation\FileMimeType; +use Irstea\FileUploadBundle\Validation\FileSize; use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; @@ -22,6 +24,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Routing\Router; use Symfony\Component\Validator\Constraints\Count; +use Symfony\Component\Validator\Constraints\NotBlank; /** Type de champ 'file_upload'. * @@ -150,12 +153,19 @@ class FileUploadType extends AbstractType } } elseif ($options['required']) { - $required = new \Symfony\Component\Validator\Constraints\NotBlank(['message' => 'file_upload.required']); + $required = new NotBlank(['message' => 'file_upload.required']); $constraints[] = $required; } - if ($options['min_file_size'] || $options['max_file_size'] || $options['accept_file_types']) { - // @TODO gère les tailles + if ($options['min_file_size'] || $options['max_file_size']) { + $constraints[] = new FileSize([ + 'min' => $options['min_file_size'], + 'max' => $options['max_file_size'] + ]); + } + + if ($options['accept_file_types']) { + $constraints[] = new FileMimeType($options['accept_file_types']); } return $constraints; diff --git a/Validation/FileMimeType.php b/Validation/FileMimeType.php new file mode 100644 index 00000000..a1d3aa0e --- /dev/null +++ b/Validation/FileMimeType.php @@ -0,0 +1,44 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileMimeType extends Constraint +{ + /** + * @var string + */ + public $acceptedMimeTypes = null; + + /** + * @var string + */ + public $message = 'file_upload.acceptFileTypes'; + + /** + * {@inheritdoc} + */ + public function getDefaultOption() + { + return 'acceptedMimeTypes'; + } + + /** + * {@inheritdoc} + */ + public function getRequiredOptions() + { + return ['acceptedMimeTypes']; + } +} diff --git a/Validation/FileMimeTypeValidator.php b/Validation/FileMimeTypeValidator.php new file mode 100644 index 00000000..8e7f9a2c --- /dev/null +++ b/Validation/FileMimeTypeValidator.php @@ -0,0 +1,37 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Irstea\FileUploadBundle\Model\UploadedFileInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * Vérifie que le type MIME du fichier valide la contrainte. + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileMimeTypeValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$value instanceof UploadedFileInterface) { + return; + } + if (preg_match('@^'.$constraint->acceptedMimeTypes.'$@i', $value->getMimeType())) { + return; + } + $this->context->buildViolation($constraint->message) + ->setParameter('%displayName%', $value->getDisplayName()) + ->setParameter('%mimeType%', $value->getMimeType()) + ->addViolation(); + } +} diff --git a/Validation/FileSize.php b/Validation/FileSize.php new file mode 100644 index 00000000..976bae31 --- /dev/null +++ b/Validation/FileSize.php @@ -0,0 +1,37 @@ +<?php +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileSize extends Constraint +{ + /** + * @var integer|null + */ + public $min = null; + + /** + * @var integer|null + */ + public $max = null; + + /** + * @var string + */ + public $minMessage = 'file_upload.minFileSize'; + + /** + * @var string + */ + public $maxMessage = 'file_upload.maxFileSize'; +} diff --git a/Validation/FileSizeValidator.php b/Validation/FileSizeValidator.php new file mode 100644 index 00000000..fec88094 --- /dev/null +++ b/Validation/FileSizeValidator.php @@ -0,0 +1,42 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Irstea\FileUploadBundle\Model\UploadedFileInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * Vérifie que la taille du fichier + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileSizeValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$value instanceof UploadedFileInterface) { + return; + } + if ($constraint->min && $value->getSize() < $constraint->min) { + $this->context->buildViolation($constraint->minMessage) + ->setParameter('%displayName%', $value->getDisplayName()) + ->setParameter('%min%', $constraint->min) + ->addViolation(); + } + if ($constraint->max && $value->getSize() > $constraint->max) { + $this->context->buildViolation($constraint->maxMessage) + ->setParameter('%displayName%', $value->getDisplayName()) + ->setParameter('%max%', $constraint->max) + ->addViolation(); + } + } +} -- GitLab