Skip to content

Commit

Permalink
Revert "Make sure image metadata is not fetched from remote storage e…
Browse files Browse the repository at this point in the history
…very time"
  • Loading branch information
akaplya committed Mar 5, 2020
1 parent 39914f7 commit c093733
Show file tree
Hide file tree
Showing 45 changed files with 1,637 additions and 1,571 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
*/
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery;

use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
use Magento\Framework\App\ObjectManager;
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\Framework\Storage\FileNotFoundException;
use Magento\Framework\Storage\StorageProvider;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
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 @@ -59,21 +55,11 @@ 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 @@ -82,20 +68,17 @@ 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 @@ -174,49 +157,10 @@ 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 @@ -226,14 +170,24 @@ 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']);
$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']));
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);
}
}
return $this->_jsonEncoder->encode($images);
Expand Down
51 changes: 12 additions & 39 deletions app/code/Magento/Catalog/Block/Product/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
*/
namespace Magento\Catalog\Block\Product;

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

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

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

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param Registry $registry
* @param \Magento\Framework\Registry $registry
* @param array $data
* @param StorageProvider $storageProvider
* @param Config $mediaConfig
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Registry $registry,
array $data = [],
StorageProvider $storageProvider = null,
Config $mediaConfig = null
\Magento\Framework\Registry $registry,
array $data = []
) {
$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 @@ -140,24 +121,16 @@ public function getImageFile()
*/
public function getImageWidth()
{
$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) {
$file = $this->getCurrentImage()->getPath();

if ($this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->isFile($file)) {
$size = getimagesize($file);
if (isset($size[0])) {
if ($size[0] > 600) {
return 600;
} else {
return (int) $size;
return (int) $size[0];
}
} 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\Storage\StorageProvider;
use Magento\Framework\Exception\LocalizedException;

/**
* Upload product image action controller
Expand Down Expand Up @@ -52,23 +52,16 @@ 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 @@ -81,7 +74,6 @@ 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 @@ -92,7 +84,6 @@ 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 @@ -102,18 +93,11 @@ 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($baseImagePath)
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
);

$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: 2 additions & 6 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,8 +166,7 @@ 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 @@ -574,9 +573,6 @@ 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' => __('Legacy mode (unique hash per image variant)')]
['value' => CatalogMediaConfig::HASH, 'label' => __('Unique hash per image variant (Legacy mode)')]
];
}
}
Loading

0 comments on commit c093733

Please sign in to comment.