DataUriNormalizer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\Mime\MimeTypeGuesserInterface;
  13. use Symfony\Component\Mime\MimeTypes;
  14. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  15. use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
  16. /**
  17. * Normalizes an {@see \SplFileInfo} object to a data URI.
  18. * Denormalizes a data URI to a {@see \SplFileObject} object.
  19. *
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. */
  22. class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
  23. {
  24. private const SUPPORTED_TYPES = [
  25. \SplFileInfo::class => true,
  26. \SplFileObject::class => true,
  27. File::class => true,
  28. ];
  29. /**
  30. * @var MimeTypeGuesserInterface|null
  31. */
  32. private $mimeTypeGuesser;
  33. public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
  34. {
  35. if (!$mimeTypeGuesser && class_exists(MimeTypes::class)) {
  36. $mimeTypeGuesser = MimeTypes::getDefault();
  37. }
  38. $this->mimeTypeGuesser = $mimeTypeGuesser;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @return string
  44. */
  45. public function normalize($object, string $format = null, array $context = [])
  46. {
  47. if (!$object instanceof \SplFileInfo) {
  48. throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
  49. }
  50. $mimeType = $this->getMimeType($object);
  51. $splFileObject = $this->extractSplFileObject($object);
  52. $data = '';
  53. $splFileObject->rewind();
  54. while (!$splFileObject->eof()) {
  55. $data .= $splFileObject->fgets();
  56. }
  57. if ('text' === explode('/', $mimeType, 2)[0]) {
  58. return sprintf('data:%s,%s', $mimeType, rawurlencode($data));
  59. }
  60. return sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function supportsNormalization($data, string $format = null)
  66. {
  67. return $data instanceof \SplFileInfo;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. *
  72. * Regex adapted from Brian Grinstead code.
  73. *
  74. * @see https://gist.github.com/bgrins/6194623
  75. *
  76. * @throws InvalidArgumentException
  77. * @throws NotNormalizableValueException
  78. *
  79. * @return \SplFileInfo
  80. */
  81. public function denormalize($data, string $type, string $format = null, array $context = [])
  82. {
  83. if (!preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i', $data)) {
  84. throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
  85. }
  86. try {
  87. switch ($type) {
  88. case 'Symfony\Component\HttpFoundation\File\File':
  89. if (!class_exists(File::class)) {
  90. throw new InvalidArgumentException(sprintf('Cannot denormalize to a "%s" without the HttpFoundation component installed. Try running "composer require symfony/http-foundation".', File::class));
  91. }
  92. return new File($data, false);
  93. case 'SplFileObject':
  94. case 'SplFileInfo':
  95. return new \SplFileObject($data);
  96. }
  97. } catch (\RuntimeException $exception) {
  98. throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
  99. }
  100. throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $type));
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function supportsDenormalization($data, string $type, string $format = null)
  106. {
  107. return isset(self::SUPPORTED_TYPES[$type]);
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function hasCacheableSupportsMethod(): bool
  113. {
  114. return __CLASS__ === static::class;
  115. }
  116. /**
  117. * Gets the mime type of the object. Defaults to application/octet-stream.
  118. */
  119. private function getMimeType(\SplFileInfo $object): string
  120. {
  121. if ($object instanceof File) {
  122. return $object->getMimeType();
  123. }
  124. if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guessMimeType($object->getPathname())) {
  125. return $mimeType;
  126. }
  127. return 'application/octet-stream';
  128. }
  129. /**
  130. * Returns the \SplFileObject instance associated with the given \SplFileInfo instance.
  131. */
  132. private function extractSplFileObject(\SplFileInfo $object): \SplFileObject
  133. {
  134. if ($object instanceof \SplFileObject) {
  135. return $object;
  136. }
  137. return $object->openFile();
  138. }
  139. }