From 9fd0895e218531600cc11c9e2ab92bf91d5b1d55 Mon Sep 17 00:00:00 2001 From: Matt Walters Date: Wed, 1 Feb 2023 22:28:17 +0000 Subject: [PATCH 1/3] magento/magento2-page-builder#842: Template Preview Images Incorrectly Saved to Media Directory - Adding `use function` for `preg_replace`, `str_replace`, `strpos`, `strtolower`, `substr`, and `uniqid` - Adding Missing `DIRECTORY_SEPARATOR` - Adding Missing `@throws` - Fixing `@return` Type(s) - Making `phpcs:ignore` Less Generic --- .../Controller/Adminhtml/Template/Save.php | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php index 516ac89e1ed..3e7c32a69c2 100644 --- a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php +++ b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php @@ -8,6 +8,13 @@ namespace Magento\PageBuilder\Controller\Adminhtml\Template; +use function preg_replace; +use function str_replace; +use function strpos; +use function strtolower; +use function substr; +use function uniqid; +use Exception; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\Api\ImageContent; @@ -15,16 +22,19 @@ use Magento\Framework\Api\ImageContentValidator; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\App\Action\HttpPostActionInterface; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\RequestInterface; use Magento\Framework\Controller\ResultFactory; +use Magento\Framework\Exception\FileSystemException; +use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem; +use Magento\Framework\Image\AdapterFactory; use Magento\MediaStorage\Helper\File\Storage\Database; use Magento\PageBuilder\Api\Data\TemplateInterface; use Magento\PageBuilder\Api\TemplateRepositoryInterface; use Magento\PageBuilder\Model\TemplateFactory; use Psr\Log\LoggerInterface; -use Magento\Framework\Image\AdapterFactory; /** * Save a template within template manager @@ -151,7 +161,7 @@ public function execute() $filePath = $this->storePreviewImage($request); // Store the preview image within the new entity $template->setPreviewImage($filePath); - } catch (\Exception $e) { + } catch (Exception $e) { $this->logger->critical($e); return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData( @@ -176,7 +186,7 @@ public function execute() 'status' => 'error', 'message' => $e->getMessage() ]; - } catch (\Exception $e) { + } catch (Exception $e) { $this->logger->critical($e); $result = [ @@ -215,12 +225,13 @@ private function validate(RequestInterface $request) * Handle storing the preview image * * @param RequestInterface $request - * @return string + * @return null|string + * @throws Exception + * @throws FileSystemException + * @throws InputException * @throws LocalizedException - * @throws \Magento\Framework\Exception\FileSystemException - * @throws \Magento\Framework\Exception\InputException */ - private function storePreviewImage(RequestInterface $request) : ?string + private function storePreviewImage(RequestInterface $request): ?string { $fileName = preg_replace("/[^A-Za-z0-9]/", '', str_replace( ' ', @@ -230,11 +241,12 @@ private function storePreviewImage(RequestInterface $request) : ?string // Prepare the image data $imgData = str_replace(' ', '+', $request->getParam('previewImage')); - $imgData = substr($imgData, strpos($imgData, ",") + 1); - // phpcs:ignore + $imgData = substr($imgData, strpos($imgData, ',') + 1); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $decodedImage = base64_decode($imgData); $imageProperties = getimagesizefromstring($decodedImage); + if (!$imageProperties) { throw new LocalizedException(__('Unable to get properties from image.')); } @@ -246,16 +258,16 @@ private function storePreviewImage(RequestInterface $request) : ?string $imageContent->setType($imageProperties['mime']); if ($this->imageContentValidator->isValid($imageContent)) { - $mediaDirWrite = $this->filesystem - ->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA); + $mediaDirWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); $directory = $mediaDirWrite->getAbsolutePath('.template-manager'); $mediaDirWrite->create($directory); - $fileAbsolutePath = $directory . $fileName; + + $fileAbsolutePath = $directory . DIRECTORY_SEPARATOR . $fileName; // Write the file to the directory $mediaDirWrite->getDriver()->filePutContents($fileAbsolutePath, $decodedImage); // Generate a thumbnail, called -thumb next to the image for usage in the grid $thumbPath = str_replace('.jpg', '-thumb.jpg', $fileName); - $thumbAbsolutePath = $directory . $thumbPath; + $thumbAbsolutePath = $directory . DIRECTORY_SEPARATOR . $thumbPath; $imageFactory = $this->imageAdapterFactory->create(); $imageFactory->open($fileAbsolutePath); $imageFactory->resize(350); From 8a36f42d0b02151b97704cf983f08b06ba749f02 Mon Sep 17 00:00:00 2001 From: engcom-Hotel Date: Wed, 23 Oct 2024 13:48:52 +0530 Subject: [PATCH 2/3] Fix static test failures --- .../PageBuilder/Controller/Adminhtml/Template/Save.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php index 3e7c32a69c2..5808179f1f4 100644 --- a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php +++ b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php @@ -43,7 +43,7 @@ */ class Save extends Action implements HttpPostActionInterface { - const ADMIN_RESOURCE = 'Magento_PageBuilder::template_save'; + public const ADMIN_RESOURCE = 'Magento_PageBuilder::template_save'; /** * @var LoggerInterface @@ -234,10 +234,10 @@ private function validate(RequestInterface $request) private function storePreviewImage(RequestInterface $request): ?string { $fileName = preg_replace("/[^A-Za-z0-9]/", '', str_replace( - ' ', - '-', - strtolower($request->getParam(TemplateInterface::KEY_NAME)) - )) . uniqid() . '.jpg'; + ' ', + '-', + strtolower($request->getParam(TemplateInterface::KEY_NAME)) + )) . uniqid() . '.jpg'; // Prepare the image data $imgData = str_replace(' ', '+', $request->getParam('previewImage')); From 3941c9948e10f6ab0b91f9e05bcde30b4b666f82 Mon Sep 17 00:00:00 2001 From: engcom-hotel Date: Wed, 23 Oct 2024 18:49:10 +0530 Subject: [PATCH 3/3] Modify copywrite text --- .../PageBuilder/Controller/Adminhtml/Template/Save.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php index 5808179f1f4..3f7f17af375 100644 --- a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php +++ b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php @@ -1,7 +1,7 @@