-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added hs:imageclean:clean command to clear images using CLI (#2)
* Added hs:imageclean:clean command to clear images using CLI * Version update
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters