6
6
7
7
namespace Magento \Catalog \Model \Product \Gallery ;
8
8
9
+ use Magento \Framework \Api \Data \ImageContentInterface ;
9
10
use Magento \Framework \App \Filesystem \DirectoryList ;
10
11
use Magento \Framework \Exception \LocalizedException ;
11
- use Magento \Framework \Filesystem \ DriverInterface ;
12
+ use Magento \Framework \App \ ObjectManager ;
12
13
13
14
/**
14
15
* Catalog product Media Gallery attribute processor.
@@ -56,28 +57,39 @@ class Processor
56
57
*/
57
58
protected $ resourceModel ;
58
59
60
+ /**
61
+ * @var \Magento\Framework\File\Mime
62
+ */
63
+ private $ mime ;
64
+
59
65
/**
60
66
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
61
67
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
62
68
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
63
69
* @param \Magento\Framework\Filesystem $filesystem
64
70
* @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
71
+ * @param \Magento\Framework\File\Mime|null $mime
72
+ * @throws \Magento\Framework\Exception\FileSystemException
65
73
*/
66
74
public function __construct (
67
75
\Magento \Catalog \Api \ProductAttributeRepositoryInterface $ attributeRepository ,
68
76
\Magento \MediaStorage \Helper \File \Storage \Database $ fileStorageDb ,
69
77
\Magento \Catalog \Model \Product \Media \Config $ mediaConfig ,
70
78
\Magento \Framework \Filesystem $ filesystem ,
71
- \Magento \Catalog \Model \ResourceModel \Product \Gallery $ resourceModel
79
+ \Magento \Catalog \Model \ResourceModel \Product \Gallery $ resourceModel ,
80
+ \Magento \Framework \File \Mime $ mime = null
72
81
) {
73
82
$ this ->attributeRepository = $ attributeRepository ;
74
83
$ this ->fileStorageDb = $ fileStorageDb ;
75
84
$ this ->mediaConfig = $ mediaConfig ;
76
85
$ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
77
86
$ this ->resourceModel = $ resourceModel ;
87
+ $ this ->mime = $ mime ?: ObjectManager::getInstance ()->get (\Magento \Framework \File \Mime::class);
78
88
}
79
89
80
90
/**
91
+ * Return media_gallery attribute
92
+ *
81
93
* @return \Magento\Catalog\Api\Data\ProductAttributeInterface
82
94
* @since 101.0.0
83
95
*/
@@ -183,6 +195,13 @@ public function addImage(
183
195
$ attrCode = $ this ->getAttribute ()->getAttributeCode ();
184
196
$ mediaGalleryData = $ product ->getData ($ attrCode );
185
197
$ position = 0 ;
198
+
199
+ $ absoluteFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ file );
200
+ $ imageMimeType = $ this ->mime ->getMimeType ($ absoluteFilePath );
201
+ $ imageContent = $ this ->mediaDirectory ->readFile ($ absoluteFilePath );
202
+ $ imageBase64 = base64_encode ($ imageContent );
203
+ $ imageName = $ pathinfo ['filename ' ];
204
+
186
205
if (!is_array ($ mediaGalleryData )) {
187
206
$ mediaGalleryData = ['images ' => []];
188
207
}
@@ -197,9 +216,17 @@ public function addImage(
197
216
$ mediaGalleryData ['images ' ][] = [
198
217
'file ' => $ fileName ,
199
218
'position ' => $ position ,
200
- 'media_type ' => 'image ' ,
201
219
'label ' => '' ,
202
220
'disabled ' => (int )$ exclude ,
221
+ 'media_type ' => 'image ' ,
222
+ 'types ' => $ mediaAttribute ,
223
+ 'content ' => [
224
+ 'data ' => [
225
+ ImageContentInterface::NAME => $ imageName ,
226
+ ImageContentInterface::BASE64_ENCODED_DATA => $ imageBase64 ,
227
+ ImageContentInterface::TYPE => $ imageMimeType ,
228
+ ]
229
+ ]
203
230
];
204
231
205
232
$ product ->setData ($ attrCode , $ mediaGalleryData );
@@ -358,7 +385,8 @@ public function setMediaAttribute(\Magento\Catalog\Model\Product $product, $medi
358
385
}
359
386
360
387
/**
361
- * get media attribute codes
388
+ * Get media attribute codes
389
+ *
362
390
* @return array
363
391
* @since 101.0.0
364
392
*/
@@ -368,6 +396,8 @@ public function getMediaAttributeCodes()
368
396
}
369
397
370
398
/**
399
+ * Trim .tmp ending from filename
400
+ *
371
401
* @param string $file
372
402
* @return string
373
403
* @since 101.0.0
@@ -489,7 +519,6 @@ public function getAffectedFields($object)
489
519
/**
490
520
* Attribute value is not to be saved in a conventional way, separate table is used to store the complex value
491
521
*
492
- * {@inheritdoc}
493
522
* @since 101.0.0
494
523
*/
495
524
public function isScalar ()
0 commit comments