/var/www/html/project/app/code/Embitel/ImageCleanup/Cron/Clean.php
/**
* Copyright © All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace EmbitelImageCleanupCron;
use MagentoFrameworkExceptionFileSystemException;
use MagentoFrameworkFilesystemDirectoryWriteInterface;
use MagentoFrameworkAppFilesystemDirectoryList;
class Clean
{
/**
* @var EmbitelImageCleanupModelimageFactory
*/
public $_imageFactory;
const IMAGE_CLEANUP_CRON = 'imagecleanup/general/enable';
public function __construct(
EmbitelImageCleanupModelImageFactory $ImageFactory,
DirectoryList $directoryList,
MagentoFrameworkFilesystemDriverFile $file,
MagentoFrameworkFilesystem $filesystem,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
) {
$this->_imageFactory = $ImageFactory;
$this->file = $file;
$this->directoryList = $directoryList;
$this->filesystem = $filesystem;
$this->scopeConfig = $scopeConfig;
}
/**
* {@inheritdoc}
*/
public function execute()
{
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$enable = $this->scopeConfig->getValue(self::IMAGE_CLEANUP_CRON, $storeScope);
if ($enable) {
$mediaRootDir = $this->directoryList->getPath('media');
$linkedImagePathCollection = $this->_imageFactory->create()->getCollection();
$linkedImagePathCollection->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(['value'])
->joinLeft(
['emgve' => $linkedImagePathCollection->getTable('catalog_product_entity_media_gallery_value_to_entity')],
'main_table.value_id=emgve.value_id',
[]
)->where('emgve.value_id IS NOT NULL');
$unlinkedImageCollection = $this->_imageFactory->create()->getCollection();
$unlinkedImageCollection->getSelect()->where(
'main_table.value NOT IN (
' . $linkedImagePathCollection->getSelect() . '
)'
);
$varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$varPath = $this->directoryList->getPath('var');
$fileName = '_image.txt';
$path = $varPath . date("Y-m-d") . $fileName;
foreach ($unlinkedImageCollection as $image) {
$imagePath = $mediaRootDir . '/catalog/product' . $image->getValue();
$this->write($varDirectory, $path, $imagePath);
if ($this->file->isExists($imagePath)) {
$this->file->deleteFile($imagePath);
}
$image->delete();
}
}
}
public function write(WriteInterface $writeDirectory, string $filePath, string $imagePath)
{
$stream = $writeDirectory->openFile($filePath, 'w+');
$stream->lock();
$fileData = $imagePath . "<br>";
$stream->write($fileData);
$stream->unlock();
$stream->close();
return true;
}
}```
controller-:
/var/www/html/project/app/code/Embitel/ImageCleanup/Controller/Index/TestCron.php
```<?php
namespace EmbitelImageCleanupControllerIndex;
use MagentoBackendAppAction;
use MagentoBackendAppActionContext;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkControllerResultRaw;
use EmbitelImageCleanupCronClean;
class TestCron extends Action
{
/**
* @var Clean
*/
private $cleanCron;
public function __construct(
Context $context,
Clean $cleanCron
) {
parent::__construct($context);
$this->cleanCron = $cleanCron;
}
/**
* Execute controller action.
*/
public function execute()
{
$this->cleanCron->execute();
$this->getResponse()->setBody('Deleted Image Files cron job executed successfully.');
}
}```
```Route-:
/var/www/html/project/app/code/Embitel/ImageCleanup/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="ImageCleanup" frontName="ImageCleanup">
<module name="Embitel_ImageCleanup" />
</route>
</router>
</config>```