|
| 1 | +<?php |
| 2 | +namespace Cloudinary\Cloudinary\Controller\Adminhtml\Ajax; |
| 3 | + |
| 4 | +use Cloudinary\Api\BaseApiClient; |
| 5 | +use Cloudinary\Cloudinary\Core\ConfigurationInterface; |
| 6 | +use Cloudinary\Cloudinary\Core\ConfigurationBuilder; |
| 7 | +use Cloudinary\Cloudinary\Core\Image\ImageFactory; |
| 8 | +use Cloudinary\Cloudinary\Core\UrlGenerator; |
| 9 | +use Cloudinary\Configuration\Configuration; |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Store\Model\StoreManagerInterface; |
| 12 | +use Magento\Framework\UrlInterface; |
| 13 | +use Magento\Backend\App\Action; |
| 14 | +use Magento\Framework\Controller\Result\RawFactory as ResultRawFactory; |
| 15 | +use Magento\Backend\App\Action\Context; |
| 16 | +use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent; |
| 17 | +use Magento\Framework\Filesystem as FileSysten; |
| 18 | +use Magento\Catalog\Helper\Image as CatalogImageHelper; |
| 19 | +use Cloudinary\Cloudinary\Core\Image; |
| 20 | +use Cloudinary\Asset\Media; |
| 21 | +use Cloudinary\Cloudinary\Core\Image\Transformation; |
| 22 | + |
| 23 | +class UpdateAdminImage extends Action |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ConfigurationInterface |
| 27 | + */ |
| 28 | + protected $configuration; |
| 29 | + /** |
| 30 | + * @var UrlGenerator |
| 31 | + */ |
| 32 | + protected $urlGenerator; |
| 33 | + /** |
| 34 | + * @var ImageFactory |
| 35 | + */ |
| 36 | + protected $imageFactory; |
| 37 | + /** |
| 38 | + * @var StoreManagerInterface |
| 39 | + */ |
| 40 | + protected $storeManager; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var UrlInterface |
| 44 | + */ |
| 45 | + protected $urlInterface; |
| 46 | + |
| 47 | + protected $resultFactory; |
| 48 | + |
| 49 | + protected $imageContent; |
| 50 | + |
| 51 | + protected $filesystem; |
| 52 | + |
| 53 | + private $_authorised; |
| 54 | + |
| 55 | + protected $configurationBuilder; |
| 56 | + |
| 57 | + protected $transformation; |
| 58 | + |
| 59 | + /** |
| 60 | + * @param ImageFactory $imageFactory |
| 61 | + * @param UrlGenerator $urlGenerator |
| 62 | + * @param ConfigurationInterface $configuration |
| 63 | + * @param StoreManagerInterface $storeManager |
| 64 | + * @param UrlInterface $urlInterface |
| 65 | + */ |
| 66 | + public function __construct( |
| 67 | + Context $context, |
| 68 | + ImageFactory $imageFactory, |
| 69 | + UrlGenerator $urlGenerator, |
| 70 | + ConfigurationInterface $configuration, |
| 71 | + StoreManagerInterface $storeManager, |
| 72 | + UrlInterface $urlInterface, |
| 73 | + ResultRawFactory $resultFactory, |
| 74 | + FileSysten $filesystem, |
| 75 | + ConfigurationBuilder $configurationBuilder, |
| 76 | + Transformation $transformation |
| 77 | + ) { |
| 78 | + parent::__construct($context); |
| 79 | + $this->imageFactory = $imageFactory; |
| 80 | + $this->urlGenerator = $urlGenerator; |
| 81 | + $this->configuration = $configuration; |
| 82 | + $this->storeManager = $storeManager; |
| 83 | + $this->urlInterface = $urlInterface; |
| 84 | + $this->resultFactory = $resultFactory; |
| 85 | + $this->filesystem = $filesystem; |
| 86 | + $this->configurationBuilder = $configurationBuilder; |
| 87 | + $this->transformation = $transformation; |
| 88 | + } |
| 89 | + |
| 90 | + private function authorise() |
| 91 | + { |
| 92 | + if (!$this->_authorised && $this->configuration->isEnabled()) { |
| 93 | + Configuration::instance($this->configurationBuilder->build()); |
| 94 | + BaseApiClient::$userPlatform = $this->configuration->getUserPlatform(); |
| 95 | + $this->_authorised = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public function execute() |
| 100 | + { |
| 101 | + $this->authorise(); |
| 102 | + if ($this->configuration->isEnabled()) { |
| 103 | + try{ |
| 104 | + $remoteImageUrl = $this->getRequest()->getParam('remote_image'); |
| 105 | + $filedId = str_replace($this->storeManager->getStore()->getBaseUrl(), '', $remoteImageUrl); |
| 106 | + |
| 107 | + $result = Media::fromParams( |
| 108 | + $filedId, |
| 109 | + [ 'transformation' => $this->transformation->build(), |
| 110 | + 'secure' => true, |
| 111 | + 'sign_url' => $this->configuration->getUseSignedUrls(), |
| 112 | + 'version' => 1 |
| 113 | + ] |
| 114 | + ) . '?_i=AB'; |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + } catch (\Exception $e) { |
| 120 | + $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + $response = $this->resultFactory->create(); |
| 126 | + $response->setHeader('Content-type', 'text/plain'); |
| 127 | + $response->setContents(json_encode($result)); |
| 128 | + return $response; |
| 129 | + } |
| 130 | +} |
0 commit comments