src/EventListener/S3Listener.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\Event\AssetEvents;
  4. use Pimcore\Event\FrontendEvents;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\EventDispatcher\GenericEvent;
  7. class S3Listener implements EventSubscriberInterface
  8. {
  9.     private $s3BaseUrl;
  10.     private $s3TmpUrlPrefix;
  11.     private $s3AssetUrlPrefix;
  12.     public function __construct()
  13.     {
  14.         // you have to customize this if you'd like to deliver your assets/thumbnails in your S3 bucket by CloudFront
  15.         $this->s3BaseUrl S3_ASSET_URL_PIMCORE;
  16.         $this->s3TmpUrlPrefix $this->s3BaseUrl str_replace('s3:/'''PIMCORE_TEMPORARY_DIRECTORY);
  17.         $this->s3AssetUrlPrefix $this->s3BaseUrl str_replace('s3:/'''PIMCORE_ASSET_DIRECTORY);
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             FrontendEvents::ASSET_IMAGE_THUMBNAIL => 'onFrontendPathThumbnail',
  23.             FrontendEvents::ASSET_DOCUMENT_IMAGE_THUMBNAIL => 'onFrontendPathThumbnail',
  24.             FrontendEvents::ASSET_VIDEO_IMAGE_THUMBNAIL => 'onFrontendPathThumbnail',
  25.             FrontendEvents::ASSET_VIDEO_THUMBNAIL => 'onFrontendPathThumbnail',
  26.             FrontendEvents::ASSET_PATH => 'onFrontEndPathAsset',
  27.             AssetEvents::IMAGE_THUMBNAIL => 'onAssetThumbnailCreated',
  28.             AssetEvents::VIDEO_IMAGE_THUMBNAIL => 'onAssetThumbnailCreated',
  29.             AssetEvents::DOCUMENT_IMAGE_THUMBNAIL => 'onAssetThumbnailCreated',
  30.         ];
  31.     }
  32.     public function onFrontendPathThumbnail(GenericEvent $event)
  33.     {
  34.         // rewrite the path for the frontend
  35.         $fileSystemPath $event->getSubject()->getFileSystemPath();
  36.         $cacheKey 'thumb_s3_' md5($fileSystemPath);
  37.         $path \Pimcore\Cache::load($cacheKey);
  38.         if (!$path) {
  39.             if (!file_exists($fileSystemPath)) {
  40.                 // the thumbnail doesn't exist yet, so we need to create it on request -> Thumbnail controller plugin
  41.                 $path str_replace(PIMCORE_TEMPORARY_DIRECTORY.'/image-thumbnails'''$fileSystemPath);
  42.             } else {
  43.                 $path str_replace(PIMCORE_TEMPORARY_DIRECTORY '/'$this->s3TmpUrlPrefix '/'$fileSystemPath);
  44.             }
  45.         }
  46.         $event->setArgument('frontendPath'$path);
  47.     }
  48.     public function onAssetThumbnailCreated(GenericEvent $event)
  49.     {
  50.         $thumbnail $event->getSubject();
  51.         $fsPath $thumbnail->getFileSystemPath();
  52.         if ($fsPath && $event->getArgument('generated')) {
  53.             $cacheKey 'thumb_s3_' md5($fsPath);
  54.             \Pimcore\Cache::remove($cacheKey);
  55.         }
  56.     }
  57.     public function onFrontEndPathAsset(GenericEvent $event)
  58.     {
  59.         $asset $event->getSubject();
  60.         $path str_replace(PIMCORE_ASSET_DIRECTORY '/'$this->s3AssetUrlPrefix '/'$asset->getFrontendPath());
  61.         $event->setArgument('frontendPath'$path);
  62.     }
  63. }