DataUriNormalizer.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\HttpFoundation\File\MimeType\MimeTypeGuesser;
  13. use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
  14. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  15. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  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
  23. {
  24. /**
  25. * @var MimeTypeGuesserInterface
  26. */
  27. private $mimeTypeGuesser;
  28. public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
  29. {
  30. if (null === $mimeTypeGuesser && class_exists('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser')) {
  31. $mimeTypeGuesser = MimeTypeGuesser::getInstance();
  32. }
  33. $this->mimeTypeGuesser = $mimeTypeGuesser;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function normalize($object, $format = null, array $context = array())
  39. {
  40. if (!$object instanceof \SplFileInfo) {
  41. throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
  42. }
  43. $mimeType = $this->getMimeType($object);
  44. $splFileObject = $this->extractSplFileObject($object);
  45. $data = '';
  46. $splFileObject->rewind();
  47. while (!$splFileObject->eof()) {
  48. $data .= $splFileObject->fgets();
  49. }
  50. if ('text' === explode('/', $mimeType, 2)[0]) {
  51. return sprintf('data:%s,%s', $mimeType, rawurlencode($data));
  52. }
  53. return sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function supportsNormalization($data, $format = null)
  59. {
  60. return $data instanceof \SplFileInfo;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. *
  65. * Regex adapted from Brian Grinstead code.
  66. *
  67. * @see https://gist.github.com/bgrins/6194623
  68. *
  69. * @throws InvalidArgumentException
  70. * @throws UnexpectedValueException
  71. */
  72. public function denormalize($data, $class, $format = null, array $context = array())
  73. {
  74. 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)) {
  75. throw new UnexpectedValueException('The provided "data:" URI is not valid.');
  76. }
  77. try {
  78. switch ($class) {
  79. case 'Symfony\Component\HttpFoundation\File\File':
  80. return new File($data, false);
  81. case 'SplFileObject':
  82. case 'SplFileInfo':
  83. return new \SplFileObject($data);
  84. }
  85. } catch (\RuntimeException $exception) {
  86. throw new UnexpectedValueException($exception->getMessage(), $exception->getCode(), $exception);
  87. }
  88. throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $class));
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function supportsDenormalization($data, $type, $format = null)
  94. {
  95. $supportedTypes = array(
  96. \SplFileInfo::class => true,
  97. \SplFileObject::class => true,
  98. 'Symfony\Component\HttpFoundation\File\File' => true,
  99. );
  100. return isset($supportedTypes[$type]);
  101. }
  102. /**
  103. * Gets the mime type of the object. Defaults to application/octet-stream.
  104. *
  105. * @param \SplFileInfo $object
  106. *
  107. * @return string
  108. */
  109. private function getMimeType(\SplFileInfo $object)
  110. {
  111. if ($object instanceof File) {
  112. return $object->getMimeType();
  113. }
  114. if ($this->mimeTypeGuesser && $mimeType = $this->mimeTypeGuesser->guess($object->getPathname())) {
  115. return $mimeType;
  116. }
  117. return 'application/octet-stream';
  118. }
  119. /**
  120. * Returns the \SplFileObject instance associated with the given \SplFileInfo instance.
  121. *
  122. * @param \SplFileInfo $object
  123. *
  124. * @return \SplFileObject
  125. */
  126. private function extractSplFileObject(\SplFileInfo $object)
  127. {
  128. if ($object instanceof \SplFileObject) {
  129. return $object;
  130. }
  131. return $object->openFile();
  132. }
  133. }