<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_ImageSearch
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\ImageSearch\Controller\Index;

use Magento\Framework\App\Filesystem\DirectoryList;

/**
 * Webkul ImageSearch Delete Image Controller
 */
class DeleteImage extends \Magento\Framework\App\Action\Action
{
    /**
     * Initialize dependencies
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\App\RequestInterface $request
     * @param \Magento\Framework\Filesystem $filesystem
     * @param \Magento\Framework\Filesystem\Driver\File $file
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Filesystem\Driver\File $file
    ) {
        $this->file = $file;
        $this->filesystem = $filesystem;
        parent::__construct($context);
    }

    /**
     * Perform execute function to delete the image
     */
    public function execute()
    {
        $data = $this->_request->getParam('url');
        $url = explode("/", $data);
        $url = array_pop($url);
        $path = $this->filesystem
                      ->getDirectoryRead(DirectoryList::MEDIA)
                      ->getAbsolutePath('ImageSearch/images/');
        if ($this->file->isExists($path.$url)) {
            $this->file->deleteFile($path.$url);
        }
    }
}
