Skip to content

Commit

Permalink
Merge pull request #5305 from magento-architects/ECP-98
Browse files Browse the repository at this point in the history
Fixed issues:
- ECP-207 Make sure image metadata is not fetched from remote storage every time
  • Loading branch information
slavvka authored Mar 5, 2020
2 parents 5a9fcab + 0d3d432 commit 39914f7
Show file tree
Hide file tree
Showing 45 changed files with 1,572 additions and 1,638 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
*/
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery;

use Magento\Framework\App\ObjectManager;
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
use Magento\Backend\Block\Media\Uploader;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\FileSystemException;
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
use Magento\Framework\Storage\FileNotFoundException;
use Magento\Framework\Storage\StorageProvider;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\MediaStorage\Helper\File\Storage\Database;

/**
* Block for gallery content.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Content extends \Magento\Backend\Block\Widget
{
Expand Down Expand Up @@ -55,11 +59,21 @@ class Content extends \Magento\Backend\Block\Widget
* @var Database
*/
private $fileStorageDatabase;
/**
* @var StorageProvider
*/
private $storageProvider;

/**
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
*/
private $mediaDirectory;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
* @param StorageProvider $storageProvider
* @param array $data
* @param ImageUploadConfigDataProvider $imageUploadConfigDataProvider
* @param Database $fileStorageDatabase
Expand All @@ -68,17 +82,20 @@ public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
StorageProvider $storageProvider,
array $data = [],
ImageUploadConfigDataProvider $imageUploadConfigDataProvider = null,
Database $fileStorageDatabase = null
) {
$this->_jsonEncoder = $jsonEncoder;
$this->_mediaConfig = $mediaConfig;
parent::__construct($context, $data);
$this->mediaDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
$this->imageUploadConfigDataProvider = $imageUploadConfigDataProvider
?: ObjectManager::getInstance()->get(ImageUploadConfigDataProvider::class);
$this->fileStorageDatabase = $fileStorageDatabase
?: ObjectManager::getInstance()->get(Database::class);
$this->storageProvider = $storageProvider;
}

/**
Expand Down Expand Up @@ -157,10 +174,49 @@ public function getAddImagesButton()
);
}

/**
* Sync images to database
*
* @param string $fileName
*/
private function syncImageToDatabase(string $fileName): void
{
if ($this->fileStorageDatabase->checkDbUsage() &&
!$this->mediaDirectory->isFile($this->_mediaConfig->getMediaPath($fileName))
) {
$this->fileStorageDatabase->saveFileToFilesystem(
$this->_mediaConfig->getMediaPath($fileName)
);
}
}

/**
* Returns file metadata as an associative array
*
* @param string $fileName
* @return array
* @throws FileNotFoundException
*/
private function getFileMetadata(string $fileName): array
{
$metadata = [];
try {
$info = $this->storageProvider->get('media')
->getMetadata($this->_mediaConfig->getMediaPath($fileName));
$metadata['size'] = $info['size'];
} catch (FileSystemException $e) {
$metadata['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('small_image');
$metadata['size'] = 0;
$this->_logger->warning($e);
}
return $metadata;
}

/**
* Returns image json
*
* @return string
* @throws FileNotFoundException
*/
public function getImagesJson()
{
Expand All @@ -170,24 +226,14 @@ public function getImagesJson()
is_array($value['images']) &&
count($value['images'])
) {
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
$images = $this->sortImagesByPosition($value['images']);
foreach ($images as &$image) {
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
if ($this->fileStorageDatabase->checkDbUsage() &&
!$mediaDir->isFile($this->_mediaConfig->getMediaPath($image['file']))
) {
$this->fileStorageDatabase->saveFileToFilesystem(
$this->_mediaConfig->getMediaPath($image['file'])
);
}
try {
$fileHandler = $mediaDir->stat($this->_mediaConfig->getMediaPath($image['file']));
$image['size'] = $fileHandler['size'];
} catch (FileSystemException $e) {
$image['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('small_image');
$image['size'] = 0;
$this->_logger->warning($e);
$this->syncImageToDatabase($image['file']);
if (isset($image['image_metadata']) && is_array($image['image_metadata'])) {
$image = array_replace_recursive($image, $image['image_metadata']);
} else {
$image = array_replace_recursive($image, $this->getFileMetadata($image['file']));
}
}
return $this->_jsonEncoder->encode($images);
Expand Down
51 changes: 39 additions & 12 deletions app/code/Magento/Catalog/Block/Product/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
*/
namespace Magento\Catalog\Block\Product;

use Magento\Framework\Storage\FileNotFoundException;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Catalog\Model\Product\Media\Config;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Collection;
use Magento\Framework\Registry;
use Magento\Framework\Storage\StorageProvider;

/**
* Product gallery block
Expand All @@ -26,22 +30,37 @@ class Gallery extends \Magento\Framework\View\Element\Template
/**
* Core registry
*
* @var \Magento\Framework\Registry
* @var Registry
*/
protected $_coreRegistry = null;

/**
* @var StorageProvider
*/
private $storageProvider;
/**
* @var Config
*/
private $mediaConfig;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param Registry $registry
* @param array $data
* @param StorageProvider $storageProvider
* @param Config $mediaConfig
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
array $data = []
Registry $registry,
array $data = [],
StorageProvider $storageProvider = null,
Config $mediaConfig = null
) {
$this->_coreRegistry = $registry;
parent::__construct($context, $data);
$this->storageProvider = $storageProvider ?? ObjectManager::getInstance()->get(StorageProvider::class);
$this->mediaConfig = $mediaConfig ?? ObjectManager::getInstance()->get(Config::class);
}

/**
Expand Down Expand Up @@ -121,16 +140,24 @@ public function getImageFile()
*/
public function getImageWidth()
{
$file = $this->getCurrentImage()->getPath();

if ($this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->isFile($file)) {
$size = getimagesize($file);
if (isset($size[0])) {
if ($size[0] > 600) {
$file = $this->getCurrentImage()->getFile();
if (!$file) {
return false;
}
$productMediaFile = $this->mediaConfig->getMediaPath($file);

$mediaStorage = $this->storageProvider->get('media');
if ($mediaStorage->has($productMediaFile)) {
try {
$meta = $mediaStorage->getMetadata($productMediaFile);
$size = $meta['size'];
if ($size > 600) {
return 600;
} else {
return (int) $size[0];
return (int) $size;
}
} catch (FileNotFoundException $e) {
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Storage\StorageProvider;

/**
* Upload product image action controller
Expand Down Expand Up @@ -52,16 +52,23 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf
*/
private $productMediaConfig;

/**
* @var StorageProvider
*/
private $storageProvider;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @param StorageProvider $storageProvider
* @param \Magento\Framework\Image\AdapterFactory $adapterFactory
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Catalog\Model\Product\Media\Config $productMediaConfig
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
StorageProvider $storageProvider,
\Magento\Framework\Image\AdapterFactory $adapterFactory = null,
\Magento\Framework\Filesystem $filesystem = null,
\Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null
Expand All @@ -74,6 +81,7 @@ public function __construct(
->get(\Magento\Framework\Filesystem::class);
$this->productMediaConfig = $productMediaConfig ?: ObjectManager::getInstance()
->get(\Magento\Catalog\Model\Product\Media\Config::class);
$this->storageProvider = $storageProvider;
}

/**
Expand All @@ -84,6 +92,7 @@ public function __construct(
public function execute()
{
try {
/** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
$uploader = $this->_objectManager->create(
\Magento\MediaStorage\Model\File\Uploader::class,
['fileId' => 'image']
Expand All @@ -93,11 +102,18 @@ public function execute()
$uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);

$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$baseImagePath = $this->productMediaConfig->getBaseTmpMediaPath();
$result = $uploader->save(
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
$mediaDirectory->getAbsolutePath($baseImagePath)
);

$origFile = $this->productMediaConfig->getTmpMediaPath($result['file']);
$storage = $this->storageProvider->get('media');
$content = $mediaDirectory->readFile($origFile);
$storage->put($origFile, $content);

$this->_eventManager->dispatch(
'catalog_product_gallery_upload_image_after',
['result' => $result, 'action' => $this]
Expand Down
8 changes: 6 additions & 2 deletions app/code/Magento/Catalog/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Framework\View\Element\Block\ArgumentInterface;

/**
* Catalog image helper.
* Catalog image helper
*
* @api
* @SuppressWarnings(PHPMD.TooManyFields)
Expand Down Expand Up @@ -166,7 +166,8 @@ public function __construct(
$this->_assetRepo = $assetRepo;
$this->viewConfig = $viewConfig;
$this->viewAssetPlaceholderFactory = $placeholderFactory
?: ObjectManager::getInstance()->get(PlaceholderFactory::class);
?: ObjectManager::getInstance()
->get(PlaceholderFactory::class);
$this->mediaConfig = $mediaConfig ?: ObjectManager::getInstance()->get(CatalogMediaConfig::class);
}

Expand Down Expand Up @@ -573,6 +574,9 @@ public function save()
* Return resized product image information
*
* @return array
* @deprecated Magento is not responsible for image resizing anymore. This method works with local filesystem only.
* Service that provides resized images should guarantee that the image sizes correspond to requested ones.
* Use `getWidth()` and `getHeight()` instead.
*/
public function getResizedImageInfo()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function toOptionArray()
'value' => CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS,
'label' => __('Image optimization based on query parameters')
],
['value' => CatalogMediaConfig::HASH, 'label' => __('Unique hash per image variant (Legacy mode)')]
['value' => CatalogMediaConfig::HASH, 'label' => __('Legacy mode (unique hash per image variant)')]
];
}
}
Loading

0 comments on commit 39914f7

Please sign in to comment.