FileUploader.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\CustomerMetadataInterface;
  9. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  10. use Magento\Customer\Model\FileProcessorFactory;
  11. use Magento\Customer\Model\Metadata\ElementFactory;
  12. use Magento\Framework\Exception\LocalizedException;
  13. class FileUploader
  14. {
  15. /**
  16. * @var CustomerMetadataInterface
  17. */
  18. private $customerMetadataService;
  19. /**
  20. * @var AddressMetadataInterface
  21. */
  22. private $addressMetadataService;
  23. /**
  24. * @var ElementFactory
  25. */
  26. private $elementFactory;
  27. /**
  28. * @var FileProcessorFactory
  29. */
  30. private $fileProcessorFactory;
  31. /**
  32. * @var AttributeMetadataInterface
  33. */
  34. private $attributeMetadata;
  35. /**
  36. * @var string
  37. */
  38. private $entityTypeCode;
  39. /**
  40. * @var string
  41. */
  42. private $scope;
  43. /**
  44. * @param CustomerMetadataInterface $customerMetadataService
  45. * @param AddressMetadataInterface $addressMetadataService
  46. * @param ElementFactory $elementFactory
  47. * @param FileProcessorFactory $fileProcessorFactory
  48. * @param AttributeMetadataInterface $attributeMetadata
  49. * @param string $entityTypeCode
  50. * @param string $scope
  51. */
  52. public function __construct(
  53. CustomerMetadataInterface $customerMetadataService,
  54. AddressMetadataInterface $addressMetadataService,
  55. ElementFactory $elementFactory,
  56. FileProcessorFactory $fileProcessorFactory,
  57. AttributeMetadataInterface $attributeMetadata,
  58. $entityTypeCode,
  59. $scope
  60. ) {
  61. $this->customerMetadataService = $customerMetadataService;
  62. $this->addressMetadataService = $addressMetadataService;
  63. $this->elementFactory = $elementFactory;
  64. $this->fileProcessorFactory = $fileProcessorFactory;
  65. $this->attributeMetadata = $attributeMetadata;
  66. $this->entityTypeCode = $entityTypeCode;
  67. $this->scope = $scope;
  68. }
  69. /**
  70. * Validate uploaded file
  71. *
  72. * @return array|bool
  73. */
  74. public function validate()
  75. {
  76. $formElement = $this->elementFactory->create(
  77. $this->attributeMetadata,
  78. null,
  79. $this->entityTypeCode
  80. );
  81. $errors = $formElement->validateValue($this->getData());
  82. return $errors;
  83. }
  84. /**
  85. * Execute file uploading
  86. *
  87. * @return \string[]
  88. * @throws LocalizedException
  89. */
  90. public function upload()
  91. {
  92. /** @var FileProcessor $fileProcessor */
  93. $fileProcessor = $this->fileProcessorFactory->create([
  94. 'entityTypeCode' => $this->entityTypeCode,
  95. 'allowedExtensions' => $this->getAllowedExtensions(),
  96. ]);
  97. $result = $fileProcessor->saveTemporaryFile($this->scope . '[' . $this->getAttributeCode() . ']');
  98. // Update tmp_name param. Required for attribute validation!
  99. $result['tmp_name'] = ltrim($result['file'], '/');
  100. $result['url'] = $fileProcessor->getViewUrl(
  101. FileProcessor::TMP_DIR . '/' . ltrim($result['name'], '/'),
  102. $this->attributeMetadata->getFrontendInput()
  103. );
  104. return $result;
  105. }
  106. /**
  107. * Get attribute code
  108. *
  109. * @return string
  110. */
  111. private function getAttributeCode()
  112. {
  113. return key($_FILES[$this->scope]['name']);
  114. }
  115. /**
  116. * Retrieve data from global $_FILES array
  117. *
  118. * @return array
  119. */
  120. private function getData()
  121. {
  122. $data = [];
  123. $fileAttributes = $_FILES[$this->scope];
  124. foreach ($fileAttributes as $attributeName => $attributeValue) {
  125. $data[$attributeName] = $attributeValue[$this->getAttributeCode()];
  126. }
  127. return $data;
  128. }
  129. /**
  130. * Get allowed extensions
  131. *
  132. * @return array
  133. */
  134. private function getAllowedExtensions()
  135. {
  136. $allowedExtensions = [];
  137. $validationRules = $this->attributeMetadata->getValidationRules();
  138. foreach ($validationRules as $validationRule) {
  139. if ($validationRule->getName() == 'file_extensions') {
  140. $allowedExtensions = explode(',', $validationRule->getValue());
  141. array_walk($allowedExtensions, function (&$value) {
  142. $value = strtolower(trim($value));
  143. });
  144. break;
  145. }
  146. }
  147. return $allowedExtensions;
  148. }
  149. }