Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RetrieveImage.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
13 
17 class RetrieveImage extends \Magento\Backend\App\Action implements HttpPostActionInterface
18 {
24  const ADMIN_RESOURCE = 'Magento_Catalog::products';
25 
29  protected $resultRawFactory;
30 
34  protected $mediaConfig;
35 
39  protected $fileSystem;
40 
44  protected $imageAdapter;
45 
49  protected $curl;
50 
54  protected $fileUtility;
55 
61  private $protocolValidator;
62 
66  private $extensionValidator;
67 
79  public function __construct(
80  \Magento\Backend\App\Action\Context $context,
81  \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
82  \Magento\Catalog\Model\Product\Media\Config $mediaConfig,
83  \Magento\Framework\Filesystem $fileSystem,
84  \Magento\Framework\Image\AdapterFactory $imageAdapterFactory,
85  \Magento\Framework\HTTP\Adapter\Curl $curl,
86  \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility,
87  \Magento\Framework\Validator\ValidatorInterface $protocolValidator = null,
88  \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $extensionValidator = null
89  ) {
90  parent::__construct($context);
91  $this->resultRawFactory = $resultRawFactory;
92  $this->mediaConfig = $mediaConfig;
93  $this->fileSystem = $fileSystem;
94  $this->imageAdapter = $imageAdapterFactory->create();
95  $this->curl = $curl;
96  $this->fileUtility = $fileUtility;
97  $this->extensionValidator = $extensionValidator
99  ->get(\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension::class);
100  $this->protocolValidator = $protocolValidator ?:
102  ->get(\Magento\Framework\Validator\ValidatorInterface::class);
103  }
104 
108  public function execute()
109  {
110  $baseTmpMediaPath = $this->mediaConfig->getBaseTmpMediaPath();
111  try {
112  $remoteFileUrl = $this->getRequest()->getParam('remote_image');
113  $this->validateRemoteFile($remoteFileUrl);
114  $localFileName = Uploader::getCorrectFileName(basename($remoteFileUrl));
115  $localTmpFileName = Uploader::getDispersionPath($localFileName) . DIRECTORY_SEPARATOR . $localFileName;
116  $localFilePath = $baseTmpMediaPath . ($localTmpFileName);
117  $localUniqFilePath = $this->appendNewFileName($localFilePath);
118  $this->validateRemoteFileExtensions($localUniqFilePath);
119  $this->retrieveRemoteImage($remoteFileUrl, $localUniqFilePath);
120  $localFileFullPath = $this->appendAbsoluteFileSystemPath($localUniqFilePath);
121  $this->imageAdapter->validateUploadFile($localFileFullPath);
122  $result = $this->appendResultSaveRemoteImage($localUniqFilePath);
123  } catch (\Exception $e) {
124  $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
125  $fileWriter = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
126  if (isset($localFileFullPath) && $fileWriter->isExist($localFileFullPath)) {
127  $fileWriter->delete($localFileFullPath);
128  }
129  }
130 
132  $response = $this->resultRawFactory->create();
133  $response->setHeader('Content-type', 'text/plain');
134  $response->setContents(json_encode($result));
135  return $response;
136  }
137 
146  private function validateRemoteFile($remoteFileUrl)
147  {
148  if (!$this->protocolValidator->isValid($remoteFileUrl)) {
149  throw new LocalizedException(
150  __("Protocol isn't allowed")
151  );
152  }
153 
154  return $this;
155  }
156 
164  private function validateRemoteFileExtensions($filePath)
165  {
166  $extension = pathinfo($filePath, PATHINFO_EXTENSION);
167  if (!$this->extensionValidator->isValid($extension)) {
168  throw new \Magento\Framework\Exception\ValidatorException(__('Disallowed file type.'));
169  }
170  }
171 
177  {
178  $fileInfo = pathinfo($fileName);
179  $tmpFileName = Uploader::getDispersionPath($fileInfo['basename']) . DIRECTORY_SEPARATOR . $fileInfo['basename'];
180  $result['name'] = $fileInfo['basename'];
181  $result['type'] = $this->imageAdapter->getMimeType();
182  $result['error'] = 0;
183  $result['size'] = filesize($this->appendAbsoluteFileSystemPath($fileName));
184  $result['url'] = $this->mediaConfig->getTmpMediaUrl($tmpFileName);
185  $result['file'] = $tmpFileName;
186  return $result;
187  }
188 
197  protected function retrieveRemoteImage($fileUrl, $localFilePath)
198  {
199  $this->curl->setConfig(['header' => false]);
200  $this->curl->write('GET', $fileUrl);
201  $image = $this->curl->read();
202  if (empty($image)) {
203  throw new LocalizedException(
204  __('The preview image information is unavailable. Check your connection and try again.')
205  );
206  }
207  $this->fileUtility->saveFile($localFilePath, $image);
208  }
209 
214  protected function appendNewFileName($localFilePath)
215  {
216  $destinationFile = $this->appendAbsoluteFileSystemPath($localFilePath);
217  $fileName = Uploader::getNewFileName($destinationFile);
218  $fileInfo = pathinfo($localFilePath);
219  return $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileName;
220  }
221 
226  protected function appendAbsoluteFileSystemPath($localTmpFile)
227  {
229  $mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
230  $pathToSave = $mediaDirectory->getAbsolutePath();
231  return $pathToSave . $localTmpFile;
232  }
233 }
$response
Definition: 404.php:11
static getCorrectFileName($fileName)
Definition: Uploader.php:375
__()
Definition: __.php:13
$fileName
Definition: translate.phtml:15
$mediaDirectory
static getNewFileName($destinationFile)
Definition: Uploader.php:605
$baseTmpMediaPath
Definition: products.php:25
static getDispersionPath($fileName)
Definition: Uploader.php:642