Upload.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml\File\Customer;
  7. use Magento\Backend\App\Action;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Customer\Api\CustomerMetadataInterface;
  10. use Magento\Customer\Model\FileUploader;
  11. use Magento\Customer\Model\FileUploaderFactory;
  12. use Magento\Framework\Controller\ResultFactory;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Psr\Log\LoggerInterface;
  15. class Upload extends Action
  16. {
  17. /**
  18. * Authorization level of a basic admin session
  19. *
  20. * @see _isAllowed()
  21. */
  22. const ADMIN_RESOURCE = 'Magento_Customer::manage';
  23. /**
  24. * @var FileUploaderFactory
  25. */
  26. private $fileUploaderFactory;
  27. /**
  28. * @var CustomerMetadataInterface
  29. */
  30. private $customerMetadataService;
  31. /**
  32. * @var LoggerInterface
  33. */
  34. private $logger;
  35. /**
  36. * @param Context $context
  37. * @param FileUploaderFactory $fileUploaderFactory
  38. * @param CustomerMetadataInterface $customerMetadataService
  39. * @param LoggerInterface $logger
  40. */
  41. public function __construct(
  42. Context $context,
  43. FileUploaderFactory $fileUploaderFactory,
  44. CustomerMetadataInterface $customerMetadataService,
  45. LoggerInterface $logger
  46. ) {
  47. $this->fileUploaderFactory = $fileUploaderFactory;
  48. $this->customerMetadataService = $customerMetadataService;
  49. $this->logger = $logger;
  50. parent::__construct($context);
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. public function execute()
  56. {
  57. try {
  58. if (empty($_FILES)) {
  59. throw new \Exception('$_FILES array is empty.');
  60. }
  61. $attributeCode = key($_FILES['customer']['name']);
  62. $attributeMetadata = $this->customerMetadataService->getAttributeMetadata($attributeCode);
  63. /** @var FileUploader $fileUploader */
  64. $fileUploader = $this->fileUploaderFactory->create([
  65. 'attributeMetadata' => $attributeMetadata,
  66. 'entityTypeCode' => CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
  67. 'scope' => 'customer',
  68. ]);
  69. $errors = $fileUploader->validate();
  70. if (true !== $errors) {
  71. $errorMessage = implode('</br>', $errors);
  72. throw new LocalizedException(__($errorMessage));
  73. }
  74. $result = $fileUploader->upload();
  75. } catch (LocalizedException $e) {
  76. $result = [
  77. 'error' => $e->getMessage(),
  78. 'errorcode' => $e->getCode(),
  79. ];
  80. } catch (\Exception $e) {
  81. $this->logger->critical($e);
  82. $result = [
  83. 'error' => __('Something went wrong while saving file.'),
  84. 'errorcode' => $e->getCode(),
  85. ];
  86. }
  87. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  88. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  89. $resultJson->setData($result);
  90. return $resultJson;
  91. }
  92. }