28 'image/jpeg' =>
'jpg',
41 private $contentValidator;
46 private $dataObjectHelper;
61 private $mediaDirectory;
74 \Psr\Log\LoggerInterface
$logger,
77 $this->filesystem = $fileSystem;
78 $this->contentValidator = $contentValidator;
81 $this->uploader = $uploader;
94 $imageDataObjects = $this->dataObjectHelper->getCustomAttributeValueByType(
96 \Magento\Framework\Api\Data\ImageContentInterface::class
100 if (empty($imageDataObjects)) {
101 return $dataObjectWithCustomAttributes;
106 foreach ($imageDataObjects as $imageDataObject) {
115 $imageDataObject->getAttributeCode(),
120 if ($previousCustomerData) {
121 $previousImageAttribute = $previousCustomerData->getCustomAttribute(
122 $imageDataObject->getAttributeCode()
124 if ($previousImageAttribute) {
125 $previousImagePath = $previousImageAttribute->getValue();
126 if (!empty($previousImagePath) && ($previousImagePath != $filename)) {
127 @
unlink($this->mediaDirectory->getAbsolutePath() .
$entityType . $previousImagePath);
133 return $dataObjectWithCustomAttributes;
142 throw new InputException(
new Phrase(
'The image content is invalid. Verify the content and try again.'));
145 $fileContent = @base64_decode(
$imageContent->getBase64EncodedData(),
true);
148 $tmpFileName = substr(md5(rand()), 0, 7) .
'.' .
$fileName;
152 'tmp_name' =>
$tmpDirectory->getAbsolutePath() . $tmpFileName,
157 $this->uploader->processFileAttributes($fileAttributes);
158 $this->uploader->setFilesDispersion(
true);
159 $this->uploader->setFilenamesCaseSensitivity(
false);
160 $this->uploader->setAllowRenameFiles(
true);
162 $this->uploader->save($this->mediaDirectory->getAbsolutePath($destinationFolder),
$fileName);
163 }
catch (\Exception $e) {
164 $this->logger->critical($e);
166 return $this->uploader->getUploadedFileName();
175 return $this->mimeTypeExtensionMap[$mimeType] ??
'';
186 if (!pathinfo(
$fileName, PATHINFO_EXTENSION)) {
save(CustomAttributesDataInterface $dataObjectWithCustomAttributes, $entityType, CustomAttributesDataInterface $previousCustomerData=null)
getMimeTypeExtension($mimeType)
setCustomAttribute($attributeCode, $attributeValue)
__construct(Filesystem $fileSystem, ImageContentValidatorInterface $contentValidator, DataObjectHelper $dataObjectHelper, \Psr\Log\LoggerInterface $logger, Uploader $uploader)
processImageContent($entityType, $imageContent)