Skip to content

Commit 5acfe6c

Browse files
committed
MC-38246: Support by Magento Page Builder
1 parent f262e67 commit 5acfe6c

File tree

3 files changed

+40
-228
lines changed

3 files changed

+40
-228
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
use Magento\Backend\App\Action;
1212
use Magento\Backend\App\Action\Context;
13+
use Magento\Framework\Api\ImageContent;
1314
use Magento\Framework\Api\ImageContentFactory;
1415
use Magento\Framework\Api\ImageContentValidator;
1516
use Magento\Framework\Api\SearchCriteriaBuilder;
1617
use Magento\Framework\App\Action\HttpPostActionInterface;
17-
use Magento\Framework\App\ObjectManager;
1818
use Magento\Framework\App\RequestInterface;
1919
use Magento\Framework\Controller\ResultFactory;
2020
use Magento\Framework\Exception\LocalizedException;
@@ -25,7 +25,6 @@
2525
use Magento\PageBuilder\Model\TemplateFactory;
2626
use Psr\Log\LoggerInterface;
2727
use Magento\Framework\Image\AdapterFactory;
28-
use Magento\PageBuilder\Model\ImageContentUploader;
2928

3029
/**
3130
* Save a template within template manager
@@ -63,31 +62,23 @@ class Save extends Action implements HttpPostActionInterface
6362

6463
/**
6564
* @var ImageContentValidator
66-
* @deprecated
6765
*/
6866
private $imageContentValidator;
6967

7068
/**
7169
* @var ImageContentFactory
72-
* @deprecated
7370
*/
7471
private $imageContentFactory;
7572

7673
/**
7774
* @var Database
78-
* @deprecated
7975
*/
8076
private $mediaStorage;
8177

8278
/**
8379
* @var AdapterFactory
8480
*/
8581
private $imageAdapterFactory;
86-
87-
/**
88-
* @var ImageContentUploader
89-
*/
90-
private $contentUploader;
9182

9283
/**
9384
* @param Context $context
@@ -100,7 +91,6 @@ class Save extends Action implements HttpPostActionInterface
10091
* @param ImageContentFactory $imageContentFactory
10192
* @param Database $mediaStorage
10293
* @param AdapterFactory $imageAdapterFactory
103-
* @param ImageContentUploader|null $contentUploader
10494
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
10595
*/
10696
public function __construct(
@@ -113,8 +103,7 @@ public function __construct(
113103
ImageContentValidator $imageContentValidator,
114104
ImageContentFactory $imageContentFactory,
115105
Database $mediaStorage,
116-
AdapterFactory $imageAdapterFactory,
117-
ImageContentUploader $contentUploader = null
106+
AdapterFactory $imageAdapterFactory
118107
) {
119108
parent::__construct($context);
120109

@@ -127,7 +116,6 @@ public function __construct(
127116
$this->imageContentFactory = $imageContentFactory;
128117
$this->mediaStorage = $mediaStorage;
129118
$this->imageAdapterFactory = $imageAdapterFactory;
130-
$this->contentUploader = $contentUploader ?? ObjectManager::getInstance()->get(ImageContentUploader::class);
131119
}
132120

133121
/**
@@ -230,36 +218,54 @@ private function validate(RequestInterface $request)
230218
* @return string
231219
* @throws LocalizedException
232220
* @throws \Magento\Framework\Exception\FileSystemException
233-
* @throws \Exception
221+
* @throws \Magento\Framework\Exception\InputException
234222
*/
235223
private function storePreviewImage(RequestInterface $request) : ?string
236224
{
237225
$mediaDir = $this->filesystem
238226
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
239227
$fileName = preg_replace("/[^A-Za-z0-9]/", '', str_replace(
240-
' ',
241-
'-',
242-
strtolower($request->getParam(TemplateInterface::KEY_NAME))
243-
)) . uniqid() . '.jpg';
244-
$fileDirectoryPath = $mediaDir->getAbsolutePath('.template-manager');
228+
' ',
229+
'-',
230+
strtolower($request->getParam(TemplateInterface::KEY_NAME))
231+
)) . uniqid() . '.jpg';
232+
$filePath = '.template-manager' . DIRECTORY_SEPARATOR . $fileName;
245233

246234
// Prepare the image data
247235
$imgData = str_replace(' ', '+', $request->getParam('previewImage'));
248236
$imgData = substr($imgData, strpos($imgData, ",") + 1);
249-
250-
$uploadedImage = $this->contentUploader->upload($fileName, $imgData, $fileDirectoryPath);
251-
$filePath = $fileDirectoryPath . $uploadedImage;
252-
$relativeFilePath = $mediaDir->getRelativePath($filePath);
253-
if ($relativeFilePath === null) {
254-
throw new LocalizedException(__('Unable to retrieve image.'));
237+
// phpcs:ignore
238+
$decodedImage = base64_decode($imgData);
239+
240+
$imageProperties = getimagesizefromstring($decodedImage);
241+
if (!$imageProperties) {
242+
throw new LocalizedException(__('Unable to get properties from image.'));
243+
}
244+
245+
/* @var ImageContent $imageContent */
246+
$imageContent = $this->imageContentFactory->create();
247+
$imageContent->setBase64EncodedData($imgData);
248+
$imageContent->setName($fileName);
249+
$imageContent->setType($imageProperties['mime']);
250+
251+
if ($this->imageContentValidator->isValid($imageContent)) {
252+
$absolutePath = $mediaDir->getAbsolutePath() . $filePath;
253+
// Write the file to the directory
254+
$mediaDir->getDriver()->filePutContents($absolutePath, $decodedImage);
255+
// Generate a thumbnail, called -thumb next to the image for usage in the grid
256+
$thumbPath = str_replace('.jpg', '-thumb.jpg', $filePath);
257+
$thumbAbsolutePath = $mediaDir->getAbsolutePath() . $thumbPath;
258+
$imageFactory = $this->imageAdapterFactory->create();
259+
$imageFactory->open($absolutePath);
260+
$imageFactory->resize(350);
261+
$imageFactory->save($thumbAbsolutePath);
262+
$this->mediaStorage->saveFile($filePath);
263+
$this->mediaStorage->saveFile($thumbPath);
264+
265+
// Store the preview image within the new entity
266+
return $filePath;
255267
}
256-
$thumbPath = str_replace('.jpg', '-thumb.jpg', $relativeFilePath);
257-
$thumbAbsolutePath = $mediaDir->getAbsolutePath() . $thumbPath;
258-
$imageFactory = $this->imageAdapterFactory->create();
259-
$imageFactory->open($filePath);
260-
$imageFactory->resize(350);
261-
$imageFactory->save($thumbAbsolutePath);
262-
263-
return $relativeFilePath;
268+
269+
return null;
264270
}
265271
}

app/code/Magento/PageBuilder/Model/ImageContentUploader.php

Lines changed: 0 additions & 103 deletions
This file was deleted.

app/code/Magento/PageBuilder/Test/Unit/Model/ImageContentUploaderTest.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)