7
7
namespace Magento \Catalog \Model \Product \Attribute \Backend \Media ;
8
8
9
9
use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface ;
10
+ use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory ;
10
11
use Magento \Catalog \Model \Product ;
11
12
use Magento \Framework \Api \Data \ImageContentInterface ;
13
+ use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
14
+ use Magento \Framework \Api \DataObjectHelper ;
15
+ use Magento \Framework \App \Filesystem \DirectoryList ;
16
+ use Magento \Framework \App \ObjectManager ;
17
+ use Magento \Framework \Exception \FileSystemException ;
18
+ use Magento \Framework \Filesystem ;
19
+ use Magento \Framework \Filesystem \Driver \File \Mime ;
12
20
13
21
/**
14
22
* Converter for Image media gallery type
@@ -31,15 +39,42 @@ class ImageEntryConverter implements EntryConverterInterface
31
39
protected $ dataObjectHelper ;
32
40
33
41
/**
34
- * @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory
35
- * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
42
+ * @var ImageContentInterfaceFactory
43
+ */
44
+ protected $ imageContentInterface ;
45
+
46
+ /**
47
+ * Filesystem facade
48
+ *
49
+ * @var Filesystem
50
+ */
51
+ protected $ filesystem ;
52
+
53
+ /**
54
+ * @var Mime
55
+ */
56
+ protected $ imageMime ;
57
+
58
+ /**
59
+ * @param ProductAttributeMediaGalleryEntryInterfaceFactory $mediaGalleryEntryFactory
60
+ * @param DataObjectHelper $dataObjectHelper
61
+ * @param ImageContentInterfaceFactory|null $imageContentInterface
62
+ * @param Filesystem|null $filesystem
63
+ * @param Mime|null $imageMime
36
64
*/
37
65
public function __construct (
38
66
\Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory $ mediaGalleryEntryFactory ,
39
- \Magento \Framework \Api \DataObjectHelper $ dataObjectHelper
67
+ \Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
68
+ ImageContentInterfaceFactory $ imageContentInterface = null ,
69
+ Filesystem $ filesystem = null ,
70
+ Mime $ imageMime = null
71
+
40
72
) {
41
73
$ this ->mediaGalleryEntryFactory = $ mediaGalleryEntryFactory ;
42
74
$ this ->dataObjectHelper = $ dataObjectHelper ;
75
+ $ this ->imageContentInterface = $ imageContentInterface ?? ObjectManager::getInstance ()->get (ImageContentInterfaceFactory::class);
76
+ $ this ->filesystem = $ filesystem ?? ObjectManager::getInstance ()->get (Filesystem::class);
77
+ $ this ->imageMime = $ imageMime ?? ObjectManager::getInstance ()->get (Mime::class);
43
78
}
44
79
45
80
/**
@@ -71,9 +106,25 @@ public function convertTo(Product $product, array $rowData)
71
106
if (isset ($ image ['value_id ' ])) {
72
107
$ entry ->setId ($ image ['value_id ' ]);
73
108
}
109
+ $ imageFileContent = file_get_contents ($ product ->getMediaConfig ()->getMediaUrl (($ entry ->getFile ())));
110
+ $ entryContent = $ this ->imageContentInterface ->create ()
111
+ ->setName (basename ($ entry ->getFile ()))
112
+ ->setBase64EncodedData (base64_encode ($ imageFileContent ))
113
+ ->setType ($ this ->getImageMimeType ($ product ,$ entry ));
114
+ $ entry ->setContent ($ entryContent );
74
115
return $ entry ;
75
116
}
76
117
118
+ /**
119
+ * @throws FileSystemException
120
+ */
121
+ public function getImageMimeType ($ product , $ entry )
122
+ {
123
+ $ directory = $ this ->filesystem ->getDirectoryRead (DirectoryList::MEDIA );
124
+ $ path = $ directory ->getAbsolutePath ($ product ->getMediaConfig ()->getMediaPath ($ entry ->getFile ()));
125
+ return $ this ->imageMime ->getMimeType ($ path );
126
+ }
127
+
77
128
/**
78
129
* @param ProductAttributeMediaGalleryEntryInterface $entry
79
130
* @return array
0 commit comments