src/Base/EventSubscriber/AssetSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Base\EventSubscriber;
  4. use App\User\Service\UserService;
  5. use Pimcore\Event\AssetEvents;
  6. use Pimcore\Event\Model\AssetEvent;
  7. use Pimcore\Log\ApplicationLogger;
  8. use Pimcore\Model\Asset\Folder;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Uid\Uuid;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. /**
  15.  * Класс подписчиков на события assets
  16.  */
  17. class AssetSubscriber implements EventSubscriberInterface
  18. {
  19.     public function __construct(
  20.         protected ContainerBagInterface $params,
  21.         protected TranslatorInterface $translator,
  22.         private UserService $userService
  23.     ) {
  24.     }
  25.     /**
  26.      * @return string[]
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             AssetEvents::PRE_ADD => 'onPreAdd'
  32.         ];
  33.     }
  34.     /**
  35.      * @param \Pimcore\Event\Model\AssetEvent $event
  36.      * @throws \Exception
  37.      */
  38.     public function onPreAdd(AssetEvent $event)
  39.     {
  40.         $asset $event->getAsset();
  41.         if ($asset->getType() === 'image') {
  42.             $streamMeta stream_get_meta_data($asset->getStream());
  43.             [$imageWidth$imageHeight] = getimagesize($streamMeta['uri']);
  44.             if ($imageWidth $this->params->get('image.max_upload_width') || $imageHeight $this->params->get(
  45.                     'image.max_upload__height'
  46.                 )) {
  47.                 throw new \Exception($this->translator->trans('Photo size limits exceeded'));
  48.             }
  49.         }
  50.         if (($parent $asset->getParent()) && $parent->getFilename() == '__tmp') {
  51.             $uuid Uuid::v6()->toRfc4122();
  52.             //todo быстрое решение. Подмена пути хранения файла (нужно заменять __tmp)
  53.             $type 'photo';
  54.             if (strpos($asset->getFilename(), '/') !== false) {
  55.                 [$typeFile$arFilename] = explode('/'$asset->getFilename());
  56.                 if ($typeFile && $arFilename) {
  57.                     $asset->setFilename($arFilename);
  58.                     if (in_array($typeFile, ['album''photo''cover''audio'], true)) {
  59.                         $type $typeFile;
  60.                     }
  61.                 }
  62.             }
  63.             $ext substr(strrchr($asset->getFilename(), '.'), 1);
  64.             $baseAssetName $asset->getFilename();
  65.             $asset->setFilename($uuid '.' $ext);
  66.             //логика записи asset photo сразу пользователю
  67.             $user $this->userService->getUserByToken();
  68.             if ($user instanceof UserInterface) {
  69.                 /** @var \Pimcore\Model\User $user */
  70.                 $user $user->getUser();
  71.                 $photoFolder Folder::getByPath('/' $user->getEmail() . '/' $type '/');
  72.                 $asset->setParent($photoFolder);
  73.                 if ($asset->getType() === 'image') {
  74.                     $logger ApplicationLogger::getInstance();
  75.                     $logger->notice(
  76.                         'asset_image:onPreAdd::userEmail:' $user->getEmail() . '_fileName:' $baseAssetName
  77.                     );
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }