@@ -1568,6 +1568,7 @@ protected function _saveProducts()
1568
1568
$ priceIsGlobal = $ this ->_catalogData ->isPriceGlobal ();
1569
1569
$ previousType = null ;
1570
1570
$ prevAttributeSet = null ;
1571
+ $ productMediaPath = $ this ->getProductMediaPath ();
1571
1572
while ($ bunch = $ this ->_dataSourceModel ->getNextBunch ()) {
1572
1573
$ entityRowsIn = [];
1573
1574
$ entityRowsUp = [];
@@ -1579,7 +1580,6 @@ protected function _saveProducts()
1579
1580
$ imagesForChangeVisibility = [];
1580
1581
$ uploadedImages = [];
1581
1582
$ existingImages = $ this ->getExistingImages ($ bunch );
1582
- $ this ->addImageHashes ($ existingImages );
1583
1583
$ attributes = [];
1584
1584
foreach ($ bunch as $ rowNum => $ rowData ) {
1585
1585
try {
@@ -1626,6 +1626,7 @@ protected function _saveProducts()
1626
1626
$ rowData ,
1627
1627
$ storeId ,
1628
1628
$ existingImages ,
1629
+ $ productMediaPath ,
1629
1630
$ uploadedImages ,
1630
1631
$ imagesForChangeVisibility ,
1631
1632
$ labelsForUpdate ,
@@ -1811,6 +1812,7 @@ private function saveProductMediaGalleryPhase(
1811
1812
array &$ rowData ,
1812
1813
int $ storeId ,
1813
1814
array $ existingImages ,
1815
+ string $ productMediaPath ,
1814
1816
array &$ uploadedImages ,
1815
1817
array &$ imagesForChangeVisibility ,
1816
1818
array &$ labelsForUpdate ,
@@ -1844,10 +1846,11 @@ private function saveProductMediaGalleryPhase(
1844
1846
$ position = 0 ;
1845
1847
foreach ($ rowImages as $ column => $ columnImages ) {
1846
1848
foreach ($ columnImages as $ columnImageKey => $ columnImage ) {
1847
- $ hash = filter_var ($ columnImage , FILTER_VALIDATE_URL )
1848
- ? $ this ->getRemoteFileHash ($ columnImage )
1849
- : $ this ->getFileHash ($ this ->joinFilePaths ($ this ->getUploader ()->getTmpDir (), $ columnImage ));
1850
- $ uploadedFile = $ this ->findImageByHash ($ rowExistingImages , $ hash );
1849
+ $ uploadedFile = $ this ->findImageByColumnImage (
1850
+ $ productMediaPath ,
1851
+ $ rowExistingImages ,
1852
+ $ columnImage
1853
+ );
1851
1854
if (!$ uploadedFile && !isset ($ uploadedImages [$ columnImage ])) {
1852
1855
$ uploadedFile = $ this ->uploadMediaFiles ($ columnImage );
1853
1856
$ uploadedFile = $ uploadedFile ?: $ this ->getSystemFile ($ columnImage );
@@ -2030,54 +2033,33 @@ private function saveProductAttributesPhase(
2030
2033
}
2031
2034
2032
2035
/**
2033
- * Returns image hash by path
2036
+ * Returns image content by path
2034
2037
*
2035
2038
* @param string $path
2036
2039
* @return string
2037
2040
* @throws \Magento\Framework\Exception\FileSystemException
2038
2041
*/
2039
- private function getFileHash (string $ path ): string
2042
+ private function getFileContent (string $ path ): string
2040
2043
{
2041
2044
$ content = '' ;
2042
2045
if ($ this ->_mediaDirectory ->isFile ($ path )
2043
2046
&& $ this ->_mediaDirectory ->isReadable ($ path )
2044
2047
) {
2045
2048
$ content = $ this ->_mediaDirectory ->readFile ($ path );
2046
2049
}
2047
- return $ content ? hash ( self :: HASH_ALGORITHM , $ content ) : '' ;
2050
+ return $ content ;
2048
2051
}
2049
2052
2050
2053
/**
2051
- * Returns hash for remote file
2054
+ * Returns content for remote file
2052
2055
*
2053
2056
* @param string $filename
2054
2057
* @return string
2055
2058
*/
2056
- private function getRemoteFileHash (string $ filename ): string
2059
+ private function getRemoteFileContent (string $ filename ): string
2057
2060
{
2058
- $ hash = hash_file (self ::HASH_ALGORITHM , $ filename );
2059
- return $ hash !== false ? $ hash : '' ;
2060
- }
2061
-
2062
- /**
2063
- * Generate hashes for existing images for comparison with newly uploaded images.
2064
- *
2065
- * @param array $images
2066
- * @return void
2067
- */
2068
- private function addImageHashes (array &$ images ): void
2069
- {
2070
- $ productMediaPath = $ this ->getProductMediaPath ();
2071
- foreach ($ images as $ storeId => $ skus ) {
2072
- foreach ($ skus as $ sku => $ files ) {
2073
- foreach ($ files as $ path => $ file ) {
2074
- $ hash = $ this ->getFileHash ($ this ->joinFilePaths ($ productMediaPath , $ file ['value ' ]));
2075
- if ($ hash ) {
2076
- $ images [$ storeId ][$ sku ][$ path ]['hash ' ] = $ hash ;
2077
- }
2078
- }
2079
- }
2080
- }
2061
+ $ content = file_get_contents ($ filename );
2062
+ return $ content !== false ? $ content : '' ;
2081
2063
}
2082
2064
2083
2065
/**
@@ -3325,26 +3307,68 @@ private function getRowExistingStockItem(array $rowData): StockItemInterface
3325
3307
}
3326
3308
3327
3309
/**
3328
- * Returns image that matches the provided hash
3310
+ * Returns image that matches the provided image content
3329
3311
*
3312
+ * @param string $productMediaPath
3330
3313
* @param array $images
3331
- * @param string $hash
3314
+ * @param string $columnImage
3332
3315
* @return string
3333
3316
*/
3334
- private function findImageByHash ( array $ images , string $ hash ): string
3317
+ private function findImageByColumnImage ( string $ productMediaPath , array & $ images , string $ columnImage ): string
3335
3318
{
3319
+ $ content = filter_var ($ columnImage , FILTER_VALIDATE_URL )
3320
+ ? $ this ->getRemoteFileContent ($ columnImage )
3321
+ : $ this ->getFileContent ($ this ->joinFilePaths ($ this ->getUploader ()->getTmpDir (), $ columnImage ));
3336
3322
$ value = '' ;
3337
- if ($ hash ) {
3338
- foreach ($ images as $ image ) {
3339
- if (isset ($ image ['hash ' ]) && $ image ['hash ' ] === $ hash ) {
3340
- $ value = $ image ['value ' ];
3341
- break ;
3323
+ if ($ content ) {
3324
+ $ useHash = $ this ->shouldUseHash ($ images );
3325
+ if ($ useHash ) {
3326
+ $ hash = hash (self ::HASH_ALGORITHM , $ content );
3327
+ }
3328
+ foreach ($ images as &$ image ) {
3329
+ if ($ useHash ) {
3330
+ if (!isset ($ image ['hash ' ])) {
3331
+ $ imageContent = $ this ->getFileContent ($ this ->joinFilePaths ($ productMediaPath , $ image ['value ' ]));
3332
+ if (!$ imageContent ) {
3333
+ $ image ['hash ' ] = '' ;
3334
+ continue ;
3335
+ }
3336
+ $ image ['hash ' ] = hash (self ::HASH_ALGORITHM , $ imageContent );
3337
+ }
3338
+ if (isset ($ image ['hash ' ]) && $ image ['hash ' ] === $ hash ) {
3339
+ $ value = $ image ['value ' ];
3340
+ break ;
3341
+ }
3342
+ } else {
3343
+ if (!isset ($ image ['content ' ])) {
3344
+ $ image ['content ' ] = $ this ->getFileContent (
3345
+ $ this ->joinFilePaths ($ productMediaPath , $ image ['value ' ])
3346
+ );
3347
+ }
3348
+ if ($ content === $ image ['content ' ]) {
3349
+ $ value = $ image ['value ' ];
3350
+ break ;
3351
+ }
3342
3352
}
3343
3353
}
3344
3354
}
3345
3355
return $ value ;
3346
3356
}
3347
3357
3358
+ /**
3359
+ * Returns true if we should use hash instead of just comparing content
3360
+ *
3361
+ * @param array $images
3362
+ * @return bool
3363
+ */
3364
+ private function shouldUseHash (array $ images ): bool
3365
+ {
3366
+ if (count ($ images ) > 100 ) {
3367
+ return true ;
3368
+ }
3369
+ return false ;
3370
+ }
3371
+
3348
3372
/**
3349
3373
* Returns product media
3350
3374
*
0 commit comments