Skip to content

Added hash based check to prevent image duplication on product import #21146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 73 additions & 14 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1629,8 +1629,12 @@ protected function _saveProducts()
$uploadedImages = [];
$previousType = null;
$prevAttributeSet = null;
$importDir = $this->_mediaDirectory->getAbsolutePath($this->getImportDir());

$existingImages = $this->getExistingImages($bunch);

$this->addImageHashes($existingImages);

foreach ($bunch as $rowNum => $rowData) {
// reset category processor's failed categories array
$this->categoryProcessor->clearFailedCategories();
Expand Down Expand Up @@ -1791,17 +1795,41 @@ protected function _saveProducts()
$position = 0;
foreach ($rowImages as $column => $columnImages) {
foreach ($columnImages as $columnImageKey => $columnImage) {
if (!isset($uploadedImages[$columnImage])) {
$uploadedFile = $this->uploadMediaFiles($columnImage);
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
if ($uploadedFile) {
$uploadedImages[$columnImage] = $uploadedFile;
$filename = $importDir . DIRECTORY_SEPARATOR . $columnImage;
$hash = '';
if ($this->_mediaDirectory->isReadable($filename)) {
$hash = md5_file($filename);
}

if (!isset($existingImages[$rowSku])) {
$imageAlreadyExists = false;
} else {
$imageAlreadyExists = array_reduce($existingImages[$rowSku], function ($exists, $file) use ($hash) {
if ($exists) {
return $exists;
}
if ($file['hash'] === $hash) {
return $file['value'];
}
return $exists;
}, '');
}

if ($imageAlreadyExists) {
$uploadedFile = $imageAlreadyExists;
} else {
if (!isset($uploadedImages[$columnImage])) {
$uploadedFile = $this->uploadMediaFiles($columnImage);
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
if ($uploadedFile) {
$uploadedImages[$columnImage] = $uploadedFile;
} else {
unset($rowData[$column]);
$this->skipRow($rowNum, ValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE);
}
} else {
unset($rowData[$column]);
$this->skipRow($rowNum, ValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE);
$uploadedFile = $uploadedImages[$columnImage];
}
} else {
$uploadedFile = $uploadedImages[$columnImage];
}

if ($uploadedFile && $column !== self::COL_MEDIA_IMAGE) {
Expand Down Expand Up @@ -1975,6 +2003,23 @@ protected function _saveProducts()
return $this;
}

/**
* Generate md5 hashes for existing images for comparison with newly uploaded images.
*
* @param array $images
*/
public function addImageHashes(&$images) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make a method private and add strict types ti the method

$dirConfig = DirectoryList::getDefaultConfig();
$dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH];
$productPath = $this->_mediaDirectory->getAbsolutePath($dirAddon . '/catalog/product');

foreach ($images as $sku => $files) {
foreach ($files as $path => $file) {
$images[$sku][$path]['hash'] = md5_file($productPath . $file['value']);
}
}
}

/**
* Prepare array with image states (visible or hidden from product page)
*
Expand Down Expand Up @@ -2110,6 +2155,24 @@ protected function _saveProductTierPrices(array $tierPriceData)
return $this;
}

/**
* Returns the import directory if specified or a default import directory (media/import).
*
* @return string
*/
protected function getImportDir()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, make a method private and add strict types ti the method

{
$dirConfig = DirectoryList::getDefaultConfig();
$dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH];

if (!empty($this->_parameters[Import::FIELD_NAME_IMG_FILE_DIR])) {
$tmpPath = $this->_parameters[Import::FIELD_NAME_IMG_FILE_DIR];
} else {
$tmpPath = $dirAddon . '/' . $this->_mediaDirectory->getRelativePath('import');
}
return $tmpPath;
}

/**
* Returns an object for upload a media files
*
Expand All @@ -2126,11 +2189,7 @@ protected function _getUploader()
$dirConfig = DirectoryList::getDefaultConfig();
$dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH];

if (!empty($this->_parameters[Import::FIELD_NAME_IMG_FILE_DIR])) {
$tmpPath = $this->_parameters[Import::FIELD_NAME_IMG_FILE_DIR];
} else {
$tmpPath = $dirAddon . '/' . $this->_mediaDirectory->getRelativePath('import');
}
$tmpPath = $this->getImportDir();

if (!$this->_fileUploader->setTmpDir($tmpPath)) {
throw new LocalizedException(
Expand Down