Files
2026-09-15 10:35:56 +08:00

47 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\ImageManager\Controller\Adminhtml\Image;
use Esun\ImageManager\Model\ImageFactory;
use Esun\ImageManager\Model\MediaUploader;
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
class Delete extends Action
{
public const ADMIN_RESOURCE = 'Esun_ImageManager::images_delete';
public function __construct(
Action\Context $context,
private readonly ImageFactory $imageFactory,
private readonly MediaUploader $mediaUploader
) {
parent::__construct($context);
}
public function execute()
{
try {
$image = $this->imageFactory->create()->load((int) $this->getRequest()->getParam('id'));
if (!$image->getId()) {
throw new LocalizedException(__('The image no longer exists.'));
}
$paths = [
(string) $image->getData('thumbnail_path'),
(string) $image->getData('image_path'),
(string) $image->getData('icon_path'),
];
$image->delete();
foreach ($paths as $path) {
$this->mediaUploader->delete($path);
}
$this->messageManager->addSuccessMessage(__('The image has been deleted.'));
} catch (\Throwable $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
return $this->resultRedirectFactory->create()->setPath('esun_image_manager/image/index');
}
}