src/EventListener/AssetEventListener.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Aws\S3\S3Client;
  4. use Pimcore\Event\AssetEvents;
  5. use Pimcore\Event\Model\AssetEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class AssetEventListener implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var S3Client
  11.      */
  12.     private $s3Client;
  13.     /**
  14.      * @var string
  15.      */
  16.     private $s3TmpUrlPrefix;
  17.     private $s3BaseUrl;
  18.     /**
  19.      * @var string
  20.      */
  21.     private $s3AssetUrlPrefix;
  22.     /**
  23.      * @var Pimcore\Model\Asset
  24.      */
  25.     private $asset;
  26.     public function __construct()
  27.     {
  28.         $this->s3BaseUrl S3_ASSET_URL_PIMCORE;
  29.         $this->s3TmpUrlPrefix $this->s3BaseUrl str_replace('s3:/'''PIMCORE_TEMPORARY_DIRECTORY);
  30.         $this->s3AssetUrlPrefix $this->s3BaseUrl str_replace('s3:/'''PIMCORE_ASSET_DIRECTORY);
  31.         $this->s3Client $this->getS3Client();
  32.     }
  33.     /**
  34.      * @inheritDoc
  35.      */
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.         AssetEvents::POST_ADD => 'onPostAdd',
  40.         AssetEvents::POST_UPDATE => 'onPostUpdate',
  41.     ];
  42.     }
  43.     /**
  44.      * Create a new s3 client object
  45.      *
  46.      * @return S3Client
  47.      */
  48.     protected function getS3Client()
  49.     {
  50.         $s3ConfigSettings = [
  51.             'version' => 'latest',
  52.             'region' => AMAZON_S3_REGION,
  53.             'credentials' => [
  54.                 'key' => AMAZON_S3_KEY,
  55.                 'secret' => AMAZON_S3_SECRET
  56.             ],
  57.         ];
  58.         return new S3Client($s3ConfigSettings);
  59.     }
  60.     public function onPostAdd(AssetEvent $event)
  61.     {
  62.         $this->asset $event->getAsset();
  63.         if ($this->asset->getType() == 'folder') {
  64.             return false;
  65.         }
  66.         $key $this->asset->getRealFullPath();
  67.         $this->makeAssetPublicInS3('assets'.$key);
  68.     }
  69.     public function onPostUpdate(AssetEvent $event)
  70.     {
  71.         $this->asset $event->getAsset();
  72.         if ($this->asset->getType() == 'folder') {
  73.             return false;
  74.         }
  75.         $key $this->asset->getRealFullPath();
  76.         $this->makeAssetPublicInS3('assets'.$key);
  77.     }
  78.     /**
  79.      * Make s3 bucket asset publicly available
  80.      *
  81.      * @param $assetPath
  82.      * @param bool $isTmp
  83.      */
  84.     protected function makeAssetPublicInS3($assetPath$isTmp false)
  85.     {
  86.         $this->s3Client->putObjectAcl([
  87.             'ACL' => 'public-read-write',
  88.             'Grants' => [
  89.                 [
  90.                     'Grantee' => [
  91.                         'DisplayName' => 'public-read-write',
  92.                         'EmailAddress' => 'fyzalam@gmail.com',
  93.                         'Type' => 'CanonicalUser'],
  94.                         'Permission' => 'READ',
  95.                 ],
  96.             ],
  97.             'Owner' => ['DisplayName' => AMAZON_S3_BUCKET, ],
  98.             'Bucket' => AMAZON_S3_BUCKET,
  99.             'Key' => $assetPath,
  100.             'RequestPayer' => AMAZON_S3_BUCKET,
  101.         ]);
  102.         if ($isTmp === false) {
  103.             $this->asset->update(true);
  104.         }
  105.     }
  106. }