99use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterface ;
1010use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
1111use Magento \Catalog \Api \ProductRepositoryInterface ;
12+ use Magento \Catalog \Model \Product ;
1213use Magento \Framework \Api \Data \ImageContentInterface ;
1314use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
1415use Magento \Framework \App \Filesystem \DirectoryList ;
1516use Magento \Framework \App \ObjectManager ;
17+ use Magento \Framework \Exception \FileSystemException ;
1618use Magento \Framework \Exception \InputException ;
1719use Magento \Framework \Exception \NoSuchEntityException ;
1820use Magento \Framework \Exception \StateException ;
1921use Magento \Framework \Api \ImageContentValidatorInterface ;
2022use Magento \Framework \Filesystem ;
2123use Magento \Framework \Filesystem \Driver \File \Mime ;
24+ use Magento \Framework \Filesystem \Io \File ;
2225
2326/**
2427 * Class GalleryManagement
@@ -64,7 +67,12 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
6467 /**
6568 * @var Mime
6669 */
67- protected $ imageMime ;
70+ protected $ mime ;
71+
72+ /**
73+ * @var File
74+ */
75+ protected $ file ;
6876
6977 /**
7078 * @param ProductRepositoryInterface $productRepository
@@ -73,7 +81,8 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
7381 * @param DeleteValidator|null $deleteValidator
7482 * @param ImageContentInterfaceFactory|null $imageContentInterface
7583 * @param Filesystem|null $filesystem
76- * @param Mime|null $imageMime
84+ * @param Mime|null $mime
85+ * @param File|null $file
7786 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
7887 */
7988 public function __construct (
@@ -83,7 +92,8 @@ public function __construct(
8392 ?DeleteValidator $ deleteValidator = null ,
8493 ?ImageContentInterfaceFactory $ imageContentInterface = null ,
8594 ?Filesystem $ filesystem = null ,
86- ?Mime $ imageMime = null
95+ ?Mime $ mime = null ,
96+ ?File $ file = null
8797 ) {
8898 $ this ->productRepository = $ productRepository ;
8999 $ this ->contentValidator = $ contentValidator ;
@@ -95,8 +105,12 @@ public function __construct(
95105 ?? ObjectManager::getInstance ()->get (ImageContentInterfaceFactory::class);
96106 $ this ->filesystem = $ filesystem
97107 ?? ObjectManager::getInstance ()->get (Filesystem::class);
98- $ this ->imageMime = $ imageMime
108+ $ this ->mime = $ mime
99109 ?? ObjectManager::getInstance ()->get (Mime::class);
110+ $ this ->file = $ file
111+ ?? ObjectManager::getInstance ()->get (
112+ File::class
113+ );
100114 }
101115
102116 /**
@@ -249,23 +263,31 @@ public function get($sku, $entryId)
249263 */
250264 public function getList ($ sku )
251265 {
252- /** @var \Magento\Catalog\Model\ Product $product */
266+ /** @var Product $product */
253267 $ product = $ this ->productRepository ->get ($ sku );
254268 $ mediaGalleryEntries = $ product ->getMediaGalleryEntries ();
255269 foreach ($ mediaGalleryEntries as $ entry ) {
256- $ entry ->setContent ($ this ->getImageContent ($ product , $ entry ));
270+ $ entry ->setContent ($ this ->getImageContent ($ product , $ entry ));
257271 }
258272 return $ mediaGalleryEntries ;
259273 }
260274
275+ /**
276+ * Get image content
277+ *
278+ * @param Product $product
279+ * @param ProductAttributeMediaGalleryEntryInterface $entry
280+ * @throws FileSystemException
281+ */
261282 private function getImageContent ($ product , $ entry ): ImageContentInterface
262283 {
263284 $ mediaDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
264285 $ path = $ mediaDirectory ->getAbsolutePath ($ product ->getMediaConfig ()->getMediaPath ($ entry ->getFile ()));
286+ $ fileName = $ this ->file ->getPathInfo ($ path )['basename ' ];
265287 $ imageFileContent = $ mediaDirectory ->getDriver ()->fileGetContents ($ path );
266288 return $ this ->imageContentInterface ->create ()
267- ->setName (basename ( $ entry -> getFile ()) )
289+ ->setName ($ fileName )
268290 ->setBase64EncodedData (base64_encode ($ imageFileContent ))
269- ->setType ($ this ->imageMime ->getMimeType ($ path ));
291+ ->setType ($ this ->mime ->getMimeType ($ path ));
270292 }
271293}
0 commit comments