@@ -57,25 +57,34 @@ class Processor
57
57
*/
58
58
protected $ resourceModel ;
59
59
60
+ /**
61
+ * @var \Magento\Framework\File\Mime
62
+ */
63
+ protected $ mime ;
64
+
60
65
/**
61
66
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
62
67
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
63
68
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
64
69
* @param \Magento\Framework\Filesystem $filesystem
65
70
* @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
71
+ * @param \Magento\Framework\File\Mime $mime
72
+ * @throws \Magento\Framework\Exception\FileSystemException
66
73
*/
67
74
public function __construct (
68
75
\Magento \Catalog \Api \ProductAttributeRepositoryInterface $ attributeRepository ,
69
76
\Magento \MediaStorage \Helper \File \Storage \Database $ fileStorageDb ,
70
77
\Magento \Catalog \Model \Product \Media \Config $ mediaConfig ,
71
78
\Magento \Framework \Filesystem $ filesystem ,
72
- \Magento \Catalog \Model \ResourceModel \Product \Gallery $ resourceModel
79
+ \Magento \Catalog \Model \ResourceModel \Product \Gallery $ resourceModel ,
80
+ \Magento \Framework \File \Mime $ mime
73
81
) {
74
82
$ this ->attributeRepository = $ attributeRepository ;
75
83
$ this ->fileStorageDb = $ fileStorageDb ;
76
84
$ this ->mediaConfig = $ mediaConfig ;
77
85
$ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
78
86
$ this ->resourceModel = $ resourceModel ;
87
+ $ this ->mime = $ mime ;
79
88
}
80
89
81
90
/**
@@ -186,10 +195,10 @@ public function addImage(
186
195
$ position = 0 ;
187
196
188
197
$ absoluteFilePath = $ this ->mediaDirectory ->getAbsolutePath ($ file );
189
- $ imageMimeType = mime_content_type ($ absoluteFilePath );
190
- $ imageContent = file_get_contents ($ absoluteFilePath );
198
+ $ imageMimeType = $ this -> mime -> getMimeType ($ absoluteFilePath );
199
+ $ imageContent = $ this -> mediaDirectory -> readFile ($ absoluteFilePath );
191
200
$ imageBase64 = base64_encode ($ imageContent );
192
- $ imageName = pathinfo ( $ destinationFile , PATHINFO_FILENAME ) ;
201
+ $ imageName = $ pathinfo [ ' filename ' ] ;
193
202
194
203
if (!is_array ($ mediaGalleryData )) {
195
204
$ mediaGalleryData = ['images ' => []];
@@ -203,17 +212,17 @@ public function addImage(
203
212
204
213
$ position ++;
205
214
$ mediaGalleryData ['images ' ][] = [
206
- 'file ' => $ fileName ,
207
- 'position ' => $ position ,
208
- 'label ' => '' ,
209
- 'disabled ' => (int )$ exclude ,
215
+ 'file ' => $ fileName ,
216
+ 'position ' => $ position ,
217
+ 'label ' => '' ,
218
+ 'disabled ' => (int )$ exclude ,
210
219
'media_type ' => 'image ' ,
211
- 'types ' => $ mediaAttribute ,
212
- 'content ' => [
220
+ 'types ' => $ mediaAttribute ,
221
+ 'content ' => [
213
222
'data ' => [
214
- ImageContentInterface::NAME => $ imageName ,
223
+ ImageContentInterface::NAME => $ imageName ,
215
224
ImageContentInterface::BASE64_ENCODED_DATA => $ imageBase64 ,
216
- ImageContentInterface::TYPE => $ imageMimeType ,
225
+ ImageContentInterface::TYPE => $ imageMimeType ,
217
226
]
218
227
]
219
228
];
0 commit comments