MediaIdentifier.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php declare(strict_types=1);
  2. namespace Grav\Framework\Media;
  3. use Grav\Common\Grav;
  4. use Grav\Common\User\Interfaces\UserInterface;
  5. use Grav\Framework\Contracts\Media\MediaObjectInterface;
  6. use Grav\Framework\Flex\Flex;
  7. use Grav\Framework\Flex\FlexFormFlash;
  8. use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
  9. use Grav\Framework\Object\Identifiers\Identifier;
  10. /**
  11. * Interface IdentifierInterface
  12. *
  13. * @template T of MediaObjectInterface
  14. * @extends Identifier<T>
  15. */
  16. class MediaIdentifier extends Identifier
  17. {
  18. /** @var MediaObjectInterface|null */
  19. private $object = null;
  20. /**
  21. * @param MediaObjectInterface $object
  22. * @return MediaIdentifier<T>
  23. */
  24. public static function createFromObject(MediaObjectInterface $object): MediaIdentifier
  25. {
  26. $instance = new static($object->getId());
  27. $instance->setObject($object);
  28. return $instance;
  29. }
  30. /**
  31. * @param string $id
  32. */
  33. public function __construct(string $id)
  34. {
  35. parent::__construct($id, 'media');
  36. }
  37. /**
  38. * @return T
  39. */
  40. public function getObject(): ?MediaObjectInterface
  41. {
  42. if (!isset($this->object)) {
  43. $type = $this->getType();
  44. $id = $this->getId();
  45. $parts = explode('/', $id);
  46. if ($type === 'media' && str_starts_with($id, 'uploads/')) {
  47. array_shift($parts);
  48. [, $folder, $uniqueId, $field, $filename] = $this->findFlash($parts);
  49. $flash = $this->getFlash($folder, $uniqueId);
  50. if ($flash->exists()) {
  51. $uploadedFile = $flash->getFilesByField($field)[$filename] ?? null;
  52. $this->object = UploadedMediaObject::createFromFlash($flash, $field, $filename, $uploadedFile);
  53. }
  54. } else {
  55. $type = array_shift($parts);
  56. $key = array_shift($parts);
  57. $field = array_shift($parts);
  58. $filename = implode('/', $parts);
  59. $flexObject = $this->getFlexObject($type, $key);
  60. if ($flexObject && method_exists($flexObject, 'getMediaField') && method_exists($flexObject, 'getMedia')) {
  61. $media = $field !== 'media' ? $flexObject->getMediaField($field) : $flexObject->getMedia();
  62. $image = null;
  63. if ($media) {
  64. $image = $media[$filename];
  65. }
  66. $this->object = new MediaObject($field, $filename, $image, $flexObject);
  67. }
  68. }
  69. if (!isset($this->object)) {
  70. throw new \RuntimeException(sprintf('Object not found for identifier {type: "%s", id: "%s"}', $type, $id));
  71. }
  72. }
  73. return $this->object;
  74. }
  75. /**
  76. * @param T $object
  77. */
  78. public function setObject(MediaObjectInterface $object): void
  79. {
  80. $type = $this->getType();
  81. $objectType = $object->getType();
  82. if ($type !== $objectType) {
  83. throw new \RuntimeException(sprintf('Object has to be type %s, %s given', $type, $objectType));
  84. }
  85. $this->object = $object;
  86. }
  87. protected function findFlash(array $parts): ?array
  88. {
  89. $type = array_shift($parts);
  90. if ($type === 'account') {
  91. /** @var UserInterface|null $user */
  92. $user = Grav::instance()['user'] ?? null;
  93. $folder = $user->getMediaFolder();
  94. } else {
  95. $folder = 'tmp://';
  96. }
  97. if (!$folder) {
  98. return null;
  99. }
  100. do {
  101. $part = array_shift($parts);
  102. $folder .= "/{$part}";
  103. } while (!str_starts_with($part, 'flex-'));
  104. $uniqueId = array_shift($parts);
  105. $field = array_shift($parts);
  106. $filename = implode('/', $parts);
  107. return [$type, $folder, $uniqueId, $field, $filename];
  108. }
  109. protected function getFlash(string $folder, string $uniqueId): FlexFormFlash
  110. {
  111. $config = [
  112. 'unique_id' => $uniqueId,
  113. 'folder' => $folder
  114. ];
  115. return new FlexFormFlash($config);
  116. }
  117. protected function getFlexObject(string $type, string $key): ?FlexObjectInterface
  118. {
  119. /** @var Flex $flex */
  120. $flex = Grav::instance()['flex'];
  121. return $flex->getObject($key, $type);
  122. }
  123. }