/** * Save file to temp media directory * * @param string $fileId * @return array * @throws LocalizedException */ public function saveToTmp($fileId) { try { $result = $this->save($fileId, $this->imageConfig->getAbsoluteTmpMediaPath()); $result['url'] = $this->imageConfig->getTmpMediaUrl($result['file']); } catch (\Exception $e) { $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; } return $result; }
public function testBeforeSave() { $value = 'filename.jpg'; $tmpMediaPath = 'tmp/image/' . $value; $this->imageBackend->setScope('store'); $this->imageBackend->setScopeId(1); $this->imageBackend->setValue([['url' => 'http://magento2.com/pub/media/tmp/image/' . $value, 'file' => $value, 'size' => 234234]]); $this->imageConfig->expects($this->exactly(2))->method('getTmpMediaPath')->with($value)->willReturn($tmpMediaPath); $this->mediaDirectory->expects($this->once())->method('copyFile')->with($tmpMediaPath, 'image/store/1/' . $value)->willReturn(true); $this->mediaDirectory->expects($this->once())->method('delete')->with($tmpMediaPath); $this->imageBackend->beforeSave(); $this->assertEquals('store/1/filename.jpg', $this->imageBackend->getValue()); }
public function testSaveToTmp() { $path = 'design/header/logo_src'; $fieldCode = 'header_logo_src'; $metadata = [$fieldCode => ['path' => $path, 'backend_model' => 'Magento\\Theme\\Model\\Design\\Backend\\Image']]; $this->metadataProvider->expects($this->once())->method('get')->willReturn($metadata); $this->backendModelFactory->expects($this->once())->method('createByPath')->with($path)->willReturn($this->backendModel); $this->uploaderFactory->expects($this->once())->method('create')->with(['fileId' => $fieldCode])->willReturn($this->uploader); $this->uploader->expects($this->once())->method('setAllowRenameFiles')->with(true); $this->uploader->expects($this->once())->method('setFilesDispersion')->with(false); $this->backendModel->expects($this->once())->method('getAllowedExtensions')->willReturn(['png', 'jpg']); $this->uploader->expects($this->once())->method('setAllowedExtensions')->with(['png', 'jpg']); $this->uploader->expects($this->once())->method('addValidateCallback')->with('size', $this->backendModel, 'validateMaxSize'); $this->imageConfig->expects($this->once())->method('getAbsoluteTmpMediaPath')->willReturn('absolute/path/to/tmp/media'); $this->uploader->expects($this->once())->method('save')->with('absolute/path/to/tmp/media')->willReturn(['file' => 'file.jpg', 'size' => '234234']); $this->imageConfig->expects($this->once())->method('getTmpMediaUrl')->with('file.jpg')->willReturn('http://magento2.com/pub/media/tmp/file.jpg'); $this->assertEquals(['file' => 'file.jpg', 'size' => '234234', 'url' => 'http://magento2.com/pub/media/tmp/file.jpg'], $this->imageProcessor->saveToTmp($fieldCode)); }
/** * @return array */ public function afterLoad() { $value = $this->getValue(); if ($value && !is_array($value)) { $fileName = '/' . $this->uploadDir . '/' . $value; $stat = $this->_mediaDirectory->stat($fileName); $this->setValue([['url' => $this->imageConfig->getStoreMediaUrl() . $fileName, 'file' => $value, 'size' => is_array($stat) ? $stat['size'] : 0, 'exists' => true]]); } return $this; }