5
5
*/
6
6
namespace Magento \Catalog \Model \Product \Gallery ;
7
7
8
+ use Magento \Framework \Api \Data \ImageContentInterface ;
8
9
use Magento \Framework \App \Filesystem \DirectoryList ;
9
10
use Magento \Framework \Exception \LocalizedException ;
10
- use Magento \Framework \Filesystem \ DriverInterface ;
11
+ use Magento \Framework \App \ ObjectManager ;
11
12
12
13
/**
13
14
* Catalog product Media Gallery attribute processor.
@@ -55,28 +56,39 @@ class Processor
55
56
*/
56
57
protected $ resourceModel ;
57
58
59
+ /**
60
+ * @var \Magento\Framework\File\Mime
61
+ */
62
+ private $ mime ;
63
+
58
64
/**
59
65
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
60
66
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
61
67
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
62
68
* @param \Magento\Framework\Filesystem $filesystem
63
69
* @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
70
+ * @param \Magento\Framework\File\Mime|null $mime
71
+ * @throws \Magento\Framework\Exception\FileSystemException
64
72
*/
65
73
public function __construct (
66
74
\Magento \Catalog \Api \ProductAttributeRepositoryInterface $ attributeRepository ,
67
75
\Magento \MediaStorage \Helper \File \Storage \Database $ fileStorageDb ,
68
76
\Magento \Catalog \Model \Product \Media \Config $ mediaConfig ,
69
77
\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
71
80
) {
72
81
$ this ->attributeRepository = $ attributeRepository ;
73
82
$ this ->fileStorageDb = $ fileStorageDb ;
74
83
$ this ->mediaConfig = $ mediaConfig ;
75
84
$ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
76
85
$ this ->resourceModel = $ resourceModel ;
86
+ $ this ->mime = $ mime ?: ObjectManager::getInstance ()->get (\Magento \Framework \File \Mime::class);
77
87
}
78
88
79
89
/**
90
+ * Return media_gallery attribute
91
+ *
80
92
* @return \Magento\Catalog\Api\Data\ProductAttributeInterface
81
93
* @since 101.0.0
82
94
*/
@@ -178,6 +190,13 @@ public function addImage(
178
190
$ attrCode = $ this ->getAttribute ()->getAttributeCode ();
179
191
$ mediaGalleryData = $ product ->getData ($ attrCode );
180
192
$ 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
+
181
200
if (!is_array ($ mediaGalleryData )) {
182
201
$ mediaGalleryData = ['images ' => []];
183
202
}
@@ -192,9 +211,17 @@ public function addImage(
192
211
$ mediaGalleryData ['images ' ][] = [
193
212
'file ' => $ fileName ,
194
213
'position ' => $ position ,
195
- 'media_type ' => 'image ' ,
196
214
'label ' => '' ,
197
215
'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
+ ]
198
225
];
199
226
200
227
$ product ->setData ($ attrCode , $ mediaGalleryData );
@@ -353,7 +380,8 @@ public function setMediaAttribute(\Magento\Catalog\Model\Product $product, $medi
353
380
}
354
381
355
382
/**
356
- * get media attribute codes
383
+ * Get media attribute codes
384
+ *
357
385
* @return array
358
386
* @since 101.0.0
359
387
*/
@@ -363,6 +391,8 @@ public function getMediaAttributeCodes()
363
391
}
364
392
365
393
/**
394
+ * Trim .tmp ending from filename
395
+ *
366
396
* @param string $file
367
397
* @return string
368
398
* @since 101.0.0
@@ -484,7 +514,6 @@ public function getAffectedFields($object)
484
514
/**
485
515
* Attribute value is not to be saved in a conventional way, separate table is used to store the complex value
486
516
*
487
- * {@inheritdoc}
488
517
* @since 101.0.0
489
518
*/
490
519
public function isScalar ()
0 commit comments