<?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;
use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * Initialize dependencies
     *
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Filesystem $filesystem
     * @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
     * @param \Magento\Framework\Filesystem\Driver\File $file
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\View\Result\PageFactory $resultFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\Result\PageFactory $resultFactory
    ) {
        $this->filesystem = $filesystem;
        $this->fileUploaderFactory = $fileUploaderFactory;
        $this->file = $file;
        $this->storeManager = $storeManager;
        $this->resultFactory = $resultFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        try {
            $path = $this->filesystem
                          ->getDirectoryRead(DirectoryList::MEDIA)
                          ->getAbsolutePath('ImageSearch/images/');
            $this->createDir($path);
            $allowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
            $uploader = $this->fileUploaderFactory->create(['fileId' => 'file']);
            $uploader->setAllowedExtensions($allowedExtensions);
            $imageData = $uploader->validateFile();
            $name = $imageData['name'];
            $ext = substr($name, strrpos($name, '.') + 1);
            $imageName = 'File-'.time().'.'.$ext;
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(false);
            $uploader->save($path, $imageName);
            $baseUrl = $this->storeManager->getStore()->getBaseUrl('media');
            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($baseUrl.'ImageSearch/images/'.$imageName);
            return $resultJson;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * Create path on the server to store images
     *
     * @param string $path
     * @return void
     */
    public function createDir($path)
    {
        if (!$this->file->isDirectory($path)) {
            $this->file->createDirectory($path, 0755, true);
        }
    }
}
