/** * @return \Magento\Framework\Controller\Result\Raw */ public function execute() { $baseTmpMediaPath = $this->mediaConfig->getBaseTmpMediaPath(); try { $remoteFileUrl = $this->getRequest()->getParam('remote_image'); $originalFileName = basename($remoteFileUrl); $localFileName = Uploader::getCorrectFileName($originalFileName); $localTmpFileName = Uploader::getDispretionPath($localFileName) . DIRECTORY_SEPARATOR . $localFileName; $localFileMediaPath = $baseTmpMediaPath . $localTmpFileName; $localUniqueFileMediaPath = $this->appendNewFileName($localFileMediaPath); $this->retrieveRemoteImage($remoteFileUrl, $localUniqueFileMediaPath); $localFileFullPath = $this->appendAbsoluteFileSystemPath($localUniqueFileMediaPath); $this->imageAdapter->validateUploadFile($localFileFullPath); $result = $this->appendResultSaveRemoteImage($localUniqueFileMediaPath); } catch (\Exception $e) { $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; } /** @var \Magento\Framework\Controller\Result\Raw $response */ $response = $this->resultRawFactory->create(); $response->setHeader('Content-type', 'text/plain'); $response->setContents(json_encode($result)); return $response; }
/** * @param string $fileName * @return string */ protected function localFileName($fileName) { $fileName = Uploader::getCorrectFileName($fileName); return $fileName; }