Skip to content

Commit

Permalink
1.6.2 remove cached images
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-bigbridge committed Mar 15, 2021
1 parent 77a9092 commit 28d6af7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 1.6.2 : Remove cached images

In image-set-mode cached images were not removed when a new image was uploaded with the same name. They are now.

# 1.6.1 : Fix remove temporary images in force-download mode

In the (default) mode where images are re-downloaded each import, the images were left in the temporary directory (even though they were not used again). This is now no longer the case. The images are removed after the import.
Expand Down
15 changes: 14 additions & 1 deletion Model/Resource/Storage/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class ImageStorage
{
const PRODUCT_IMAGE_PATH = BP . "/pub/media/catalog/product";
const PRODUCT_CACHE_PATH = BP . "/pub/media/catalog/product/cache";

const URL_PATTERN = '#^(http://|https://|//)#i';

Expand Down Expand Up @@ -244,6 +245,13 @@ protected function removeTemporaryImages(Product $product)
}
}

protected function emptyCache(string $storagePath)
{
foreach (glob(self::PRODUCT_CACHE_PATH . "/*/" . $storagePath) as $filePath) {
@unlink($filePath);
}
}

/**
* Removes all images in $imageData (raw database information) that are not found in $existingImages (new import values)
* from gallery tables, product attributes, and file system.
Expand Down Expand Up @@ -304,8 +312,10 @@ protected function removeObsoleteImages(Product $product, array $existingImages,
// note! this only checks if the image has a role in a product, not if it is used in a gallery
// the real check would be too slow
if ($usageCount == 0) {
// remove from file system
// remove original
@unlink(self::PRODUCT_IMAGE_PATH . $storagePath);
// removed resized caches
$this->emptyCache($storagePath);
}

}
Expand Down Expand Up @@ -433,7 +443,10 @@ protected function updateImage(Image $image)
// only if the file is different in content will the old file be removed
if (!$this->filesAreEqual($targetPath, $image->getTemporaryStoragePath())) {

// remove original
unlink($targetPath);
// removed resized caches
$this->emptyCache($image->getActualStoragePath());

// link image from its temporary position to its final position
link($image->getTemporaryStoragePath(), $targetPath);
Expand Down

0 comments on commit 28d6af7

Please sign in to comment.