Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magento/magento2-page-builder#842: Template Preview Images Incorrectly Saved to Media Directory #843

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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