phpsymfonypluploadoneupuploaderbundle

Symfony 3.1 and OneUpUploaderBundle + Plupload = UploadListener is not called


CLARIFICATION

This question differ from similar question by that I am using different upload library (Plupload in this question versus FineUploader in similar question).

At the time of writing this question I did not know where the problem lied - so I expected usage of different library might show a way to the solution of the problem.

INTRODUCTION

Three weeks ago I created similar question about upload using OneupUploaderBundle, but with FineUploader library. Sadly there are no answers yet. In the meantime I tried setting up different upload library (Blueimp).

One week ago I created similar question about upload using OneupUploaderBundle, but with Blueimp jQuery uplaod library. Sadly there are no answers yet. In the meantime I tried setting up different upload library (Plupload).

I am developing on Windows 10 Pro with XAMPP [1] which includes PHP v7.0.8.

I am using Symfony v3.1.2, OneupUploaderBundle and Plupload library in order to upload files to server.

While setting things up I followed documentation of OneUpUploaderBundle [2] and Plupload upload library [3], [4].

PROBLEM

I want to upload files to some directory and then check their mime type and validate is file mime type allowed for uploading and after that - move them to custom directory (that can change from file to file), finally i want to persist file path and file name to database.

File upload works fine and files are uploaded to oneup_uploader_endpoint('gallery'). Even custom file Namer works and allows upload to custom directory.

However, listeners are not called and are displayed in Symfony Profiler Events section Not Called Listeners!

That is unfortunate, as I want to move files to custom directory and save info about file to database unsing Post_Persist event. Additionally, validation is not called ether.

CODE

my services.yml

services:
    app.upload_listener:
        class: AppBundle\EventListener\UploadListener
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: kernel.event_listener, event: oneup_uploader.post_persist.default_uploader, method: onPostUpload }
            - { name: kernel.event_listener, event: oneup_uploader.post_persist.default_uploader, method: onUpload }

    app.upload_unique_namer:
        class: AppBundle\Uploader\Naming\UploadUniqueNamer
        arguments: ["@session"]

my custom Namer

<?php

namespace AppBundle\Uploader\Naming;

use Oneup\UploaderBundle\Uploader\File\FileInterface;
use Oneup\UploaderBundle\Uploader\Naming\NamerInterface;
use Symfony\Component\HttpFoundation\Session\Session;

class UploadUniqueNamer implements NamerInterface
{
    private $session;

    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    /**
     * Creates a user directory name for the file being uploaded.
     *
     * @param FileInterface $file
     * @return string The directory name.
     */
    public function name(FileInterface $file)
    {
        $upload_files_path = $this->session->get('upload_files_path');
        $unique_name = uniqid();

        return sprintf('%s/%s_%s',
            $upload_files_path,
            $unique_name,
            $file->getClientOriginalName()
        );
    }
}

my config.yml

oneup_uploader:
    mappings:
        gallery:
            frontend: plupload
            enable_progress: true
            namer: app.upload_unique_namer
            use_orphanage: false
            allowed_mimetypes: [image/png, image/jpg, image/jpeg, image/gif]
            max_size: 200M

my upload listener:

<?php

namespace AppBundle\EventListener;

use Oneup\UploaderBundle\Event\PreUploadEvent;
use Oneup\UploaderBundle\Event\PostUploadEvent;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\Product;

class UploadListener
{
    /**
     * @var EntityManager
     */
    private $entityManager;

    //protected $originalName;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function onUpload(PostPersistEvent $event)
    {
        $file = $event->getFile();
        $this->originalName = $file->getClientOriginalName();
        var_dump('In -- OnUpload');
        die();
    }

    public function onPostUpload(PostPersistEvent $event)
    {
        $file = $event->getFile();

        $object = new Product();
        $object->setName($file->getClientOriginalName());
        //$object->setName($file->getPathName());

        $this->entityManager->persist($object);
        $this->entityManager->flush();
        var_dump('In -- OnPostUpload');
        die();
    }
}

upload.html.twig

{% extends 'base.html.twig' %}

{% block stylesheets %}
    {{ parent() }}
    <link type="text/css" rel="stylesheet" href="{{ asset('js/jquery-ui-1.12.0/jquery-ui.css') }}" />
    <link type="text/css" rel="stylesheet" href="{{ asset('js/plupload/jquery.ui.plupload/css/jquery.ui.plupload.css') }}" media="screen" />
{% endblock %}

{% block content %}
    <div id="box-upload">
        <div id="uploader">
            <p>Your browser doesn't have HTML5 support.</p>
        </div>
    </div>
{% endblock %}

{% block javascripts %}
    <script type="text/javascript" src="{{ asset('js/jquery-2.2.4.js') }}"></script>
    <script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
    <script type="text/javascript" src="{{ asset('js/jquery-ui-1.12.0/jquery-ui.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/plupload/plupload.full.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('js/plupload/jquery.ui.plupload/jquery.ui.plupload.js') }}"></script>
    <script type="text/javascript">
        $(function()
        {
            $("#uploader").plupload(
            {
                // General settings
                runtimes : 'html5',
                url: "{{ oneup_uploader_endpoint('gallery') }}",
                multi_selection: false,

                // Maximum file size
                max_file_size: '10mb',

                chunk_size: '1mb',

                // Resize images on clientside if we can
                resize: {
                    width: 200,
                    height: 200,
                    quality: 90,
                    crop: true // crop to exact dimensions
                },

                // Specify what files to browse for
                filters: [
                    {title: "Image files", extensions : "jpg,jpeg,png,gif"},
                    {title: "Zip files", extensions : "zip,avi"}
                ],

                // Rename files by clicking on their titles
                rename: true,

                // Sort files
                sortable: true,

                // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
                dragdrop: true,

                // Views to activate
                views: {
                    list: true,
                    thumbs: false, // Show thumbs
                    active: 'list'
                },

                // Flash settings
                flash_swf_url: '/plupload/js/Moxie.swf',

                // Silverlight settings
                silverlight_xap_url: '/plupload/js/Moxie.xap'

            });
        });
    </script>
{% endblock %}

UPDATES

  1. none yet

CONCLUSION

Please advise.

Thank You for your time and knowledge.


Solution

  • The problem in question was caused by misconfiguration of event listeners in services.yml. Here is working configuration.

    my services.yml

    services:
        app.upload_listener:
            class: AppBundle\EventListener\UploadListener
            arguments: ["@doctrine.orm.entity_manager"]
            tags:
                - { name: kernel.event_listener, event: oneup_uploader.post_persist.gallery, method: onPostUpload }
                - { name: kernel.event_listener, event: oneup_uploader.post_persist.gallery, method: onUpload }
    
        app.upload_unique_namer:
            class: AppBundle\Uploader\Naming\UploadUniqueNamer
            arguments: ["@session"]