Skip to content

Commit

Permalink
magento#842: Template Preview Images Incorrectly Saved to Media Direc…
Browse files Browse the repository at this point in the history
…tory

- 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
  • Loading branch information
bluemwhitew committed Feb 1, 2023
1 parent 47efafd commit 9fd0895
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions app/code/Magento/PageBuilder/Controller/Adminhtml/Template/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@

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;
use Magento\Framework\Api\ImageContentFactory;
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
Expand Down Expand Up @@ -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(
Expand All @@ -176,7 +186,7 @@ public function execute()
'status' => 'error',
'message' => $e->getMessage()
];
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->critical($e);

$result = [
Expand Down Expand Up @@ -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(
' ',
Expand All @@ -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.'));
}
Expand All @@ -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);
Expand Down

0 comments on commit 9fd0895

Please sign in to comment.