55 */
66namespace Magento \Catalog \Model \Product \Gallery ;
77
8+ use Magento \Framework \Api \Data \ImageContentInterface ;
89use Magento \Framework \App \Filesystem \DirectoryList ;
910use Magento \Framework \Exception \LocalizedException ;
10- use Magento \Framework \Filesystem \ DriverInterface ;
11+ use Magento \Framework \App \ ObjectManager ;
1112
1213/**
1314 * Catalog product Media Gallery attribute processor.
@@ -55,28 +56,39 @@ class Processor
5556 */
5657 protected $ resourceModel ;
5758
59+ /**
60+ * @var \Magento\Framework\File\Mime
61+ */
62+ private $ mime ;
63+
5864 /**
5965 * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
6066 * @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
6167 * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
6268 * @param \Magento\Framework\Filesystem $filesystem
6369 * @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
70+ * @param \Magento\Framework\File\Mime|null $mime
71+ * @throws \Magento\Framework\Exception\FileSystemException
6472 */
6573 public function __construct (
6674 \Magento \Catalog \Api \ProductAttributeRepositoryInterface $ attributeRepository ,
6775 \Magento \MediaStorage \Helper \File \Storage \Database $ fileStorageDb ,
6876 \Magento \Catalog \Model \Product \Media \Config $ mediaConfig ,
6977 \Magento \Framework \Filesystem $ filesystem ,
70- \Magento \Catalog \Model \ResourceModel \Product \Gallery $ resourceModel
78+ \Magento \Catalog \Model \ResourceModel \Product \Gallery $ resourceModel ,
79+ \Magento \Framework \File \Mime $ mime = null
7180 ) {
7281 $ this ->attributeRepository = $ attributeRepository ;
7382 $ this ->fileStorageDb = $ fileStorageDb ;
7483 $ this ->mediaConfig = $ mediaConfig ;
7584 $ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
7685 $ this ->resourceModel = $ resourceModel ;
86+ $ this ->mime = $ mime ?: ObjectManager::getInstance ()->get (\Magento \Framework \File \Mime::class);
7787 }
7888
7989 /**
90+ * Return media_gallery attribute
91+ *
8092 * @return \Magento\Catalog\Api\Data\ProductAttributeInterface
8193 * @since 101.0.0
8294 */
@@ -178,6 +190,13 @@ public function addImage(
178190 $ attrCode = $ this ->getAttribute ()->getAttributeCode ();
179191 $ mediaGalleryData = $ product ->getData ($ attrCode );
180192 $ position = 0 ;
193+
194+ $ absoluteFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ file );
195+ $ imageMimeType = $ this ->mime ->getMimeType ($ absoluteFilePath );
196+ $ imageContent = $ this ->mediaDirectory ->readFile ($ absoluteFilePath );
197+ $ imageBase64 = base64_encode ($ imageContent );
198+ $ imageName = $ pathinfo ['filename ' ];
199+
181200 if (!is_array ($ mediaGalleryData )) {
182201 $ mediaGalleryData = ['images ' => []];
183202 }
@@ -192,9 +211,17 @@ public function addImage(
192211 $ mediaGalleryData ['images ' ][] = [
193212 'file ' => $ fileName ,
194213 'position ' => $ position ,
195- 'media_type ' => 'image ' ,
196214 'label ' => '' ,
197215 'disabled ' => (int )$ exclude ,
216+ 'media_type ' => 'image ' ,
217+ 'types ' => $ mediaAttribute ,
218+ 'content ' => [
219+ 'data ' => [
220+ ImageContentInterface::NAME => $ imageName ,
221+ ImageContentInterface::BASE64_ENCODED_DATA => $ imageBase64 ,
222+ ImageContentInterface::TYPE => $ imageMimeType ,
223+ ]
224+ ]
198225 ];
199226
200227 $ product ->setData ($ attrCode , $ mediaGalleryData );
@@ -353,7 +380,8 @@ public function setMediaAttribute(\Magento\Catalog\Model\Product $product, $medi
353380 }
354381
355382 /**
356- * get media attribute codes
383+ * Get media attribute codes
384+ *
357385 * @return array
358386 * @since 101.0.0
359387 */
@@ -363,6 +391,8 @@ public function getMediaAttributeCodes()
363391 }
364392
365393 /**
394+ * Trim .tmp ending from filename
395+ *
366396 * @param string $file
367397 * @return string
368398 * @since 101.0.0
@@ -484,7 +514,6 @@ public function getAffectedFields($object)
484514 /**
485515 * Attribute value is not to be saved in a conventional way, separate table is used to store the complex value
486516 *
487- * {@inheritdoc}
488517 * @since 101.0.0
489518 */
490519 public function isScalar ()
0 commit comments