Search code examples
symfonysonata-adminsonata

Sonata media validation at admin


I'm trying to validate image. I saw answer at Sonata Media: The file could not be found

How to validate image (width and height). I need help on that. There is no proper doc in net.


Solution

  • To validate image dimensions using sonata media you need to override sonata media's ImageProvider class, sonata uses this class to handle image manipulation.If you already have an extended bundle of sonata media bundle then in services.yml file you can define your own provider as below ,make sure your yml file is included in main config.yml

    parameters:
        sonata.media.provider.file.class: Application\Sonata\MediaBundle\Provider\ImageProvider
    

    Now create your provider and extend it with sonata media's ImageProvider overrider validate() function and define your own validation or can override buildCreateForm()/buildEditForm() and define your asserts for binaryContent field

    namespace Application\Sonata\MediaBundle\Provider;

    //... other uses classes
    use Sonata\MediaBundle\Provider\ImageProvider as BaseProvider;
    
    class ImageProvider extends BaseProvider
    {
    
        public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail, array $allowedExtensions = array(), array $allowedMimeTypes = array(), MetadataBuilderInterface $metadata = null)
        {
            parent::__construct($name, $filesystem, $cdn, $pathGenerator, $thumbnail);
    
            $this->allowedExtensions = $allowedExtensions;
            $this->allowedMimeTypes = $allowedMimeTypes;
            $this->metadata = $metadata;
        }
    
        /**
         * {@inheritdoc}
         */
        public function validate(ErrorElement $errorElement, MediaInterface $media)
        {
            if (!$media->getBinaryContent() instanceof \SplFileInfo) {
                return;
            }
    
            if ($media->getBinaryContent() instanceof UploadedFile) {
                $fileName = $media->getBinaryContent()->getClientOriginalName();
            } elseif ($media->getBinaryContent() instanceof File) {
                $fileName = $media->getBinaryContent()->getFilename();
            } else {
                throw new \RuntimeException(sprintf('Invalid binary content type: %s', get_class($media->getBinaryContent())));
            }
    
            if (!in_array(strtolower(pathinfo($fileName, PATHINFO_EXTENSION)), $this->allowedExtensions)) {
                $errorElement
                    ->with('binaryContent')
                    ->addViolation('Invalid extensions')
                    ->end();
            }
    
            if (!in_array($media->getBinaryContent()->getMimeType(), $this->allowedMimeTypes)) {
                $errorElement
                    ->with('binaryContent')
                    ->addViolation('Invalid mime type : ' . $media->getBinaryContent()->getMimeType())
                    ->end();
            }
    
            if ($media->getWidth() > '1280' || $media->getHeight() > 1280) {
                $errorElement
                    ->with('binaryContent')
                    ->addViolation('Invalid File Dimension : Please upload 1280px * (1280px) image')
                    ->end();
            }
        }
    
    }