Skip to content

Commit bca6bf6

Browse files
fix import image from external url
1 parent f16811c commit bca6bf6

File tree

1 file changed

+30
-17
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+30
-17
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,11 +1750,7 @@ protected function _saveProducts()
17501750
$position = 0;
17511751
foreach ($rowImages as $column => $columnImages) {
17521752
foreach ($columnImages as $columnImageKey => $columnImage) {
1753-
$filePath = filter_var($columnImage, FILTER_VALIDATE_URL)
1754-
? $columnImage
1755-
: $importDir . DS . $columnImage;
1756-
1757-
$uploadedFile = $this->getAlreadyExistedImage($rowExistingImages, $filePath);
1753+
$uploadedFile = $this->getAlreadyExistedImage($rowExistingImages, $columnImage, $importDir);
17581754
if (!$uploadedFile && !isset($uploadedImages[$columnImage])) {
17591755
$uploadedFile = $this->uploadMediaFiles($columnImage);
17601756
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
@@ -1948,30 +1944,30 @@ protected function _saveProducts()
19481944
/**
19491945
* Returns image hash by path
19501946
*
1951-
* @param string $filePath
1947+
* @param string $path
19521948
* @return string
19531949
*/
1954-
private function getFileHash(string $filePath): string
1950+
private function getFileHash(string $path): string
19551951
{
1956-
try {
1957-
$fileExists = $this->fileDriver->isExists($filePath);
1958-
} catch (\Exception $exception) {
1959-
$fileExists = false;
1960-
}
1961-
1962-
return $fileExists ? hash_file(self::HASH_ALGORITHM, $filePath) : '';
1952+
return hash_file(self::HASH_ALGORITHM, $path);
19631953
}
19641954

19651955
/**
19661956
* Returns existed image
19671957
*
19681958
* @param array $imageRow
1969-
* @param string $filePath
1959+
* @param string $columnImage
1960+
* @param string $importDir
19701961
* @return string
19711962
*/
1972-
private function getAlreadyExistedImage(array $imageRow, string $filePath): string
1963+
private function getAlreadyExistedImage(array $imageRow, string $columnImage, string $importDir): string
19731964
{
1974-
$hash = $this->getFileHash($filePath);
1965+
if (filter_var($columnImage, FILTER_VALIDATE_URL)) {
1966+
$hash = $this->getFileHash($columnImage);
1967+
} else {
1968+
$path = $importDir . DS . $columnImage;
1969+
$hash = $this->isFileExists($path) ? $this->getFileHash($path) : '';
1970+
}
19751971

19761972
return array_reduce(
19771973
$imageRow,
@@ -2013,6 +2009,23 @@ private function addImageHashes(array &$images): void
20132009
}
20142010
}
20152011

2012+
/**
2013+
* Is file exists
2014+
*
2015+
* @param string $path
2016+
* @return bool
2017+
*/
2018+
private function isFileExists(string $path): bool
2019+
{
2020+
try {
2021+
$fileExists = $this->fileDriver->isExists($path);
2022+
} catch (\Exception $exception) {
2023+
$fileExists = false;
2024+
}
2025+
2026+
return $fileExists;
2027+
}
2028+
20162029
/**
20172030
* Clears entries from Image Set and Row Data marked as no_selection
20182031
*

0 commit comments

Comments
 (0)