61 private $protocolValidator;
66 private $extensionValidator;
84 \
Magento\Framework\Image\AdapterFactory $imageAdapterFactory,
88 \
Magento\MediaStorage\Model\File\
Validator\NotProtectedExtension $extensionValidator =
null 90 parent::__construct($context);
94 $this->imageAdapter = $imageAdapterFactory->create();
97 $this->extensionValidator = $extensionValidator
99 ->get(\
Magento\MediaStorage\Model\File\
Validator\NotProtectedExtension::class);
100 $this->protocolValidator = $protocolValidator ?:
112 $remoteFileUrl = $this->
getRequest()->getParam(
'remote_image');
113 $this->validateRemoteFile($remoteFileUrl);
118 $this->validateRemoteFileExtensions($localUniqFilePath);
120 $localFileFullPath = $this->appendAbsoluteFileSystemPath($localUniqFilePath);
121 $this->imageAdapter->validateUploadFile($localFileFullPath);
123 }
catch (\Exception $e) {
124 $result = [
'error' => $e->getMessage(),
'errorcode' => $e->getCode()];
126 if (isset($localFileFullPath) && $fileWriter->isExist($localFileFullPath)) {
127 $fileWriter->delete($localFileFullPath);
132 $response = $this->resultRawFactory->create();
133 $response->setHeader(
'Content-type',
'text/plain');
146 private function validateRemoteFile($remoteFileUrl)
148 if (!$this->protocolValidator->isValid($remoteFileUrl)) {
149 throw new LocalizedException(
150 __(
"Protocol isn't allowed")
164 private function validateRemoteFileExtensions($filePath)
166 $extension = pathinfo($filePath, PATHINFO_EXTENSION);
167 if (!$this->extensionValidator->isValid(
$extension)) {
168 throw new \Magento\Framework\Exception\ValidatorException(
__(
'Disallowed file type.'));
181 $result[
'type'] = $this->imageAdapter->getMimeType();
184 $result[
'url'] = $this->mediaConfig->getTmpMediaUrl($tmpFileName);
185 $result[
'file'] = $tmpFileName;
199 $this->curl->setConfig([
'header' =>
false]);
200 $this->curl->write(
'GET', $fileUrl);
201 $image = $this->curl->read();
204 __(
'The preview image information is unavailable. Check your connection and try again.')
207 $this->fileUtility->saveFile($localFilePath,
$image);
216 $destinationFile = $this->appendAbsoluteFileSystemPath($localFilePath);
226 protected function appendAbsoluteFileSystemPath($localTmpFile)
231 return $pathToSave . $localTmpFile;
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Controller\Result\RawFactory $resultRawFactory, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Framework\Filesystem $fileSystem, \Magento\Framework\Image\AdapterFactory $imageAdapterFactory, \Magento\Framework\HTTP\Adapter\Curl $curl, \Magento\MediaStorage\Model\ResourceModel\File\Storage\File $fileUtility, \Magento\Framework\Validator\ValidatorInterface $protocolValidator=null, \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $extensionValidator=null)
appendNewFileName($localFilePath)
static getCorrectFileName($fileName)
retrieveRemoteImage($fileUrl, $localFilePath)
appendResultSaveRemoteImage($fileName)
static getNewFileName($destinationFile)
static getDispersionPath($fileName)