imageContentFactory = $imageContentInterfaceFactory ?: ObjectManager::getInstance() ->get(ImageContentInterfaceFactory::class); $this->ioFileSystem = $ioFileSystem ?: ObjectManager::getInstance() ->get(IoFileSystem::class); $this->mediaWriteDirectory = $fileSystem->getDirectoryWrite(DirectoryList::MEDIA); $this->mediaEntityTmpReadDirectory = $fileSystem->getDirectoryReadByPath( $this->mediaWriteDirectory->getAbsolutePath() . $this->_entityTypeCode . DIRECTORY_SEPARATOR . FileProcessor::TMP_DIR . DIRECTORY_SEPARATOR ); } /** * Validate file by attribute validate rules * * Return array of errors * * @param array $value * @return string[] * @throws LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _validateByRules($value) { $label = $value['name']; $rules = $this->getAttribute()->getValidationRules(); try { // phpcs:ignore Magento2.Functions.DiscouragedFunction $imageProp = getimagesize($value['tmp_name']); } catch (\Throwable $e) { $imageProp = false; } if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) { return [__('"%1" is not a valid file.', $label)]; } $allowImageTypes = [1 => 'gif', 2 => 'jpg', 3 => 'png']; if (!isset($allowImageTypes[$imageProp[2]])) { return [__('"%1" is not a valid image format.', $label)]; } // modify image name $extension = $this->ioFileSystem->getPathInfo($value['name'])['extension']; if ($extension != $allowImageTypes[$imageProp[2]]) { $value['name'] = $this->ioFileSystem->getPathInfo($value['name'])['filename'] . '.' . $allowImageTypes[$imageProp[2]]; } $maxFileSize = ArrayObjectSearch::getArrayElementByName( $rules, 'max_file_size' ); $errors = []; if ($maxFileSize !== null) { $size = $value['size']; if ($maxFileSize < $size) { $errors[] = __('"%1" exceeds the allowed file size.', $label); } } $maxImageWidth = ArrayObjectSearch::getArrayElementByName( $rules, 'max_image_width' ); if ($maxImageWidth !== null) { if ($maxImageWidth < $imageProp[0]) { $r = $maxImageWidth; $errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r); } } $maxImageHeight = ArrayObjectSearch::getArrayElementByName( $rules, 'max_image_height' ); if ($maxImageHeight !== null) { if ($maxImageHeight < $imageProp[1]) { $r = $maxImageHeight; $errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r); } } return $errors; } /** * Process file uploader UI component data * * @param array $value * @return bool|int|ImageContentInterface|string * @throws LocalizedException */ protected function processUiComponentValue(array $value) { if ($this->_entityTypeCode == AddressMetadataInterface::ENTITY_TYPE_ADDRESS) { return $this->processCustomerAddressValue($value); } if ($this->_entityTypeCode == CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER) { return $this->processCustomerValue($value); } return $this->_value; } /** * Process file uploader UI component data for customer_address entity * * @param array $value * @return string * @throws LocalizedException */ protected function processCustomerAddressValue(array $value) { $fileName = $this->mediaWriteDirectory ->getDriver() ->getRealPathSafety( $this->mediaEntityTmpReadDirectory->getAbsolutePath( ltrim( $value['file'], '/' ) ) ); return $this->getFileProcessor()->moveTemporaryFile( $this->mediaEntityTmpReadDirectory->getRelativePath($fileName) ); } /** * Process file uploader UI component data for customer entity * * @param array $value * @return bool|int|ImageContentInterface|string * @throws LocalizedException */ protected function processCustomerValue(array $value) { $file = ltrim($value['file'], '/'); if ($this->mediaEntityTmpReadDirectory->isExist($file)) { $temporaryFile = FileProcessor::TMP_DIR . '/' . $file; $base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile); /** @var ImageContentInterface $imageContentDataObject */ $imageContentDataObject = $this->imageContentFactory->create() ->setName($value['name']) ->setBase64EncodedData($base64EncodedData) ->setType($value['type']); // Remove temporary file $this->getFileProcessor()->removeUploadedFile($temporaryFile); return $imageContentDataObject; } return $this->_value ?: $value['file']; } }