Skip to content

Commit c093733

Browse files
authored
Revert "Make sure image metadata is not fetched from remote storage every time"
1 parent 39914f7 commit c093733

File tree

45 files changed

+1637
-1571
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1637
-1571
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.php

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@
1313
*/
1414
namespace Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery;
1515

16-
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
16+
use Magento\Framework\App\ObjectManager;
1717
use Magento\Backend\Block\Media\Uploader;
18+
use Magento\Framework\View\Element\AbstractBlock;
1819
use Magento\Framework\App\Filesystem\DirectoryList;
19-
use Magento\Framework\App\ObjectManager;
2020
use Magento\Framework\Exception\FileSystemException;
21-
use Magento\Framework\Storage\FileNotFoundException;
22-
use Magento\Framework\Storage\StorageProvider;
23-
use Magento\Framework\View\Element\AbstractBlock;
21+
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
2422
use Magento\MediaStorage\Helper\File\Storage\Database;
2523

2624
/**
2725
* Block for gallery content.
28-
*
29-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3026
*/
3127
class Content extends \Magento\Backend\Block\Widget
3228
{
@@ -59,21 +55,11 @@ class Content extends \Magento\Backend\Block\Widget
5955
* @var Database
6056
*/
6157
private $fileStorageDatabase;
62-
/**
63-
* @var StorageProvider
64-
*/
65-
private $storageProvider;
66-
67-
/**
68-
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
69-
*/
70-
private $mediaDirectory;
7158

7259
/**
7360
* @param \Magento\Backend\Block\Template\Context $context
7461
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
7562
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
76-
* @param StorageProvider $storageProvider
7763
* @param array $data
7864
* @param ImageUploadConfigDataProvider $imageUploadConfigDataProvider
7965
* @param Database $fileStorageDatabase
@@ -82,20 +68,17 @@ public function __construct(
8268
\Magento\Backend\Block\Template\Context $context,
8369
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
8470
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
85-
StorageProvider $storageProvider,
8671
array $data = [],
8772
ImageUploadConfigDataProvider $imageUploadConfigDataProvider = null,
8873
Database $fileStorageDatabase = null
8974
) {
9075
$this->_jsonEncoder = $jsonEncoder;
9176
$this->_mediaConfig = $mediaConfig;
9277
parent::__construct($context, $data);
93-
$this->mediaDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
9478
$this->imageUploadConfigDataProvider = $imageUploadConfigDataProvider
9579
?: ObjectManager::getInstance()->get(ImageUploadConfigDataProvider::class);
9680
$this->fileStorageDatabase = $fileStorageDatabase
9781
?: ObjectManager::getInstance()->get(Database::class);
98-
$this->storageProvider = $storageProvider;
9982
}
10083

10184
/**
@@ -174,49 +157,10 @@ public function getAddImagesButton()
174157
);
175158
}
176159

177-
/**
178-
* Sync images to database
179-
*
180-
* @param string $fileName
181-
*/
182-
private function syncImageToDatabase(string $fileName): void
183-
{
184-
if ($this->fileStorageDatabase->checkDbUsage() &&
185-
!$this->mediaDirectory->isFile($this->_mediaConfig->getMediaPath($fileName))
186-
) {
187-
$this->fileStorageDatabase->saveFileToFilesystem(
188-
$this->_mediaConfig->getMediaPath($fileName)
189-
);
190-
}
191-
}
192-
193-
/**
194-
* Returns file metadata as an associative array
195-
*
196-
* @param string $fileName
197-
* @return array
198-
* @throws FileNotFoundException
199-
*/
200-
private function getFileMetadata(string $fileName): array
201-
{
202-
$metadata = [];
203-
try {
204-
$info = $this->storageProvider->get('media')
205-
->getMetadata($this->_mediaConfig->getMediaPath($fileName));
206-
$metadata['size'] = $info['size'];
207-
} catch (FileSystemException $e) {
208-
$metadata['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('small_image');
209-
$metadata['size'] = 0;
210-
$this->_logger->warning($e);
211-
}
212-
return $metadata;
213-
}
214-
215160
/**
216161
* Returns image json
217162
*
218163
* @return string
219-
* @throws FileNotFoundException
220164
*/
221165
public function getImagesJson()
222166
{
@@ -226,14 +170,24 @@ public function getImagesJson()
226170
is_array($value['images']) &&
227171
count($value['images'])
228172
) {
173+
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
229174
$images = $this->sortImagesByPosition($value['images']);
230175
foreach ($images as &$image) {
231176
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
232-
$this->syncImageToDatabase($image['file']);
233-
if (isset($image['image_metadata']) && is_array($image['image_metadata'])) {
234-
$image = array_replace_recursive($image, $image['image_metadata']);
235-
} else {
236-
$image = array_replace_recursive($image, $this->getFileMetadata($image['file']));
177+
if ($this->fileStorageDatabase->checkDbUsage() &&
178+
!$mediaDir->isFile($this->_mediaConfig->getMediaPath($image['file']))
179+
) {
180+
$this->fileStorageDatabase->saveFileToFilesystem(
181+
$this->_mediaConfig->getMediaPath($image['file'])
182+
);
183+
}
184+
try {
185+
$fileHandler = $mediaDir->stat($this->_mediaConfig->getMediaPath($image['file']));
186+
$image['size'] = $fileHandler['size'];
187+
} catch (FileSystemException $e) {
188+
$image['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('small_image');
189+
$image['size'] = 0;
190+
$this->_logger->warning($e);
237191
}
238192
}
239193
return $this->_jsonEncoder->encode($images);

app/code/Magento/Catalog/Block/Product/Gallery.php

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
*/
1212
namespace Magento\Catalog\Block\Product;
1313

14-
use Magento\Framework\Storage\FileNotFoundException;
1514
use Magento\Catalog\Model\Product;
16-
use Magento\Catalog\Model\Product\Media\Config;
17-
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\App\Filesystem\DirectoryList;
1816
use Magento\Framework\Data\Collection;
19-
use Magento\Framework\Registry;
20-
use Magento\Framework\Storage\StorageProvider;
2117

2218
/**
2319
* Product gallery block
@@ -30,37 +26,22 @@ class Gallery extends \Magento\Framework\View\Element\Template
3026
/**
3127
* Core registry
3228
*
33-
* @var Registry
29+
* @var \Magento\Framework\Registry
3430
*/
3531
protected $_coreRegistry = null;
3632

37-
/**
38-
* @var StorageProvider
39-
*/
40-
private $storageProvider;
41-
/**
42-
* @var Config
43-
*/
44-
private $mediaConfig;
45-
4633
/**
4734
* @param \Magento\Framework\View\Element\Template\Context $context
48-
* @param Registry $registry
35+
* @param \Magento\Framework\Registry $registry
4936
* @param array $data
50-
* @param StorageProvider $storageProvider
51-
* @param Config $mediaConfig
5237
*/
5338
public function __construct(
5439
\Magento\Framework\View\Element\Template\Context $context,
55-
Registry $registry,
56-
array $data = [],
57-
StorageProvider $storageProvider = null,
58-
Config $mediaConfig = null
40+
\Magento\Framework\Registry $registry,
41+
array $data = []
5942
) {
6043
$this->_coreRegistry = $registry;
6144
parent::__construct($context, $data);
62-
$this->storageProvider = $storageProvider ?? ObjectManager::getInstance()->get(StorageProvider::class);
63-
$this->mediaConfig = $mediaConfig ?? ObjectManager::getInstance()->get(Config::class);
6445
}
6546

6647
/**
@@ -140,24 +121,16 @@ public function getImageFile()
140121
*/
141122
public function getImageWidth()
142123
{
143-
$file = $this->getCurrentImage()->getFile();
144-
if (!$file) {
145-
return false;
146-
}
147-
$productMediaFile = $this->mediaConfig->getMediaPath($file);
148-
149-
$mediaStorage = $this->storageProvider->get('media');
150-
if ($mediaStorage->has($productMediaFile)) {
151-
try {
152-
$meta = $mediaStorage->getMetadata($productMediaFile);
153-
$size = $meta['size'];
154-
if ($size > 600) {
124+
$file = $this->getCurrentImage()->getPath();
125+
126+
if ($this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->isFile($file)) {
127+
$size = getimagesize($file);
128+
if (isset($size[0])) {
129+
if ($size[0] > 600) {
155130
return 600;
156131
} else {
157-
return (int) $size;
132+
return (int) $size[0];
158133
}
159-
} catch (FileNotFoundException $e) {
160-
return false;
161134
}
162135
}
163136

app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\Storage\StorageProvider;
11+
use Magento\Framework\Exception\LocalizedException;
1212

1313
/**
1414
* Upload product image action controller
@@ -52,23 +52,16 @@ class Upload extends \Magento\Backend\App\Action implements HttpPostActionInterf
5252
*/
5353
private $productMediaConfig;
5454

55-
/**
56-
* @var StorageProvider
57-
*/
58-
private $storageProvider;
59-
6055
/**
6156
* @param \Magento\Backend\App\Action\Context $context
6257
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
63-
* @param StorageProvider $storageProvider
6458
* @param \Magento\Framework\Image\AdapterFactory $adapterFactory
6559
* @param \Magento\Framework\Filesystem $filesystem
6660
* @param \Magento\Catalog\Model\Product\Media\Config $productMediaConfig
6761
*/
6862
public function __construct(
6963
\Magento\Backend\App\Action\Context $context,
7064
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
71-
StorageProvider $storageProvider,
7265
\Magento\Framework\Image\AdapterFactory $adapterFactory = null,
7366
\Magento\Framework\Filesystem $filesystem = null,
7467
\Magento\Catalog\Model\Product\Media\Config $productMediaConfig = null
@@ -81,7 +74,6 @@ public function __construct(
8174
->get(\Magento\Framework\Filesystem::class);
8275
$this->productMediaConfig = $productMediaConfig ?: ObjectManager::getInstance()
8376
->get(\Magento\Catalog\Model\Product\Media\Config::class);
84-
$this->storageProvider = $storageProvider;
8577
}
8678

8779
/**
@@ -92,7 +84,6 @@ public function __construct(
9284
public function execute()
9385
{
9486
try {
95-
/** @var \Magento\MediaStorage\Model\File\Uploader $uploader */
9687
$uploader = $this->_objectManager->create(
9788
\Magento\MediaStorage\Model\File\Uploader::class,
9889
['fileId' => 'image']
@@ -102,18 +93,11 @@ public function execute()
10293
$uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
10394
$uploader->setAllowRenameFiles(true);
10495
$uploader->setFilesDispersion(true);
105-
10696
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
107-
$baseImagePath = $this->productMediaConfig->getBaseTmpMediaPath();
10897
$result = $uploader->save(
109-
$mediaDirectory->getAbsolutePath($baseImagePath)
98+
$mediaDirectory->getAbsolutePath($this->productMediaConfig->getBaseTmpMediaPath())
11099
);
111100

112-
$origFile = $this->productMediaConfig->getTmpMediaPath($result['file']);
113-
$storage = $this->storageProvider->get('media');
114-
$content = $mediaDirectory->readFile($origFile);
115-
$storage->put($origFile, $content);
116-
117101
$this->_eventManager->dispatch(
118102
'catalog_product_gallery_upload_image_after',
119103
['result' => $result, 'action' => $this]

app/code/Magento/Catalog/Helper/Image.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\View\Element\Block\ArgumentInterface;
1414

1515
/**
16-
* Catalog image helper
16+
* Catalog image helper.
1717
*
1818
* @api
1919
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -166,8 +166,7 @@ public function __construct(
166166
$this->_assetRepo = $assetRepo;
167167
$this->viewConfig = $viewConfig;
168168
$this->viewAssetPlaceholderFactory = $placeholderFactory
169-
?: ObjectManager::getInstance()
170-
->get(PlaceholderFactory::class);
169+
?: ObjectManager::getInstance()->get(PlaceholderFactory::class);
171170
$this->mediaConfig = $mediaConfig ?: ObjectManager::getInstance()->get(CatalogMediaConfig::class);
172171
}
173172

@@ -574,9 +573,6 @@ public function save()
574573
* Return resized product image information
575574
*
576575
* @return array
577-
* @deprecated Magento is not responsible for image resizing anymore. This method works with local filesystem only.
578-
* Service that provides resized images should guarantee that the image sizes correspond to requested ones.
579-
* Use `getWidth()` and `getHeight()` instead.
580576
*/
581577
public function getResizedImageInfo()
582578
{

app/code/Magento/Catalog/Model/Config/Source/Web/CatalogMediaUrlFormat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function toOptionArray()
2424
'value' => CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS,
2525
'label' => __('Image optimization based on query parameters')
2626
],
27-
['value' => CatalogMediaConfig::HASH, 'label' => __('Legacy mode (unique hash per image variant)')]
27+
['value' => CatalogMediaConfig::HASH, 'label' => __('Unique hash per image variant (Legacy mode)')]
2828
];
2929
}
3030
}

0 commit comments

Comments
 (0)