Skip to content

Commit ef6c9b8

Browse files
author
lakshmana
committed
ACP2E-1526 : No base64 image code in REST API response
1 parent bd8634b commit ef6c9b8

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media/ImageEntryConverter.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
namespace Magento\Catalog\Model\Product\Attribute\Backend\Media;
88

99
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
10+
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory;
1011
use Magento\Catalog\Model\Product;
1112
use Magento\Framework\Api\Data\ImageContentInterface;
13+
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
14+
use Magento\Framework\Api\DataObjectHelper;
15+
use Magento\Framework\App\Filesystem\DirectoryList;
16+
use Magento\Framework\App\ObjectManager;
17+
use Magento\Framework\Exception\FileSystemException;
18+
use Magento\Framework\Filesystem;
19+
use Magento\Framework\Filesystem\Driver\File\Mime;
1220

1321
/**
1422
* Converter for Image media gallery type
@@ -31,15 +39,42 @@ class ImageEntryConverter implements EntryConverterInterface
3139
protected $dataObjectHelper;
3240

3341
/**
34-
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory
35-
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
42+
* @var ImageContentInterfaceFactory
43+
*/
44+
protected $imageContentInterface;
45+
46+
/**
47+
* Filesystem facade
48+
*
49+
* @var Filesystem
50+
*/
51+
protected $filesystem;
52+
53+
/**
54+
* @var Mime
55+
*/
56+
protected $imageMime;
57+
58+
/**
59+
* @param ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory
60+
* @param DataObjectHelper $dataObjectHelper
61+
* @param ImageContentInterfaceFactory|null $imageContentInterface
62+
* @param Filesystem|null $filesystem
63+
* @param Mime|null $imageMime
3664
*/
3765
public function __construct(
3866
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory,
39-
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
67+
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
68+
ImageContentInterfaceFactory $imageContentInterface = null,
69+
Filesystem $filesystem = null,
70+
Mime $imageMime = null
71+
4072
) {
4173
$this->mediaGalleryEntryFactory = $mediaGalleryEntryFactory;
4274
$this->dataObjectHelper = $dataObjectHelper;
75+
$this->imageContentInterface = $imageContentInterface ?? ObjectManager::getInstance()->get(ImageContentInterfaceFactory::class);
76+
$this->filesystem = $filesystem ?? ObjectManager::getInstance()->get(Filesystem::class);
77+
$this->imageMime = $imageMime ?? ObjectManager::getInstance()->get(Mime::class);
4378
}
4479

4580
/**
@@ -71,9 +106,25 @@ public function convertTo(Product $product, array $rowData)
71106
if (isset($image['value_id'])) {
72107
$entry->setId($image['value_id']);
73108
}
109+
$imageFileContent = file_get_contents($product->getMediaConfig()->getMediaUrl(($entry->getFile())));
110+
$entryContent = $this->imageContentInterface->create()
111+
->setName(basename($entry->getFile()))
112+
->setBase64EncodedData(base64_encode($imageFileContent))
113+
->setType($this->getImageMimeType($product,$entry));
114+
$entry->setContent($entryContent);
74115
return $entry;
75116
}
76117

118+
/**
119+
* @throws FileSystemException
120+
*/
121+
public function getImageMimeType($product, $entry)
122+
{
123+
$directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
124+
$path = $directory->getAbsolutePath($product->getMediaConfig()->getMediaPath($entry->getFile()));
125+
return $this->imageMime->getMimeType($path);
126+
}
127+
77128
/**
78129
* @param ProductAttributeMediaGalleryEntryInterface $entry
79130
* @return array

0 commit comments

Comments
 (0)