Skip to content

Commit

Permalink
Merge pull request #25 from fvanzani/main
Browse files Browse the repository at this point in the history
Consider the relation table media to entity when removing unused media images
  • Loading branch information
peterjaap authored Dec 31, 2024
2 parents 447e799 + 6f1c890 commit 3e18c33
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Console/Command/RemoveUnusedMediaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class RemoveUnusedMediaCommand extends Command
{
private const OPTION_DRY_RUN = 'dry-run';
private const OPTION_INCLUDING_CACHE = 'including-cache';
private const OPTION_ONLY_CACHE = 'only-cache';
private const OPTION_INCLUDING_RELATION_ENTITY = 'including-relation';
private const OPTION_FORCE = 'force';
private const COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED = 'eav:media:remove-unused';

Expand Down Expand Up @@ -44,6 +46,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
$isForce = $input->getOption(self::OPTION_FORCE);
$isDryRun = $input->getOption(self::OPTION_DRY_RUN);
$deleteCacheAsWell = $input->getOption(self::OPTION_INCLUDING_CACHE);
$deleteOnlyCache = $input->getOption(self::OPTION_ONLY_CACHE);
if ($deleteOnlyCache) {
$deleteCacheAsWell=true;
}
$deleteNotInRelation = $input->getOption(self::OPTION_INCLUDING_RELATION_ENTITY);

if (!$isDryRun && !$isForce) {
if (!$input->isInteractive()) {
Expand Down Expand Up @@ -76,6 +83,12 @@ public function execute(InputInterface $input, OutputInterface $output): int

$imagesToKeep = $connection->fetchCol('SELECT value FROM ' . $mediaGalleryTable);

if ($deleteNotInRelation) {
$mediaGalleryToEntityTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value_to_entity');
$sql='SELECT value FROM ' . $mediaGalleryTable. ' where value_id IN (SELECT value_id from '.$mediaGalleryToEntityTable.')';
$imagesToKeep = $connection->fetchCol($sql);
}

foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
// Directory guard
if (is_dir($file)) {
Expand All @@ -87,6 +100,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
continue;
}

// Original image guard if option --only-cache
if (!$this->isInCachePath($file) && $deleteOnlyCache) {
continue;
}

$filePath = str_replace($imageDir, "", $file);
// Filepath guard
if (empty($filePath)) {
Expand Down Expand Up @@ -157,13 +175,24 @@ protected function configure(): void
{
$this->setName(self::COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED);
$this->setDescription('Remove unused product images');

$this->addOption(
self::OPTION_INCLUDING_CACHE,
'c',
null,
'Also clear the ./cache/* entries for the corresponding images'
);
$this->addOption(
self::OPTION_ONLY_CACHE,
'k',
null,
'Clear only the ./cache/* entries for the corresponding images, but not the corresponding images'
);
$this->addOption(
self::OPTION_INCLUDING_RELATION_ENTITY,
'r',
null,
'Also clear the media not in relation table "catalog_product_entity_media_gallery_value_to_entity"'
);
$this->addOption(
self::OPTION_DRY_RUN,
'd',
Expand Down

0 comments on commit 3e18c33

Please sign in to comment.