9
9
use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface ;
10
10
use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
+ use Magento \Catalog \Model \Product ;
12
13
use Magento \Framework \Api \Data \ImageContentInterface ;
13
14
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
14
15
use Magento \Framework \App \Filesystem \DirectoryList ;
15
16
use Magento \Framework \App \ObjectManager ;
17
+ use Magento \Framework \Exception \FileSystemException ;
16
18
use Magento \Framework \Exception \InputException ;
17
19
use Magento \Framework \Exception \NoSuchEntityException ;
18
20
use Magento \Framework \Exception \StateException ;
19
21
use Magento \Framework \Api \ImageContentValidatorInterface ;
20
22
use Magento \Framework \Filesystem ;
21
23
use Magento \Framework \Filesystem \Driver \File \Mime ;
24
+ use Magento \Framework \Filesystem \Io \File ;
22
25
23
26
/**
24
27
* Class GalleryManagement
@@ -64,7 +67,12 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
64
67
/**
65
68
* @var Mime
66
69
*/
67
- protected $ imageMime ;
70
+ protected $ mime ;
71
+
72
+ /**
73
+ * @var File
74
+ */
75
+ protected $ file ;
68
76
69
77
/**
70
78
* @param ProductRepositoryInterface $productRepository
@@ -73,7 +81,8 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
73
81
* @param DeleteValidator|null $deleteValidator
74
82
* @param ImageContentInterfaceFactory|null $imageContentInterface
75
83
* @param Filesystem|null $filesystem
76
- * @param Mime|null $imageMime
84
+ * @param Mime|null $mime
85
+ * @param File|null $file
77
86
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
78
87
*/
79
88
public function __construct (
@@ -83,7 +92,8 @@ public function __construct(
83
92
?DeleteValidator $ deleteValidator = null ,
84
93
?ImageContentInterfaceFactory $ imageContentInterface = null ,
85
94
?Filesystem $ filesystem = null ,
86
- ?Mime $ imageMime = null
95
+ ?Mime $ mime = null ,
96
+ ?File $ file = null
87
97
) {
88
98
$ this ->productRepository = $ productRepository ;
89
99
$ this ->contentValidator = $ contentValidator ;
@@ -95,8 +105,12 @@ public function __construct(
95
105
?? ObjectManager::getInstance ()->get (ImageContentInterfaceFactory::class);
96
106
$ this ->filesystem = $ filesystem
97
107
?? ObjectManager::getInstance ()->get (Filesystem::class);
98
- $ this ->imageMime = $ imageMime
108
+ $ this ->mime = $ mime
99
109
?? ObjectManager::getInstance ()->get (Mime::class);
110
+ $ this ->file = $ file
111
+ ?? ObjectManager::getInstance ()->get (
112
+ File::class
113
+ );
100
114
}
101
115
102
116
/**
@@ -249,23 +263,31 @@ public function get($sku, $entryId)
249
263
*/
250
264
public function getList ($ sku )
251
265
{
252
- /** @var \Magento\Catalog\Model\ Product $product */
266
+ /** @var Product $product */
253
267
$ product = $ this ->productRepository ->get ($ sku );
254
268
$ mediaGalleryEntries = $ product ->getMediaGalleryEntries ();
255
269
foreach ($ mediaGalleryEntries as $ entry ) {
256
- $ entry ->setContent ($ this ->getImageContent ($ product , $ entry ));
270
+ $ entry ->setContent ($ this ->getImageContent ($ product , $ entry ));
257
271
}
258
272
return $ mediaGalleryEntries ;
259
273
}
260
274
275
+ /**
276
+ * Get image content
277
+ *
278
+ * @param Product $product
279
+ * @param ProductAttributeMediaGalleryEntryInterface $entry
280
+ * @throws FileSystemException
281
+ */
261
282
private function getImageContent ($ product , $ entry ): ImageContentInterface
262
283
{
263
284
$ mediaDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
264
285
$ path = $ mediaDirectory ->getAbsolutePath ($ product ->getMediaConfig ()->getMediaPath ($ entry ->getFile ()));
286
+ $ fileName = $ this ->file ->getPathInfo ($ path )['basename ' ];
265
287
$ imageFileContent = $ mediaDirectory ->getDriver ()->fileGetContents ($ path );
266
288
return $ this ->imageContentInterface ->create ()
267
- ->setName (basename ( $ entry -> getFile ()) )
289
+ ->setName ($ fileName )
268
290
->setBase64EncodedData (base64_encode ($ imageFileContent ))
269
- ->setType ($ this ->imageMime ->getMimeType ($ path ));
291
+ ->setType ($ this ->mime ->getMimeType ($ path ));
270
292
}
271
293
}
0 commit comments