FileUploadExtension.php 4.75 KiB
<?php
/*
 * Copyright (C) 2015 IRSTEA
 * All rights reserved.
 * @package irstea/file-upload-bundle
 * @copyright 2015 Irstea
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
namespace Irstea\FileUploadBundle\Twig;
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Irstea\FileUploadBundle\Utils\MimeTypeIcon;
use \NumberFormatter;
use Symfony\Component\Translation\TranslatorInterface;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFilter;
use Twig_SimpleFunction;
/**
 * Description of FileUploadExtension
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
class FileUploadExtension extends Twig_Extension
    /**
     * @var TranslatorInterface
    private $translator;
    /**
     * @param TranslatorInterface $translator
    public function __construct(TranslatorInterface $translator)
        $this->translator = $translator;
    /**
     * {@inheritdoc}
    public function getFunctions()
        return [
            new Twig_SimpleFunction(
                'irstea_uploaded_file',
                [$this, 'formatUploadedFile'],
                    'needs_environment' => true,
                    'is_safe' => ['html']
            new Twig_SimpleFunction(
                'irstea_file_icon',
                [$this, 'formatFileIcon'],
                ['is_safe' => ['html']]
    /**
     * {@inheritdoc}
    public function getFilters()
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
{ return [ new Twig_SimpleFilter( 'irstea_file_size', [$this, 'formatFileSize'], ['needs_context' => true] ), ]; } /** * {@inheritdoc} */ public function getName() { return "irstea_file_upload"; } /** Genère un tag HTML pour représenter le fichier. * * Inclut un lien sécurisé qui permet de télécharger le fichier, mais uniquement à l'utilisateur qui a affiché la page. * * @param Twig_Environment $env * @param UploadedFileInterface $file * * @return string */ public function formatUploadedFile(Twig_Environment $env, $file = null) { if(null === $file) { return ''; } elseif($file instanceof \Irstea\FileUploadBundle\Entity\UploadedFile) { $file = $file->toArray(); } elseif(!is_array($file)) { throw new \InvalidArgumentException("irstea_uploaded_file expects an UploadedFile instance, array or null"); } return $env->render('IrsteaFileUploadBundle:Extension:uploaded_file.html.twig', ['file' => $file]); } /** Génère un tag "icône" Font-Awesome pour le type MIME indiqué. * * @param string $mimeType Type MIME. * * @return string Un tag <i></i> avec les classes Font-Awesome. * * @uses MimeTypeIcon::getMimeTypeIcon */ public function formatFileIcon($mimeType) { $icon = MimeTypeIcon::getMimeTypeIcon($mimeType); return sprintf('<i class="icon fa fa-file%s-o"></i>', ($icon && $icon !== 'file') ? ('-'.$icon) : ''); } /** Formate une taille de fichier. * * @param array $context Contexte Twig. * @param integer $size Taille du fichier. * @param integer $precision Nombre de chiffres après la virgule. * @param string $locale Langue * * @return string * * @uses \NumberFormatter */ public function formatFileSize($context, $size, $precision = null, $locale = null) { if(!is_numeric($size)) { return ''; }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
if($size > 1000000000) { $size /= 1000000000.0; $defaultPrecision = 2; $unit = 'Gi'; } elseif($size > 1000000) { $size /= 1000000.0; $defaultPrecision = 1; $unit = 'Mi'; } elseif($size > 1000) { $size /= 1000.0; $defaultPrecision = 1; $unit = 'Ki'; } else { $unit = ''; $defaultPrecision = 0; } if(null === $locale) { $locale = $context['app']->getRequest()->getLocale(); } $formatter = NumberFormatter::create($locale, NumberFormatter::DECIMAL); $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, null !== $precision ? $precision : 0); $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, null !== $precision ? $precision : $defaultPrecision); return $this->translator->trans( 'file_size(%size%,%unit%)', [ '%size%' => $formatter->format($size), '%unit%' => $unit, ], 'file_upload', $locale ); } }