FileUploaderDataResolver.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Model;
  8. use Magento\Eav\Model\Entity\Type;
  9. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  10. use Magento\Customer\Api\AddressMetadataInterface;
  11. use Magento\Customer\Api\CustomerMetadataInterface;
  12. /**
  13. * Class to retrieve file uploader data for customer and customer address file & image attributes
  14. */
  15. class FileUploaderDataResolver
  16. {
  17. /**
  18. * Maximum file size allowed for file_uploader UI component
  19. * This constant was copied from deprecated data provider \Magento\Customer\Model\Customer\DataProvider
  20. */
  21. private const MAX_FILE_SIZE = 2097152;
  22. /**
  23. * @var FileProcessorFactory
  24. */
  25. private $fileProcessorFactory;
  26. /**
  27. * File types allowed for file_uploader UI component
  28. *
  29. * @var array
  30. */
  31. private $fileUploaderTypes = [
  32. 'image',
  33. 'file',
  34. ];
  35. /**
  36. * @param FileProcessorFactory $fileProcessorFactory
  37. */
  38. public function __construct(
  39. FileProcessorFactory $fileProcessorFactory
  40. ) {
  41. $this->fileProcessorFactory = $fileProcessorFactory;
  42. }
  43. /**
  44. * Override file uploader UI component data
  45. *
  46. * Overrides data for attributes with frontend_input equal to 'image' or 'file'.
  47. *
  48. * @param Customer|Address $entity
  49. * @param array $entityData
  50. * @return void
  51. */
  52. public function overrideFileUploaderData($entity, array &$entityData): void
  53. {
  54. $attributes = $entity->getAttributes();
  55. foreach ($attributes as $attribute) {
  56. /** @var Attribute $attribute */
  57. if (\in_array($attribute->getFrontendInput(), $this->fileUploaderTypes, true)) {
  58. $entityData[$attribute->getAttributeCode()] = $this->getFileUploaderData(
  59. $entity->getEntityType(),
  60. $attribute,
  61. $entityData
  62. );
  63. }
  64. }
  65. }
  66. /**
  67. * Retrieve array of values required by file uploader UI component
  68. *
  69. * @param Type $entityType
  70. * @param Attribute $attribute
  71. * @param array $customerData
  72. * @return array
  73. */
  74. private function getFileUploaderData(
  75. Type $entityType,
  76. Attribute $attribute,
  77. array $customerData
  78. ): array {
  79. $attributeCode = $attribute->getAttributeCode();
  80. $file = $customerData[$attributeCode] ?? null;
  81. /** @var FileProcessor $fileProcessor */
  82. $fileProcessor = $this->fileProcessorFactory->create([
  83. 'entityTypeCode' => $entityType->getEntityTypeCode(),
  84. ]);
  85. if (!empty($file)
  86. && $fileProcessor->isExist($file)
  87. ) {
  88. $stat = $fileProcessor->getStat($file);
  89. $viewUrl = $fileProcessor->getViewUrl($file, $attribute->getFrontendInput());
  90. return [
  91. [
  92. 'file' => $file,
  93. 'size' => null !== $stat ? $stat['size'] : 0,
  94. 'url' => $viewUrl ?? '',
  95. 'name' => basename($file),
  96. 'type' => $fileProcessor->getMimeType($file),
  97. ],
  98. ];
  99. }
  100. return [];
  101. }
  102. /**
  103. * Override file uploader UI component metadata
  104. *
  105. * Overrides metadata for attributes with frontend_input equal to 'image' or 'file'.
  106. *
  107. * @param Type $entityType
  108. * @param AbstractAttribute $attribute
  109. * @param array $config
  110. * @return void
  111. */
  112. public function overrideFileUploaderMetadata(
  113. Type $entityType,
  114. AbstractAttribute $attribute,
  115. array &$config
  116. ): void {
  117. if (\in_array($attribute->getFrontendInput(), $this->fileUploaderTypes, true)) {
  118. $maxFileSize = self::MAX_FILE_SIZE;
  119. if (isset($config['validation']['max_file_size'])) {
  120. $maxFileSize = (int)$config['validation']['max_file_size'];
  121. }
  122. $allowedExtensions = [];
  123. if (isset($config['validation']['file_extensions'])) {
  124. $allowedExtensions = explode(',', $config['validation']['file_extensions']);
  125. array_walk($allowedExtensions, function (&$value) {
  126. $value = strtolower(trim($value));
  127. });
  128. }
  129. $allowedExtensions = implode(' ', $allowedExtensions);
  130. $entityTypeCode = $entityType->getEntityTypeCode();
  131. $url = $this->getFileUploadUrl($entityTypeCode);
  132. $config = [
  133. 'formElement' => 'fileUploader',
  134. 'componentType' => 'fileUploader',
  135. 'maxFileSize' => $maxFileSize,
  136. 'allowedExtensions' => $allowedExtensions,
  137. 'uploaderConfig' => [
  138. 'url' => $url,
  139. ],
  140. 'label' => $this->getMetadataValue($config, 'label'),
  141. 'sortOrder' => $this->getMetadataValue($config, 'sortOrder'),
  142. 'required' => $this->getMetadataValue($config, 'required'),
  143. 'visible' => $this->getMetadataValue($config, 'visible'),
  144. 'validation' => $this->getMetadataValue($config, 'validation'),
  145. ];
  146. }
  147. }
  148. /**
  149. * Retrieve metadata value
  150. *
  151. * @param array $config
  152. * @param string $name
  153. * @param mixed $default
  154. * @return mixed
  155. */
  156. private function getMetadataValue($config, $name, $default = null)
  157. {
  158. return $config[$name] ?? $default;
  159. }
  160. /**
  161. * Retrieve URL to file upload
  162. *
  163. * @param string $entityTypeCode
  164. * @return string
  165. */
  166. private function getFileUploadUrl($entityTypeCode): string
  167. {
  168. switch ($entityTypeCode) {
  169. case CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER:
  170. $url = 'customer/file/customer_upload';
  171. break;
  172. case AddressMetadataInterface::ENTITY_TYPE_ADDRESS:
  173. $url = 'customer/file/address_upload';
  174. break;
  175. default:
  176. $url = '';
  177. break;
  178. }
  179. return $url;
  180. }
  181. }