<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ProductBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Liip\ImagineBundle\Model\FileBinary;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class MediaSecurityController extends AbstractController
{
public function fileAction(Request $request): Response
{
$path = $request->query->get('path', '');
$filter = $request->query->get('filter', null);
// $publicDir = '/public';
$publicDir = '/public_html';
if(!$filter){
$filename = $this->container->getParameter('kernel.project_dir') . $publicDir .'/'. $path;
return new BinaryFileResponse($filename);
}else{
$filenameFilter = $this->container->getParameter('kernel.project_dir') . $publicDir .'/media/cache/'. $filter .'/'. $path;
if (file_exists($filenameFilter)) {
return new BinaryFileResponse($filenameFilter);
}
$filename = $this->container->getParameter('kernel.project_dir') . $publicDir .'/media/image/' . $path;
if (file_exists($filename)) {
$filterManager = $this->container->get('liip_imagine.filter.manager');
$cacheManager = $this->container->get('liip_imagine.cache.manager');
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$extensions = $this->container->getParameter('image_extensions');
if(in_array($ext, $extensions)){
$binary = $filterManager->applyFilter(new FileBinary($filename, mime_content_type($filename), $ext), $filter);
$resolver = $cacheManager->store($binary, $path, $filter);
$filterFilename = $cacheManager->getBrowserPath($path, $filter);
$filenameFilter = $this->container->getParameter('kernel.project_dir') . $publicDir .'/media/cache/'. $filter .'/'. $path;
if (file_exists($filenameFilter)) {
return new BinaryFileResponse($filenameFilter);
}
return new BinaryFileResponse($filename);
}
}
}
return new JsonResponse(null, 404);
}
}