Skip to content

Commit 3e18c33

Browse files
authored
Merge pull request #25 from fvanzani/main
Consider the relation table media to entity when removing unused media images
2 parents 447e799 + 6f1c890 commit 3e18c33

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Console/Command/RemoveUnusedMediaCommand.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class RemoveUnusedMediaCommand extends Command
1717
{
1818
private const OPTION_DRY_RUN = 'dry-run';
1919
private const OPTION_INCLUDING_CACHE = 'including-cache';
20+
private const OPTION_ONLY_CACHE = 'only-cache';
21+
private const OPTION_INCLUDING_RELATION_ENTITY = 'including-relation';
2022
private const OPTION_FORCE = 'force';
2123
private const COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED = 'eav:media:remove-unused';
2224

@@ -44,6 +46,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
4446
$isForce = $input->getOption(self::OPTION_FORCE);
4547
$isDryRun = $input->getOption(self::OPTION_DRY_RUN);
4648
$deleteCacheAsWell = $input->getOption(self::OPTION_INCLUDING_CACHE);
49+
$deleteOnlyCache = $input->getOption(self::OPTION_ONLY_CACHE);
50+
if ($deleteOnlyCache) {
51+
$deleteCacheAsWell=true;
52+
}
53+
$deleteNotInRelation = $input->getOption(self::OPTION_INCLUDING_RELATION_ENTITY);
4754

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

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

86+
if ($deleteNotInRelation) {
87+
$mediaGalleryToEntityTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value_to_entity');
88+
$sql='SELECT value FROM ' . $mediaGalleryTable. ' where value_id IN (SELECT value_id from '.$mediaGalleryToEntityTable.')';
89+
$imagesToKeep = $connection->fetchCol($sql);
90+
}
91+
7992
foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
8093
// Directory guard
8194
if (is_dir($file)) {
@@ -87,6 +100,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
87100
continue;
88101
}
89102

103+
// Original image guard if option --only-cache
104+
if (!$this->isInCachePath($file) && $deleteOnlyCache) {
105+
continue;
106+
}
107+
90108
$filePath = str_replace($imageDir, "", $file);
91109
// Filepath guard
92110
if (empty($filePath)) {
@@ -157,13 +175,24 @@ protected function configure(): void
157175
{
158176
$this->setName(self::COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED);
159177
$this->setDescription('Remove unused product images');
160-
161178
$this->addOption(
162179
self::OPTION_INCLUDING_CACHE,
163180
'c',
164181
null,
165182
'Also clear the ./cache/* entries for the corresponding images'
166183
);
184+
$this->addOption(
185+
self::OPTION_ONLY_CACHE,
186+
'k',
187+
null,
188+
'Clear only the ./cache/* entries for the corresponding images, but not the corresponding images'
189+
);
190+
$this->addOption(
191+
self::OPTION_INCLUDING_RELATION_ENTITY,
192+
'r',
193+
null,
194+
'Also clear the media not in relation table "catalog_product_entity_media_gallery_value_to_entity"'
195+
);
167196
$this->addOption(
168197
self::OPTION_DRY_RUN,
169198
'd',

0 commit comments

Comments
 (0)