Skip to content

Commit ff50b32

Browse files
Merge branch 'develop' into PR-11-14-2024
2 parents 2b52b7e + 226509e commit ff50b32

File tree

1 file changed

+32
-20
lines changed
  • app/code/Magento/PageBuilder/Controller/Adminhtml/Template

1 file changed

+32
-20
lines changed

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

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* All Rights Reserved.
55
*/
66

77
declare(strict_types=1);
88

99
namespace Magento\PageBuilder\Controller\Adminhtml\Template;
1010

11+
use function preg_replace;
12+
use function str_replace;
13+
use function strpos;
14+
use function strtolower;
15+
use function substr;
16+
use function uniqid;
17+
use Exception;
1118
use Magento\Backend\App\Action;
1219
use Magento\Backend\App\Action\Context;
1320
use Magento\Framework\Api\ImageContent;
1421
use Magento\Framework\Api\ImageContentFactory;
1522
use Magento\Framework\Api\ImageContentValidator;
1623
use Magento\Framework\Api\SearchCriteriaBuilder;
1724
use Magento\Framework\App\Action\HttpPostActionInterface;
25+
use Magento\Framework\App\Filesystem\DirectoryList;
1826
use Magento\Framework\App\RequestInterface;
1927
use Magento\Framework\Controller\ResultFactory;
28+
use Magento\Framework\Exception\FileSystemException;
29+
use Magento\Framework\Exception\InputException;
2030
use Magento\Framework\Exception\LocalizedException;
2131
use Magento\Framework\Filesystem;
32+
use Magento\Framework\Image\AdapterFactory;
2233
use Magento\MediaStorage\Helper\File\Storage\Database;
2334
use Magento\PageBuilder\Api\Data\TemplateInterface;
2435
use Magento\PageBuilder\Api\TemplateRepositoryInterface;
2536
use Magento\PageBuilder\Model\TemplateFactory;
2637
use Psr\Log\LoggerInterface;
27-
use Magento\Framework\Image\AdapterFactory;
2838

2939
/**
3040
* Save a template within template manager
@@ -33,7 +43,7 @@
3343
*/
3444
class Save extends Action implements HttpPostActionInterface
3545
{
36-
const ADMIN_RESOURCE = 'Magento_PageBuilder::template_save';
46+
public const ADMIN_RESOURCE = 'Magento_PageBuilder::template_save';
3747

3848
/**
3949
* @var LoggerInterface
@@ -151,7 +161,7 @@ public function execute()
151161
$filePath = $this->storePreviewImage($request);
152162
// Store the preview image within the new entity
153163
$template->setPreviewImage($filePath);
154-
} catch (\Exception $e) {
164+
} catch (Exception $e) {
155165
$this->logger->critical($e);
156166

157167
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData(
@@ -176,7 +186,7 @@ public function execute()
176186
'status' => 'error',
177187
'message' => $e->getMessage()
178188
];
179-
} catch (\Exception $e) {
189+
} catch (Exception $e) {
180190
$this->logger->critical($e);
181191

182192
$result = [
@@ -215,26 +225,28 @@ private function validate(RequestInterface $request)
215225
* Handle storing the preview image
216226
*
217227
* @param RequestInterface $request
218-
* @return string
228+
* @return null|string
229+
* @throws Exception
230+
* @throws FileSystemException
231+
* @throws InputException
219232
* @throws LocalizedException
220-
* @throws \Magento\Framework\Exception\FileSystemException
221-
* @throws \Magento\Framework\Exception\InputException
222233
*/
223-
private function storePreviewImage(RequestInterface $request) : ?string
234+
private function storePreviewImage(RequestInterface $request): ?string
224235
{
225236
$fileName = preg_replace("/[^A-Za-z0-9]/", '', str_replace(
226-
' ',
227-
'-',
228-
strtolower($request->getParam(TemplateInterface::KEY_NAME))
229-
)) . uniqid() . '.jpg';
237+
' ',
238+
'-',
239+
strtolower($request->getParam(TemplateInterface::KEY_NAME))
240+
)) . uniqid() . '.jpg';
230241

231242
// Prepare the image data
232243
$imgData = str_replace(' ', '+', $request->getParam('previewImage'));
233-
$imgData = substr($imgData, strpos($imgData, ",") + 1);
234-
// phpcs:ignore
244+
$imgData = substr($imgData, strpos($imgData, ',') + 1);
245+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
235246
$decodedImage = base64_decode($imgData);
236247

237248
$imageProperties = getimagesizefromstring($decodedImage);
249+
238250
if (!$imageProperties) {
239251
throw new LocalizedException(__('Unable to get properties from image.'));
240252
}
@@ -246,16 +258,16 @@ private function storePreviewImage(RequestInterface $request) : ?string
246258
$imageContent->setType($imageProperties['mime']);
247259

248260
if ($this->imageContentValidator->isValid($imageContent)) {
249-
$mediaDirWrite = $this->filesystem
250-
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
261+
$mediaDirWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
251262
$directory = $mediaDirWrite->getAbsolutePath('.template-manager');
252263
$mediaDirWrite->create($directory);
253-
$fileAbsolutePath = $directory . $fileName;
264+
265+
$fileAbsolutePath = $directory . DIRECTORY_SEPARATOR . $fileName;
254266
// Write the file to the directory
255267
$mediaDirWrite->getDriver()->filePutContents($fileAbsolutePath, $decodedImage);
256268
// Generate a thumbnail, called -thumb next to the image for usage in the grid
257269
$thumbPath = str_replace('.jpg', '-thumb.jpg', $fileName);
258-
$thumbAbsolutePath = $directory . $thumbPath;
270+
$thumbAbsolutePath = $directory . DIRECTORY_SEPARATOR . $thumbPath;
259271
$imageFactory = $this->imageAdapterFactory->create();
260272
$imageFactory->open($fileAbsolutePath);
261273
$imageFactory->resize(350);

0 commit comments

Comments
 (0)