src/Sylius/Bundle/ProductBundle/Controller/MediaSecurityController.php line 26

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ProductBundle\Controller;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Liip\ImagineBundle\Model\FileBinary;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  18. class MediaSecurityController extends AbstractController
  19. {
  20.     public function fileAction(Request $request): Response
  21.     {
  22.         $path $request->query->get('path''');
  23.         $filter $request->query->get('filter'null);
  24.         // $publicDir = '/public';
  25.         $publicDir '/public_html';
  26.         if(!$filter){
  27.             $filename $this->container->getParameter('kernel.project_dir') . $publicDir .'/'$path;
  28.             return new BinaryFileResponse($filename);
  29.         }else{
  30.             $filenameFilter $this->container->getParameter('kernel.project_dir') . $publicDir .'/media/cache/'$filter .'/'$path;
  31.             if (file_exists($filenameFilter)) {
  32.                 return new BinaryFileResponse($filenameFilter);
  33.             }
  34.             $filename $this->container->getParameter('kernel.project_dir') . $publicDir .'/media/image/' $path;
  35.             if (file_exists($filename)) {
  36.                 $filterManager $this->container->get('liip_imagine.filter.manager');
  37.                 $cacheManager $this->container->get('liip_imagine.cache.manager');
  38.                 
  39.                 $ext pathinfo($filenamePATHINFO_EXTENSION);
  40.                 $extensions $this->container->getParameter('image_extensions');
  41.                 if(in_array($ext$extensions)){
  42.                     $binary $filterManager->applyFilter(new FileBinary($filenamemime_content_type($filename), $ext), $filter);
  43.                     $resolver $cacheManager->store($binary$path$filter);
  44.                     $filterFilename $cacheManager->getBrowserPath($path$filter);
  45.                     $filenameFilter $this->container->getParameter('kernel.project_dir') . $publicDir .'/media/cache/'$filter .'/'$path;
  46.                     if (file_exists($filenameFilter)) {
  47.                         return new BinaryFileResponse($filenameFilter);
  48.                     }
  49.                     return new BinaryFileResponse($filename);
  50.                 }
  51.             } 
  52.         }
  53.         
  54.         return new JsonResponse(null404);
  55.         
  56.     }
  57. }