src/EventSubscriber/Channel/ImageUploaderSubscriber.php line 74

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the adrec-platform package.
  4. *
  5. * (c) Benjamin Georgeault <https://www.pressop.eu>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace App\EventSubscriber\Channel;
  11. use App\Entity\Channel\Channel;
  12. use App\Entity\Channel\Image;
  13. use App\Entity\Account\User;
  14. use App\Enum\ChannelUserResourcePermission\ChannelUserAsyncPermissionEnum;
  15. use App\Repository\Channel\ChannelRepository;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Oneup\UploaderBundle\Event\PostPersistEvent;
  18. use Oneup\UploaderBundle\Event\PreUploadEvent;
  19. use Oneup\UploaderBundle\UploadEvents;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  22. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  23. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  24. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  25. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  26. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  27. /**
  28. * Class ImageUploaderSubscriber
  29. *
  30. * @author Benjamin Georgeault
  31. */
  32. class ImageUploaderSubscriber implements EventSubscriberInterface
  33. {
  34. /**
  35. * @var EntityManagerInterface
  36. */
  37. private $em;
  38. /**
  39. * @var TokenStorageInterface
  40. */
  41. private $token;
  42. /**
  43. * @var AuthorizationCheckerInterface
  44. */
  45. private $authorizationChecker;
  46. public function __construct(EntityManagerInterface $em, TokenStorageInterface $token, AuthorizationCheckerInterface $authorizationChecker)
  47. {
  48. $this->em = $em;
  49. $this->token = $token;
  50. $this->authorizationChecker = $authorizationChecker;
  51. }
  52. /**
  53. * @return string[]
  54. */
  55. public static function getSubscribedEvents(): array
  56. {
  57. return [
  58. UploadEvents::PRE_UPLOAD.'.channel_images' => 'onPreUpload',
  59. UploadEvents::POST_PERSIST.'.channel_images' => 'onFinishedUpload',
  60. ];
  61. }
  62. /**
  63. * @param PreUploadEvent $event
  64. */
  65. public function onPreUpload(PreUploadEvent $event)
  66. {
  67. if (null === $this->getUser()) {
  68. throw new AccessDeniedHttpException();
  69. }
  70. }
  71. /**
  72. * @param PostPersistEvent $event
  73. */
  74. public function onFinishedUpload(PostPersistEvent $event)
  75. {
  76. /** @var \Oneup\UploaderBundle\Uploader\File\FileInterface $file */
  77. $file = $event->getFile();
  78. $request = $event->getRequest();
  79. if (null === $user = $this->getUser()) {
  80. throw new AccessDeniedHttpException();
  81. }
  82. if (!$request->headers->has('x-channel-id')) {
  83. throw new BadRequestHttpException();
  84. }
  85. /** @var ChannelRepository $channelRepo */
  86. $channelRepo = $this->em->getRepository(Channel::class);
  87. if (null === $channel = $channelRepo->find($request->headers->get('x-channel-id'))) {
  88. throw new NotFoundHttpException();
  89. }
  90. if (!$this->authorizationChecker->isGranted(ChannelUserAsyncPermissionEnum::CHANNEL_USER_PERM_ASYNC_UPLOAD_FILES, $channel)) {
  91. throw new AccessDeniedHttpException();
  92. }
  93. $image = (new Image())
  94. ->setName(pathinfo($file->getBasename(), PATHINFO_FILENAME))
  95. ->setExtension($file->getExtension())
  96. ->setTitle(substr($request->request->get('qqfilename', 'IMG'), 0, 63))
  97. ->setOwnerChannel($channel)
  98. ;
  99. $this->em->persist($image);
  100. $this->em->flush();
  101. }
  102. /**
  103. * @return User|null
  104. */
  105. private function getUser(): ?User
  106. {
  107. if (null === $token = $this->token->getToken()) {
  108. return null;
  109. }
  110. if (null === $user = $token->getUser()) {
  111. return null;
  112. }
  113. if (!($user instanceof User)) {
  114. throw new ServiceUnavailableHttpException();
  115. }
  116. return $user;
  117. }
  118. }