symfonyoneupuploaderbundle

how to set max_size in OneupUploaderBundle?


I want to set the max_size of files to be uploaded to 2m. I have the config below but it is still uploading even 4m files...

oneup_uploader:
    mappings:
        motors:
            frontend: blueimp 
            enable_progress: true 
            max_size: 2m

I have seen that issue #92 and it seems that there is an extra word in my config which mappings. Is there any thing wrong??

Thanks


Solution

  • I will suggest an alternative way, since doing it like this didn't worked for me too. Do it with event listeners:

    // Resources/services.yml
    yourbundle.oneuploadvalidatorlistener:
        class: Amine\yourBundle\EventListener\oneupValidator
        arguments: [%your_own_defined_maxBytes%]
        tags:
           - { name: kernel.event_listener, event: oneup_uploader.validation, method: onValidate }
    

    Note that if you have multiple uploaders and want to target one of them, in the event you can add it in the middle like oneup_uploader.motors.validation (I'm quite sure worked for me this way )

    And then just create that EventListener class:

    namespace Amine\yourBundle\EventListener;
    class oneupValidator {
    private $max_size;
    
    function __construct($max_size) {
       $this->max_size =$max_size;
    }
    function onValidate(ValidationEvent $event) {
        $file = $event->getFile();
    // Do your logic here to check the size, and throw an exception if it does not validate 
        new ValidationException('error.max_size'); //Or your own error message
    }
    }
    

    This is just a theoretical solution try to adapt it to your needs.