Exemple #1
0
 /**
  * Image upload action in iframe
  *
  * @return string
  */
 public function execute()
 {
     try {
         $uploader = $this->uploaderFactory->create(['fileId' => 'datafile']);
         $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
         /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
         $imageAdapter = $this->adapterFactory->create();
         $uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
         $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
         $config = $this->config;
         $result = $uploader->save($mediaDirectory->getAbsolutePath($config->getBaseTmpMediaPath()));
         $this->_eventManager->dispatch('swatch_gallery_upload_image_after', ['result' => $result, 'action' => $this]);
         unset($result['tmp_name']);
         unset($result['path']);
         $result['url'] = $this->config->getTmpMediaUrl($result['file']);
         $result['file'] = $result['file'] . '.tmp';
         $newFile = $this->swatchHelper->moveImageFromTmp($result['file']);
         $this->swatchHelper->generateSwatchVariations($newFile);
         $fileData = ['swatch_path' => $this->swatchHelper->getSwatchMediaUrl(), 'file_path' => $newFile];
         $this->getResponse()->setBody(json_encode($fileData));
     } catch (\Exception $e) {
         $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
         $this->getResponse()->setBody(json_encode($result));
     }
 }
 public function testGetSwatchMediaUrl()
 {
     $storeMock = $this->getMock('\\Magento\\Store\\Model\\Store', ['getBaseUrl'], [], '', false);
     $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
     $storeMock->expects($this->once())->method('getBaseUrl')->with('media')->willReturn('http://url/pub/media/');
     $result = $this->mediaHelperObject->getSwatchMediaUrl();
     $this->assertEquals($result, 'http://url/pub/media/attribute/swatch');
 }