Commit 7d5aa8bf authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Utilise le template Twig pour mettre à jour les uploads terminés.

Ça permettra de surchager cet élément.
Showing with 35 additions and 36 deletions
+35 -36
......@@ -23,8 +23,9 @@ use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Templating\EngineInterface;
/**
* @Route("/api/files", service="irstea_file_upload.upload_controller")
......@@ -41,9 +42,9 @@ class UploadController extends Controller
/**
*
* @var RouterInterface
* @var UrlGeneratorInterface
*/
protected $router;
protected $urlGenerator;
/**
* @var CsrfProviderInterface
......@@ -55,15 +56,26 @@ class UploadController extends Controller
*/
protected $tokenStorage;
/**
* @var EngineInterface
*/
protected $templating;
/**
*
*/
public function __construct(FileManagerInterface $fileManager, RouterInterface $router, CsrfProviderInterface $csrfProvider, TokenStorageInterface $tokenStorage)
{
public function __construct(
FileManagerInterface $fileManager,
UrlGeneratorInterface $urlGenerator,
CsrfProviderInterface $csrfProvider,
TokenStorageInterface $tokenStorage,
EngineInterface $templating
) {
$this->fileManager = $fileManager;
$this->router = $router;
$this->urlGenerator = $urlGenerator;
$this->csrfProvider = $csrfProvider;
$this->tokenStorage = $tokenStorage;
$this->templating = $templating;
}
/**
......@@ -89,7 +101,7 @@ class UploadController extends Controller
$parameters = ['id' => $file->getId()];
$deleteUrl = $this->router->generate('file_upload_delete', $parameters);
$deleteUrl = $this->urlGenerator->generate('file_upload_delete', $parameters);
return $this->createResponse(
Response::HTTP_CREATED,
......@@ -97,7 +109,7 @@ class UploadController extends Controller
array_merge(
$file->toArray(),
[
'put_url' => $this->router->generate('file_upload_put_content', $parameters),
'put_url' => $this->urlGenerator->generate('file_upload_put_content', $parameters),
'delete_type' => 'DELETE',
'delete_url' => $deleteUrl
]
......@@ -174,33 +186,27 @@ class UploadController extends Controller
*/
protected function completeUpload(UploadedFileInterface $file)
{
$status = Response::HTTP_OK;
$message = 'File uploaded';
try {
$this->fileManager->completed($file);
} catch(RejectedFileException $ex) {
$status = Response::HTTP_FORBIDDEN;
$message = 'File rejected: '.$ex->getMessage();
return $this->createResponse(Response::HTTP_FORBIDDEN, 'File rejected: '.$ex->getMessage());
}
$parameters = ['id' => $file->getId()];
return $this->createResponse(
$status,
$message,
$fileData = array_merge(
$file->toArray(),
[
'files' => [
array_merge(
$file->toArray(),
[
'url' => $this->router->generate('file_upload_get_content', $parameters),
'delete_type' => 'DELETE',
'delete_url' => $this->router->generate('file_upload_delete', $parameters),
]
)
]
'url' => $this->urlGenerator->generate('file_upload_get_content', $parameters),
'delete_type' => 'DELETE',
'delete_url' => $this->urlGenerator->generate('file_upload_delete', $parameters),
'repr' => $this->templating->render(
'IrsteaFileUploadBundle:Extension:uploaded_file.html.twig',
['file' => $file->toArray()]
)
]
);
return $this->createResponse(Response::HTTP_OK, 'File uploaded.', ['files' => [$fileData]]);
}
/**
......
......@@ -32,6 +32,7 @@ services:
- @router
- @form.csrf_provider
- @security.token_storage
- @templating
# Type de champ
irstea_file_upload.field_type:
......
......@@ -140,20 +140,12 @@
showError(row, file.error);
return;
}
row.find('.size').text(formatFileSize(file.size));
row.find('.name a')
.text(file.name)
.prop('href', file.url + csrfQuery);
row.find('.repr').innerHtml(file.repr);
row.find('.delete')
.attr('data-type', file.delete_type)
.attr('data-url', file.delete_url + csrfQuery);
row.find('input.id')
.val(file.id);
if (file.icon && file.icon !== 'file') {
row.find('.icon')
.removeClass('fa-file-o')
.addClass('fa-file-' + file.icon + '-o');
}
});
return rows;
},
......
......@@ -18,7 +18,7 @@
{%- set file = file|default([]) %}
<div class="template-download fileinput-entry">
<input class="id" type="hidden" name="{{ full_name }}{% if multiple %}[{{index|default('__index__')}}]{% endif %}[id]" value="{{ file.id|default }}"/>
{{ irstea_uploaded_file(file) }}
<span class="repr">{{ irstea_uploaded_file(file) }}</span>
<input class="form-control description" type="text" placeholder="{% trans from 'file_upload' %}form.description.placeholder{% endtrans %}" name="{{ full_name }}{% if multiple %}[{{index|default('__index__')}}]{% endif %}[description]" value="{{ file.description|default }}"/>
<span class="error"></span>
<a href="#" class="danger delete {%- if disabled or read_only %} disabled{% endif -%}" title="{% trans %}button.delete{% endtrans %}">
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment