|
9 | 9 | use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface; |
10 | 10 | use Magento\Catalog\Api\Data\ProductInterfaceFactory; |
11 | 11 | use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\Api\Data\ImageContentInterface; |
| 13 | +use Magento\Framework\Api\Data\ImageContentInterfaceFactory; |
| 14 | +use Magento\Framework\App\Filesystem\DirectoryList; |
12 | 15 | use Magento\Framework\App\ObjectManager; |
13 | 16 | use Magento\Framework\Exception\InputException; |
14 | 17 | use Magento\Framework\Exception\NoSuchEntityException; |
15 | 18 | use Magento\Framework\Exception\StateException; |
16 | 19 | use Magento\Framework\Api\ImageContentValidatorInterface; |
| 20 | +use Magento\Framework\Filesystem; |
| 21 | +use Magento\Framework\Filesystem\Driver\File\Mime; |
17 | 22 |
|
18 | 23 | /** |
19 | 24 | * Class GalleryManagement |
@@ -44,25 +49,54 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal |
44 | 49 | */ |
45 | 50 | private $deleteValidator; |
46 | 51 |
|
| 52 | + /** |
| 53 | + * @var ImageContentInterfaceFactory |
| 54 | + */ |
| 55 | + protected $imageContentInterface; |
| 56 | + |
| 57 | + /** |
| 58 | + * Filesystem facade |
| 59 | + * |
| 60 | + * @var Filesystem |
| 61 | + */ |
| 62 | + protected $filesystem; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var Mime |
| 66 | + */ |
| 67 | + protected $imageMime; |
| 68 | + |
47 | 69 | /** |
48 | 70 | * @param ProductRepositoryInterface $productRepository |
49 | 71 | * @param ImageContentValidatorInterface $contentValidator |
50 | 72 | * @param ProductInterfaceFactory|null $productInterfaceFactory |
51 | 73 | * @param DeleteValidator|null $deleteValidator |
| 74 | + * @param ImageContentInterfaceFactory|null $imageContentInterface |
| 75 | + * @param Filesystem|null $filesystem |
| 76 | + * @param Mime|null $imageMime |
52 | 77 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
53 | 78 | */ |
54 | 79 | public function __construct( |
55 | 80 | ProductRepositoryInterface $productRepository, |
56 | 81 | ImageContentValidatorInterface $contentValidator, |
57 | 82 | ?ProductInterfaceFactory $productInterfaceFactory = null, |
58 | | - ?DeleteValidator $deleteValidator = null |
| 83 | + ?DeleteValidator $deleteValidator = null, |
| 84 | + ?ImageContentInterfaceFactory $imageContentInterface = null, |
| 85 | + ?Filesystem $filesystem = null, |
| 86 | + ?Mime $imageMime = null |
59 | 87 | ) { |
60 | 88 | $this->productRepository = $productRepository; |
61 | 89 | $this->contentValidator = $contentValidator; |
62 | 90 | $this->productInterfaceFactory = $productInterfaceFactory |
63 | 91 | ?? ObjectManager::getInstance()->get(ProductInterfaceFactory::class); |
64 | 92 | $this->deleteValidator = $deleteValidator |
65 | 93 | ?? ObjectManager::getInstance()->get(DeleteValidator::class); |
| 94 | + $this->imageContentInterface = $imageContentInterface |
| 95 | + ?? ObjectManager::getInstance()->get(ImageContentInterfaceFactory::class); |
| 96 | + $this->filesystem = $filesystem |
| 97 | + ?? ObjectManager::getInstance()->get(Filesystem::class); |
| 98 | + $this->imageMime = $imageMime |
| 99 | + ?? ObjectManager::getInstance()->get(Mime::class); |
66 | 100 | } |
67 | 101 |
|
68 | 102 | /** |
@@ -217,7 +251,21 @@ public function getList($sku) |
217 | 251 | { |
218 | 252 | /** @var \Magento\Catalog\Model\Product $product */ |
219 | 253 | $product = $this->productRepository->get($sku); |
| 254 | + $mediaGalleryEntries = $product->getMediaGalleryEntries(); |
| 255 | + foreach ($mediaGalleryEntries as $entry) { |
| 256 | + $entry->setContent($this->getImageContent($product, $entry)); |
| 257 | + } |
| 258 | + return $mediaGalleryEntries; |
| 259 | + } |
220 | 260 |
|
221 | | - return $product->getMediaGalleryEntries(); |
| 261 | + private function getImageContent($product, $entry): ImageContentInterface |
| 262 | + { |
| 263 | + $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 264 | + $path = $mediaDirectory->getAbsolutePath($product->getMediaConfig()->getMediaPath($entry->getFile())); |
| 265 | + $imageFileContent = $mediaDirectory->getDriver()->fileGetContents($path); |
| 266 | + return $this->imageContentInterface->create() |
| 267 | + ->setName(basename($entry->getFile())) |
| 268 | + ->setBase64EncodedData(base64_encode($imageFileContent)) |
| 269 | + ->setType($this->imageMime->getMimeType($path)); |
222 | 270 | } |
223 | 271 | } |
0 commit comments