diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index e69f307963a64885c5ed2c79801a257a6d574477..420bb164d5c32f6e6b585e2429420f6710c50496 100644 --- a/Entity/UploadedFile.php +++ b/Entity/UploadedFile.php @@ -552,6 +552,7 @@ class UploadedFile implements UploadedFileInterface 'type' => $this->getMimeType(), 'etat' => $this->getEtat(), 'checksum' => $this->getChecksum(), + 'icon' => MimeTypeIcon::getMimeTypeIcon($this->getMimeType()) ]; } } diff --git a/Resources/js/widget/file_upload.js b/Resources/js/widget/file_upload.js index 91808f9773b466ddbf66bc987666e0681c561379..c93c531c3180b80c24eb2e6d0a10ef81aff4119e 100644 --- a/Resources/js/widget/file_upload.js +++ b/Resources/js/widget/file_upload.js @@ -103,6 +103,11 @@ .attr('data-url', file.delete_url + csrfQuery); row.find('input:hidden') .val(file.id); + if(file.icon && file.icon !== 'file') { + row.find('.icon') + .removeClass('fa-file-o') + .addClass('fa-file-'+file.icon+'-o'); + } } rows = rows.add(row); }); diff --git a/Resources/views/Form/file_upload.html.twig b/Resources/views/Form/file_upload.html.twig index c4faca4a5409bfc0cd39135ea45c9b4742075306..dd575032815296874ed6a62802ea1f084a6cfa21 100644 --- a/Resources/views/Form/file_upload.html.twig +++ b/Resources/views/Form/file_upload.html.twig @@ -2,7 +2,7 @@ {% block file_upload_progress_prototype %} <div class="template-upload fileinput-entry"> <div class="progress-text" style="display: none"></div> - <span class="name">Monfichier.com</span> (<span class="size">1.5 Gio</span>) + <i class="fa fa-circle-o-notch fa-spin icon"></i> <span class="name"></span> (<span class="size"></span>) <span class="error" style="display: none"></span> <a href="#" class="danger cancel" title="{% trans %}button.cancel{% endtrans %}"> {{- irstea_icon('remove') -}} @@ -17,9 +17,9 @@ {%- set file = file|default([]) %} <div class="template-download fileinput-entry"> <input type="hidden" name="{{ full_name }}{% if multiple %}[]{% endif %}" value="{{ file.id|default }}"/> - <span class="name"> - <a{% if file %} href="{{ path('file_upload_get_content', {id: file.id, token: csrf_token}) }}"{% endif %}>{{ file.originalFilename|default }}</a> - </span> + <i class="fa fa-file{% if file.icon|default(false) %}-{{ file.icon }}{% endif %}-o icon"></i> <span class="name"> + <a{% if file %} href="{{ path('file_upload_get_content', {id: file.id, token: csrf_token}) }}"{% endif %}>{{ file.name|default }}</a> + </span> (<span class="size"> {{- file.size|default -}} </span>) diff --git a/Utils/MimeTypeIcon.php b/Utils/MimeTypeIcon.php new file mode 100644 index 0000000000000000000000000000000000000000..e14a824aafa3821efdb1ceff852b1e181839c9ed --- /dev/null +++ b/Utils/MimeTypeIcon.php @@ -0,0 +1,59 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Utils; + +/** + * Description of MimeTypeIcon + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +final class MimeTypeIcon +{ + private static $icons = [ + 'application/x-gzip' => 'zip', + 'application/vnd.oasis.opendocument.text' => 'word', + 'application/vnd.oasis.opendocument.spreadsheet' => 'excel', + 'application/vnd.oasis.opendocument.presentation' => 'powerpoint', + 'application/vnd.oasis.opendocument.graphics' => 'image', + 'application/vnd.ms-excel' => 'excel', + 'application/vnd.ms-powerpoint' => 'powerpoint', + 'application/msword' => 'word', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'excel', + 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'powerpoint', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'word', + 'application/pdf' => 'pdf', + 'application/zip' => 'archive', + 'application/x-rar-compressed' => 'archive', + 'application/x-gtar' => 'archive', + 'application/x-gzip' => 'archive', + 'application/x-tar' => 'archive' + ]; + + /** + * + * @param string $mimeType + * @return string|null + */ + public static function getMimeTypeIcon($mimeType) + { + if(!is_string($mimeType)) { + return null; + } + + if(isset(self::$icons[$mimeType])) { + return self::$icons[$mimeType]; + } + + $matches = []; + if(preg_match('@^(text|audio|video|image)/@', $mimeType, $matches)) { + return $matches[1]; + } + + return null; + } +}