<commit_before><?php

namespace Alpixel\Bundle\MediaBundle\Twig\Extension;

use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManager;

class MediaPreviewExtension extends \Twig_Extension
{
    protected $entityManager;
    protected $requestStack;
    protected $previewIcons;

    public function __construct(RequestStack $requestStack, EntityManager $entityManager, $previewIcons)
    {
        $this->requestStack  = $requestStack->getCurrentRequest();
        $this->entityManager = $entityManager;
        $this->previewIcon   = $previewIcons;
    }

    public function getFilters()
    {
        return array(
            'previewIcon' => new \Twig_Filter_Method($this, 'previewIconFilter', array(
                'is_safe' => array('html'),
                'needs_environment' => true,
                )
            )
        );
    }

    public function previewIconFilter(\Twig_Environment $twig, $secretKey = '')
    {
        if($secretKey == '')
            return '';

        $mimeType = $this->getMimeType($secretKey);
        $icon     = '';
        $link     = '';

        if(preg_match('/^image/', $mimeType) === 0) {
            $icon = $this->getIcon($mimeType);
            $link = $this->generatePath(true, $icon);
        }
        else {
            $link = $this->generatePath(false, $secretKey);
        }

        return $twig->render('MediaBundle:Form:blocks/show_icon.html.twig', array(
            'link'      => $link,
            'icon'      => $icon,
            'secretKey' => $secretKey,
        ));
    }

    protected function getIcon($mimeType)
    {
        $explMime = explode('/', $mimeType);
        $mime     = (isset($explMime[1])) ? $explMime[1] : '';
        return (array_key_exists($mime, $this->previewIcon)) ? $this->previewIcon[$mime] : $this->previewIcon['unknown'];
    }

    protected function generatePath($isIcon, $str)
    {
        if($isIcon === true) {
            return $this->requestStack->getSchemeAndHttpHost().$this->requestStack->getBasePath().'/bundles/media/images/'.$str;
        }

        return $this->requestStack->getSchemeAndHttpHost().$this->requestStack->getBaseUrl().'/media/'.$str.'/admin';
    }
<commit_msg>Add function for generate path from secret key<commit_after><?php

namespace Alpixel\Bundle\MediaBundle\Twig\Extension;

use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManager;

class MediaPreviewExtension extends \Twig_Extension
{
    protected $entityManager;
    protected $requestStack;
    protected $previewIcons;

    public function __construct(RequestStack $requestStack, EntityManager $entityManager, $previewIcons)
    {
        $this->requestStack  = $requestStack->getCurrentRequest();
        $this->entityManager = $entityManager;
        $this->previewIcon   = $previewIcons;
    }

    public function getFilters()
    {
        return array(
            'previewIcon' => new \Twig_Filter_Method($this, 'previewIconFilter', array(
                'is_safe' => array('html'),
                'needs_environment' => true,
                )
            )
        );
    }

    public function previewIconFilter(\Twig_Environment $twig, $secretKey = '')
    {
        if($secretKey == '')
            return '';

        $mimeType = $this->getMimeType($secretKey);
        $icon     = '';
        $link     = '';

        if(preg_match('/^image/', $mimeType) === 0) {
            $icon = $this->getIcon($mimeType);
            $link = $this->generatePath(true, $icon);
        }
        else {
            $link = $this->generatePath(false, $secretKey);
        }

        return $twig->render('MediaBundle:Form:blocks/show_icon.html.twig', array(
            'link'      => $link,
            'icon'      => $icon,
            'secretKey' => $secretKey,
        ));
    }

    protected function getIcon($mimeType)
    {
        $explMime = explode('/', $mimeType);
        $mime     = (isset($explMime[1])) ? $explMime[1] : '';
        return (array_key_exists($mime, $this->previewIcon)) ? $this->previewIcon[$mime] : $this->previewIcon['unknown'];
    }

    public function generatePathFromSecretKey($secretKey)
    {
         if($secretKey == '')
            return '';

        $mimeType = $this->getMimeType($secretKey);

        if(preg_match('/^image/', $mimeType) === 0) {
            $icon = $this->getIcon($mimeType);
            return $this->generatePath(true, $icon);
        }

        return $this->generatePath(false, $secretKey);
    }

    protected function generatePath($isIcon, $str)
    {
        if($isIcon === true) {
            return $this->requestStack->getSchemeAndHttpHost().$this->requestStack->getBasePath().'/bundles/media/images/'.$str;
        }

        return $this->requestStack->getSchemeAndHttpHost().$this->requestStack->getBaseUrl().'/media/'.$str.'/admin';
    }

{'fraction_non_alphanumeric': 0.1514279843846312, 'fraction_numerical': 0.0014382576535853708, 'mean_word_length': 2.3970690858339148, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1090244', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1402, 'n_words': 225}