<?php
/**
* Copyright (C) SUEZ Smart Solutions - All Rights Reserved
* On’Connect Gateway Management, 2018
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* For the full copyright and license information, please report to the LICENSE CONTRACT.
*/
namespace TSMS\TradeBundle\EventListener;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
/**
* This class listens to the "oneup_uploader.post_persist" event
* It customizes the response sent to the front after each file upload to an existing request.
*/
class UploadListener
{
/**
* @var RouterInterface
*/
private $router;
/**
* @param RouterInterface $router
*/
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
/**
* @param PostPersistEvent $event
*/
public function onUpload(PostPersistEvent $event)
{
$response = $event->getResponse();
/** @var File $file */
$file = $event->getFile();
/** @var Request $request */
$request = $event->getRequest();
/** @var UploadedFile[] $requestFileList */
$requestFileList = $request->files->get('files');
$requestFile = $requestFileList[0];
$filename = $file->getFilename();
$response['files'] = [[
'original_filename' => $requestFile->getClientOriginalName(),
'temp_filename' => $filename,
'url' => $this->router->generate('request_attachment_show', ['filename' => $filename]),
'type' => $file->getMimeType(),
'size' => $file->getSize(),
'deleteType' => 'DELETE',
'deleteUrl' => $this->router->generate('request_attachment_delete', ['filename' => $filename]),
'deleteWithCredentials' => true,
]];
}
}