Skip to content

Commit

Permalink
Added hs:imageclean:clean command to clear images using CLI (#2)
Browse files Browse the repository at this point in the history
* Added hs:imageclean:clean command to clear images using CLI

* Version update
  • Loading branch information
easeq authored Jan 6, 2021
1 parent faea2d5 commit 622d7e6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
72 changes: 72 additions & 0 deletions Console/Command/Clean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace HS\ImageClean\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class Clean extends Command
{
/**
* @var \HS\ImageClean\Model\imageFactory
*/
protected $_imageFactory;

public function __construct(
\HS\ImageClean\Model\ImageFactory $ImageFactory,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Filesystem\Driver\File $file
) {
$this->_imageFactory = $ImageFactory;
$this->file = $file;
$this->directoryList = $directoryList;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function execute(
InputInterface $input,
OutputInterface $output
) {
$mediaRootDir = $this->directoryList->getPath('media');

$collection = $this->_imageFactory->create()->getCollection();
$collection->getSelect()
->joinLeft(
['emgve' => $collection->getTable('catalog_product_entity_media_gallery_value_to_entity')],
'main_table.value_id=emgve.value_id',
[]
)->where('emgve.value_id IS NULL');

ProgressBar::setFormatDefinition('custom', ' %current%/%max% -- %message%');
$progressBar = new ProgressBar($output, $collection->getSize());
$progressBar->setFormat('custom');
$progressBar->start();
foreach ($collection as $image) {
$filePath = $mediaRootDir.'/catalog/product'.$image->getValue();
$progressBar->setMessage('Deleting ' . $filePath . '...');
if ($this->file->isExists($filePath)) {
$this->file->deleteFile($filePath);
}
$image->delete();
$progressBar->advance();
}
$progressBar->finish();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName("hs:imageclean:clean");
$this->setDescription("Clean unused product images");
parent::configure();
}
}
4 changes: 2 additions & 2 deletions Observer/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function __construct(
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->helper->register('HS_ImageClean', '1.0.0', 'confirm');
$this->helper->register('HS_ImageClean', '1.1.0', 'confirm');
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"hs/module-all": "*"
},
"type": "magento2-module",
"version": "1.0.0",
"version": "1.1.0",
"license": [
"proprietary"
],
Expand Down
10 changes: 10 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="clean" xsi:type="object">HS\ImageClean\Console\Command\Clean</item>
</argument>
</arguments>
</type>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="HS_ImageClean" setup_version="1.0.0">
<module name="HS_ImageClean" setup_version="1.1.0">
<sequence>
<module name="HS_All"/>
</sequence>
Expand Down

0 comments on commit 622d7e6

Please sign in to comment.