vendor/ibexa/image-editor/src/bundle/Event/Base64ResponseSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ImageEditor\Event;
  8. use Ibexa\Bundle\ImageEditor\Controller\Base64Controller;
  9. use Ibexa\Core\FieldType\Image\Value;
  10. use Ibexa\Core\IO\IOServiceInterface;
  11. use Ibexa\Core\IO\Values\BinaryFile;
  12. use Ibexa\ImageEditor\Output\Base64File;
  13. use Ibexa\ImageEditor\Response\Base64Response;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  18. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  19. use Symfony\Component\HttpKernel\Event\ViewEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. final class Base64ResponseSubscriber implements EventSubscriberInterface
  22. {
  23. private IOServiceInterface $ioService;
  24. public function __construct(
  25. IOServiceInterface $ioService
  26. ) {
  27. $this->ioService = $ioService;
  28. }
  29. public static function getSubscribedEvents(): array
  30. {
  31. return [
  32. KernelEvents::VIEW => 'onView',
  33. KernelEvents::CONTROLLER => 'onKernelController',
  34. ];
  35. }
  36. public function onKernelController(ControllerEvent $event): void
  37. {
  38. $controller = $event->getController();
  39. if (is_array($controller) && $controller[0] instanceof Base64Controller) {
  40. $event->getRequest()->attributes->set('base_64_required', true);
  41. }
  42. }
  43. public function onView(ViewEvent $event): void
  44. {
  45. $value = $event->getControllerResult();
  46. if ($event->getRequest()->attributes->get('base_64_required') !== true) {
  47. return;
  48. }
  49. if (!$value instanceof Value) {
  50. return;
  51. }
  52. $file = $this->ioService->loadBinaryFile($value->id);
  53. $requestedType = $event->getRequest()->headers->get('Accept', '');
  54. $response = new Response();
  55. if ($requestedType === '' || $requestedType === 'application/octet-stream') {
  56. $response = new Base64Response($file, $this->ioService);
  57. $response->setContentDisposition(
  58. ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  59. (string) $value->fileName
  60. );
  61. }
  62. if ($requestedType === 'application/json') {
  63. $response = new JsonResponse(
  64. new Base64File(
  65. $this->buildBase64($file),
  66. (string) $value->fileName
  67. )
  68. );
  69. }
  70. $event->setResponse($response);
  71. }
  72. private function buildBase64(BinaryFile $binaryFile): string
  73. {
  74. return sprintf(
  75. 'data:%s;base64,%s',
  76. $this->ioService->getMimeType($binaryFile->id),
  77. base64_encode($this->ioService->getFileContents($binaryFile))
  78. );
  79. }
  80. }
  81. class_alias(Base64ResponseSubscriber::class, 'Ibexa\Platform\Bundle\ImageEditor\Event\Base64ResponseSubscriber');