fileUploaderFactory = $fileUploaderFactory; $this->addressMetadataService = $addressMetadataService; $this->logger = $logger; $this->fileProcessorFactory = $fileProcessorFactory; parent::__construct($context); } /** * @inheritDoc */ public function execute() { try { $requestedFiles = $this->getRequest()->getFiles('custom_attributes'); if (empty($requestedFiles)) { $result = $this->processError(__('No files for upload.')); } else { $attributeCode = key($requestedFiles); $attributeMetadata = $this->addressMetadataService->getAttributeMetadata($attributeCode); /** @var FileUploader $fileUploader */ $fileUploader = $this->fileUploaderFactory->create([ 'attributeMetadata' => $attributeMetadata, 'entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'scope' => CustomAttributesDataInterface::CUSTOM_ATTRIBUTES, ]); $errors = $fileUploader->validate(); if (true !== $errors) { $errorMessage = implode('
', $errors); $result = $this->processError(($errorMessage)); } else { $result = $fileUploader->upload(); $this->moveTmpFileToSuitableFolder($result); } } } catch (LocalizedException $e) { $result = $this->processError($e->getMessage(), $e->getCode()); } catch (\Exception $e) { $this->logger->critical($e); $result = $this->processError($e->getMessage(), $e->getCode()); } /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); $resultJson->setData($result); return $resultJson; } /** * Move file from temporary folder to the 'customer_address' media folder * * @param array $fileInfo * @throws LocalizedException */ private function moveTmpFileToSuitableFolder(&$fileInfo) { $fileName = $fileInfo['file']; $fileProcessor = $this->fileProcessorFactory ->create(['entityTypeCode' => AddressMetadataInterface::ENTITY_TYPE_ADDRESS]); $newFilePath = $fileProcessor->moveTemporaryFile($fileName); $fileInfo['file'] = $newFilePath; $fileInfo['url'] = $fileProcessor->getViewUrl( $newFilePath, 'file' ); } /** * Prepare result array for errors * * @param string $message * @param int $code * @return array */ private function processError($message, $code = 0) { $result = [ 'error' => $message, 'errorcode' => $code, ]; return $result; } }