src/Controller/AssetController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use FrontendPermissionToolkitBundle\Service;
  4. use Pimcore\Controller\FrontendController;
  5. use Pimcore\Model\Asset;
  6. use Pimcore\Tool\Admin;
  7. use Pimcore\Tool\Storage;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\StreamedResponse;
  10. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  11. use Symfony\Component\Routing\Matcher\UrlMatcher;
  12. use Symfony\Component\Routing\RouteCollection;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class AssetController extends DefaultController
  16. {
  17.     /** @var TranslatorInterface */
  18.     protected $translator;
  19.     /** 
  20.      * @var Service 
  21.      */
  22.     private $permissionService;
  23.     public function __construct(TranslatorInterface $translatorService $permissionService )
  24.     {
  25.         $this->translator $translator;
  26.         $this->permissionService $permissionService;
  27.         
  28.         parent::__construct($translator$permissionService);
  29.     }
  30.     public function protectedAssetAction(Request $requestRouterInterface $router)
  31.     {
  32.         // IMPORTANT!
  33.         // Add your code here to check permission!
  34.         $currentUser Admin::getCurrentUser();
  35.         if ($currentUser == null) {
  36.             $currentUser \Pimcore\Tool\Session::getReadonly()->get("user");
  37.         }
  38.         // the following code is responsible to deliver asset & thumbnail contents
  39.         // modify it the way you need it for your use-case
  40.         $pathInfo $request->getPathInfo();
  41.         if($pathInfo == "/faq"){
  42.             $params $this->faqAction($request);
  43.             return $this->render("default/faq.html.twig"$params);
  44.         }
  45.         if($pathInfo == "/Emails/PublicationNewsletterEmail"){
  46.             return $this->sentPublicationNewsletterEmailAction($request);
  47.         }
  48.         $asset Asset::getByPath($pathInfo);
  49.         if ($asset){
  50.             if(!$currentUser){
  51.                 throw new AccessDeniedHttpException('Access denied.');
  52.             }
  53.             $stream $asset->getStream();
  54.             return new StreamedResponse(function () use ($stream) {
  55.                 fpassthru($stream);
  56.             }, 200, [
  57.                 'Content-Type' => $asset->getMimeType(),
  58.             ]);
  59.         } elseif (preg_match('@.*/(image|video)-thumb__[\d]+__.*@'$pathInfo$matches)) {
  60.             $storage Storage::get('thumbnail');
  61.             $storagePath urldecode($pathInfo);
  62.             if($storage->fileExists($storagePath)){
  63.                 $stream $storage->readStream($storagePath);
  64.                 return new StreamedResponse(function () use ($stream) {
  65.                     fpassthru($stream);
  66.                 }, 200, [
  67.                     'Content-Type' => $storage->mimeType($storagePath),
  68.                 ]);
  69.             } else {
  70.                 $pimcoreThumbnailRoute '_pimcore_service_thumbnail';
  71.                 $route $router->getRouteCollection()->get($pimcoreThumbnailRoute);
  72.                 $collection = new RouteCollection();
  73.                 $collection->add($pimcoreThumbnailRoute$route);
  74.                 $matcher = new UrlMatcher($collection$router->getContext());
  75.                 try {
  76.                     $parameters $matcher->matchRequest($request);
  77.                     return $this->forward('PimcoreCoreBundle:PublicServices:thumbnail'$parameters);
  78.                 } catch (\Exception $e) {
  79.                     // nothing to do
  80.                 }
  81.             }
  82.         }
  83.         throw new AccessDeniedHttpException('Access denied.');
  84.     }
  85. }