diff --git a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php index 516ac89e1ed..3f7f17af375 100644 --- a/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php +++ b/app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php @@ -1,13 +1,20 @@ 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,26 +225,28 @@ 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( - ' ', - '-', - 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')); - $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);