src/TSMS/TradeBundle/EventListener/UploadListener.php line 40

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT.
  8. */
  9. namespace TSMS\TradeBundle\EventListener;
  10. use Oneup\UploaderBundle\Event\PostPersistEvent;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\HttpFoundation\File\UploadedFile;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\RouterInterface;
  15. /**
  16.  * This class listens to the "oneup_uploader.post_persist" event
  17.  * It customizes the response sent to the front after each file upload to an existing request.
  18.  */
  19. class UploadListener
  20. {
  21.     /**
  22.      * @var RouterInterface
  23.      */
  24.     private $router;
  25.     /**
  26.      * @param RouterInterface $router
  27.      */
  28.     public function __construct(RouterInterface $router)
  29.     {
  30.         $this->router $router;
  31.     }
  32.     /**
  33.      * @param PostPersistEvent $event
  34.      */
  35.     public function onUpload(PostPersistEvent $event)
  36.     {
  37.         $response $event->getResponse();
  38.         /** @var File $file */
  39.         $file $event->getFile();
  40.         /** @var Request $request */
  41.         $request $event->getRequest();
  42.         /** @var UploadedFile[] $requestFileList */
  43.         $requestFileList $request->files->get('files');
  44.         $requestFile     $requestFileList[0];
  45.         $filename          $file->getFilename();
  46.         $response['files'] = [[
  47.             'original_filename'     => $requestFile->getClientOriginalName(),
  48.             'temp_filename'         => $filename,
  49.             'url'                   => $this->router->generate('request_attachment_show', ['filename' => $filename]),
  50.             'type'                  => $file->getMimeType(),
  51.             'size'                  => $file->getSize(),
  52.             'deleteType'            => 'DELETE',
  53.             'deleteUrl'             => $this->router->generate('request_attachment_delete', ['filename' => $filename]),
  54.             'deleteWithCredentials' => true,
  55.         ]];
  56.     }
  57. }