diff --git a/Console/Command/Clean.php b/Console/Command/Clean.php new file mode 100644 index 0000000..ab95357 --- /dev/null +++ b/Console/Command/Clean.php @@ -0,0 +1,72 @@ +_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(); + } +} \ No newline at end of file diff --git a/Observer/Register.php b/Observer/Register.php index a84c1ac..50ddb07 100644 --- a/Observer/Register.php +++ b/Observer/Register.php @@ -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'); } -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index 3757cc3..a6ed7e6 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "hs/module-all": "*" }, "type": "magento2-module", - "version": "1.0.0", + "version": "1.1.0", "license": [ "proprietary" ], diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..dea472e --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,10 @@ + + + + + + HS\ImageClean\Console\Command\Clean + + + + \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index f7db583..a57e27c 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +