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.     public function __construct(TranslatorInterface $translatorService $permissionService )
  18.     {
  19.         parent::__construct($translator$permissionService);
  20.     }
  21.     public function protectedAssetAction(Request $requestRouterInterface $router)
  22.     {
  23.         // IMPORTANT!
  24.         // Add your code here to check permission!
  25.         $currentUser Admin::getCurrentUser();
  26.         if ($currentUser == null) {
  27.             $currentUser \Pimcore\Tool\Session::getReadonly()->get("user");
  28.         }
  29.         // the following code is responsible to deliver asset & thumbnail contents
  30.         // modify it the way you need it for your use-case
  31.         $pathInfo $request->getPathInfo();
  32.         if($pathInfo == "/faq"){
  33.             $params $this->faqAction($request);
  34.             return $this->render("default/faq.html.twig"$params);
  35.         }
  36.         if($pathInfo == "/Emails/PublicationNewsletterEmail"){
  37.             return $this->sentPublicationNewsletterEmailAction($request);
  38.         }
  39.         if ($pathInfo == "/Emails/OrderForm") {
  40.             return $this->sendEmailOrderFormAction($request);
  41.         }
  42.         if ($pathInfo == "/Emails/OrderFormCustomer") {
  43.             return $this->sendEmailOrderFormCustomerAction($request);
  44.         }
  45.         $asset Asset::getByPath($pathInfo);
  46.         if ($asset){
  47.             if(!$currentUser){
  48.                 throw new AccessDeniedHttpException('Access denied.');
  49.             }
  50.             $stream $asset->getStream();
  51.             return new StreamedResponse(function () use ($stream) {
  52.                 fpassthru($stream);
  53.             }, 200, [
  54.                 'Content-Type' => $asset->getMimeType(),
  55.             ]);
  56.         } elseif (preg_match('@.*/(image|video)-thumb__[\d]+__.*@'$pathInfo$matches)) {
  57.             $storage Storage::get('thumbnail');
  58.             $storagePath urldecode($pathInfo);
  59.             if($storage->fileExists($storagePath)){
  60.                 $stream $storage->readStream($storagePath);
  61.                 return new StreamedResponse(function () use ($stream) {
  62.                     fpassthru($stream);
  63.                 }, 200, [
  64.                     'Content-Type' => $storage->mimeType($storagePath),
  65.                 ]);
  66.             } else {
  67.                 $pimcoreThumbnailRoute '_pimcore_service_thumbnail';
  68.                 $route $router->getRouteCollection()->get($pimcoreThumbnailRoute);
  69.                 $collection = new RouteCollection();
  70.                 $collection->add($pimcoreThumbnailRoute$route);
  71.                 $matcher = new UrlMatcher($collection$router->getContext());
  72.                 try {
  73.                     $parameters $matcher->matchRequest($request);
  74.                     return $this->forward('PimcoreCoreBundle:PublicServices:thumbnail'$parameters);
  75.                 } catch (\Exception $e) {
  76.                     // nothing to do
  77.                 }
  78.             }
  79.         }
  80.         throw new AccessDeniedHttpException('Access denied.');
  81.     }
  82. }