Skip to content

Commit 00a170a

Browse files
authored
[performance] MC-38137 Custom option (#6275)
1 parent 7928751 commit 00a170a

File tree

7 files changed

+57
-47
lines changed

7 files changed

+57
-47
lines changed

app/code/Magento/Bundle/Model/Product/Type.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Catalog\Api\ProductRepositoryInterface;
1414
use Magento\Framework\App\ObjectManager;
1515
use Magento\Framework\EntityManager\MetadataPool;
16+
use Magento\Framework\File\UploaderFactory;
1617
use Magento\Framework\Pricing\PriceCurrencyInterface;
1718
use Magento\Framework\Serialize\Serializer\Json;
1819
use Magento\Framework\Stdlib\ArrayUtils;
@@ -190,11 +191,11 @@ class Type extends \Magento\Catalog\Model\Product\Type\AbstractType
190191
* @param PriceCurrencyInterface $priceCurrency
191192
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
192193
* @param \Magento\CatalogInventory\Api\StockStateInterface $stockState
193-
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
194+
* @param Json|null $serializer
194195
* @param MetadataPool|null $metadataPool
195196
* @param SelectionCollectionFilterApplier|null $selectionCollectionFilterApplier
196197
* @param ArrayUtils|null $arrayUtility
197-
*
198+
* @param UploaderFactory $uploaderFactory
198199
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
199200
*/
200201
public function __construct(
@@ -222,7 +223,8 @@ public function __construct(
222223
Json $serializer = null,
223224
MetadataPool $metadataPool = null,
224225
SelectionCollectionFilterApplier $selectionCollectionFilterApplier = null,
225-
ArrayUtils $arrayUtility = null
226+
ArrayUtils $arrayUtility = null,
227+
UploaderFactory $uploaderFactory = null
226228
) {
227229
$this->_catalogProduct = $catalogProduct;
228230
$this->_catalogData = $catalogData;
@@ -254,7 +256,8 @@ public function __construct(
254256
$coreRegistry,
255257
$logger,
256258
$productRepository,
257-
$serializer
259+
$serializer,
260+
$uploaderFactory
258261
);
259262
}
260263

app/code/Magento/Catalog/Model/Product/Option/Type/File/ValidatorFile.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\Math\Random;
1414
use Magento\Framework\App\ObjectManager;
15+
use Magento\MediaStorage\Model\File\Uploader;
1516

1617
/**
1718
* Validator class. Represents logic for validation file given from product option
@@ -173,15 +174,11 @@ public function validate($processingParams, $option)
173174
$userValue = [];
174175

175176
if ($upload->isUploaded($file) && $upload->isValid($file)) {
176-
$fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($fileInfo['name']);
177-
$dispersion = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
178-
179-
$filePath = $dispersion;
180-
181177
$tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
182-
$fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name'])));
183178
$fileRandomName = $this->random->getRandomString(32);
184-
$filePath .= '/' .$fileRandomName;
179+
$fileName = Uploader::getCorrectFileName($fileRandomName);
180+
$dispersion = Uploader::getDispersionPath($fileName);
181+
$filePath = $dispersion . '/' . $fileName;
185182
$fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath);
186183

187184
$upload->addFilter(new \Zend_Filter_File_Rename(['target' => $fileFullPath, 'overwrite' => true]));
@@ -216,6 +213,8 @@ public function validate($processingParams, $option)
216213
}
217214
}
218215

216+
$fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name'])));
217+
219218
$userValue = [
220219
'type' => $fileInfo['type'],
221220
'title' => $fileInfo['name'],

app/code/Magento/Catalog/Model/Product/Type/AbstractType.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
namespace Magento\Catalog\Model\Product\Type;
88

99
use Magento\Catalog\Api\ProductRepositoryInterface;
10-
use Magento\Framework\App\Filesystem\DirectoryList;
1110
use Magento\Framework\Exception\LocalizedException;
1211
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\File\UploaderFactory;
1313

1414
/**
1515
* Abstract model for product type implementation
@@ -113,6 +113,11 @@ abstract class AbstractType
113113
*/
114114
protected $_cacheProductSetAttributes = '_cache_instance_product_set_attributes';
115115

116+
/**
117+
* @var UploaderFactory
118+
*/
119+
private $uploaderFactory;
120+
116121
/**
117122
* Delete data specific for this product type
118123
*
@@ -175,8 +180,6 @@ abstract public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $
175180
protected $serializer;
176181

177182
/**
178-
* Construct
179-
*
180183
* @param \Magento\Catalog\Model\Product\Option $catalogProductOption
181184
* @param \Magento\Eav\Model\Config $eavConfig
182185
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
@@ -187,6 +190,7 @@ abstract public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $
187190
* @param \Psr\Log\LoggerInterface $logger
188191
* @param ProductRepositoryInterface $productRepository
189192
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
193+
* @param UploaderFactory $uploaderFactory
190194
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191195
*/
192196
public function __construct(
@@ -199,7 +203,8 @@ public function __construct(
199203
\Magento\Framework\Registry $coreRegistry,
200204
\Psr\Log\LoggerInterface $logger,
201205
ProductRepositoryInterface $productRepository,
202-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
206+
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
207+
UploaderFactory $uploaderFactory = null
203208
) {
204209
$this->_catalogProductOption = $catalogProductOption;
205210
$this->_eavConfig = $eavConfig;
@@ -212,6 +217,7 @@ public function __construct(
212217
$this->productRepository = $productRepository;
213218
$this->serializer = $serializer ?: ObjectManager::getInstance()
214219
->get(\Magento\Framework\Serialize\Serializer\Json::class);
220+
$this->uploaderFactory = $uploaderFactory ?: ObjectManager::getInstance()->get(UploaderFactory::class);
215221
}
216222

217223
/**
@@ -493,28 +499,20 @@ public function processFileQueue()
493499
if (isset($queueOptions['operation']) && ($operation = $queueOptions['operation'])) {
494500
switch ($operation) {
495501
case 'receive_uploaded_file':
496-
$src = isset($queueOptions['src_name']) ? $queueOptions['src_name'] : '';
497-
$dst = isset($queueOptions['dst_name']) ? $queueOptions['dst_name'] : '';
502+
$src = $queueOptions['src_name'] ?? '';
503+
$dst = $queueOptions['dst_name'] ?? '';
498504
/** @var $uploader \Zend_File_Transfer_Adapter_Http */
499-
$uploader = isset($queueOptions['uploader']) ? $queueOptions['uploader'] : null;
500-
501-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
502-
$path = dirname($dst);
503-
504-
try {
505-
$rootDir = $this->_filesystem->getDirectoryWrite(
506-
DirectoryList::ROOT
507-
);
508-
$rootDir->create($rootDir->getRelativePath($path));
509-
} catch (\Magento\Framework\Exception\FileSystemException $e) {
510-
throw new \Magento\Framework\Exception\LocalizedException(
511-
__('We can\'t create the "%1" writeable directory.', $path)
512-
);
505+
$uploader = $queueOptions['uploader'] ?? null;
506+
$isUploaded = false;
507+
if ($uploader && $uploader->isValid()) {
508+
$path = pathinfo($dst, PATHINFO_DIRNAME);
509+
$uploader = $this->uploaderFactory->create(['fileId' => $src]);
510+
$uploader->setFilesDispersion(false);
511+
$uploader->setAllowRenameFiles(true);
512+
$isUploaded = $uploader->save($path, pathinfo($dst, PATHINFO_FILENAME));
513513
}
514514

515-
$uploader->setDestination($path);
516-
517-
if (empty($src) || empty($dst) || !$uploader->receive($src)) {
515+
if (empty($src) || empty($dst) || !$isUploaded) {
518516
/**
519517
* @todo: show invalid option
520518
*/

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\App\ObjectManager;
1818
use Magento\Framework\EntityManager\MetadataPool;
1919
use Magento\Framework\Api\SearchCriteriaBuilder;
20+
use Magento\Framework\File\UploaderFactory;
2021

2122
/**
2223
* Configurable product type implementation
@@ -235,11 +236,12 @@ class Configurable extends \Magento\Catalog\Model\Product\Type\AbstractType
235236
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
236237
* @param \Magento\Framework\Cache\FrontendInterface|null $cache
237238
* @param \Magento\Customer\Model\Session|null $customerSession
238-
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
239-
* @param ProductInterfaceFactory $productFactory
240-
* @param SalableProcessor $salableProcessor
239+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
240+
* @param ProductInterfaceFactory|null $productFactory
241+
* @param SalableProcessor|null $salableProcessor
241242
* @param ProductAttributeRepositoryInterface|null $productAttributeRepository
242243
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
244+
* @param UploaderFactory|null $uploaderFactory
243245
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
244246
*/
245247
public function __construct(
@@ -266,7 +268,8 @@ public function __construct(
266268
ProductInterfaceFactory $productFactory = null,
267269
SalableProcessor $salableProcessor = null,
268270
ProductAttributeRepositoryInterface $productAttributeRepository = null,
269-
SearchCriteriaBuilder $searchCriteriaBuilder = null
271+
SearchCriteriaBuilder $searchCriteriaBuilder = null,
272+
UploaderFactory $uploaderFactory = null
270273
) {
271274
$this->typeConfigurableFactory = $typeConfigurableFactory;
272275
$this->_eavAttributeFactory = $eavAttributeFactory;
@@ -295,7 +298,8 @@ public function __construct(
295298
$coreRegistry,
296299
$logger,
297300
$productRepository,
298-
$serializer
301+
$serializer,
302+
$uploaderFactory
299303
);
300304
}
301305

app/code/Magento/Downloadable/Model/Product/Type.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Api\ProductRepositoryInterface;
99
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
10+
use Magento\Framework\File\UploaderFactory;
1011

1112
/**
1213
* Downloadable product type model
@@ -67,8 +68,6 @@ class Type extends \Magento\Catalog\Model\Product\Type\Virtual
6768
private $extensionAttributesJoinProcessor;
6869

6970
/**
70-
* Construct
71-
*
7271
* @param \Magento\Catalog\Model\Product\Option $catalogProductOption
7372
* @param \Magento\Eav\Model\Config $eavConfig
7473
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
@@ -87,6 +86,7 @@ class Type extends \Magento\Catalog\Model\Product\Type\Virtual
8786
* @param TypeHandler\TypeHandlerInterface $typeHandler
8887
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
8988
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
89+
* @param UploaderFactory|null $uploaderFactory
9090
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9191
*/
9292
public function __construct(
@@ -107,7 +107,8 @@ public function __construct(
107107
\Magento\Downloadable\Model\LinkFactory $linkFactory,
108108
\Magento\Downloadable\Model\Product\TypeHandler\TypeHandlerInterface $typeHandler,
109109
JoinProcessorInterface $extensionAttributesJoinProcessor,
110-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
110+
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
111+
UploaderFactory $uploaderFactory = null
111112
) {
112113
$this->_sampleResFactory = $sampleResFactory;
113114
$this->_linkResource = $linkResource;
@@ -127,7 +128,8 @@ public function __construct(
127128
$coreRegistry,
128129
$logger,
129130
$productRepository,
130-
$serializer
131+
$serializer,
132+
$uploaderFactory
131133
);
132134
}
133135

app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\GroupedProduct\Model\Product\Type;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\File\UploaderFactory;
1112

1213
/**
1314
* Grouped product type model
@@ -102,6 +103,7 @@ class Grouped extends \Magento\Catalog\Model\Product\Type\AbstractType
102103
* @param \Magento\Framework\App\State $appState
103104
* @param \Magento\Msrp\Helper\Data $msrpData
104105
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
106+
* @param UploaderFactory|null $uploaderFactory
105107
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
106108
*/
107109
public function __construct(
@@ -119,7 +121,8 @@ public function __construct(
119121
\Magento\Catalog\Model\Product\Attribute\Source\Status $catalogProductStatus,
120122
\Magento\Framework\App\State $appState,
121123
\Magento\Msrp\Helper\Data $msrpData,
122-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
124+
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
125+
UploaderFactory $uploaderFactory = null
123126
) {
124127
$this->productLinks = $catalogProductLink;
125128
$this->_storeManager = $storeManager;
@@ -136,7 +139,8 @@ public function __construct(
136139
$coreRegistry,
137140
$logger,
138141
$productRepository,
139-
$serializer
142+
$serializer,
143+
$uploaderFactory
140144
);
141145
}
142146

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Option/Type/File/ValidatorFileTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ protected function expectedValidate()
362362
return [
363363
'type' => 'image/jpeg',
364364
'title' => 'test.jpg',
365-
'quote_path' => 'custom_options/quote/t/e/RandomString',
366-
'order_path' => 'custom_options/order/t/e/RandomString',
365+
'quote_path' => 'custom_options/quote/R/a/RandomString',
366+
'order_path' => 'custom_options/order/R/a/RandomString',
367367
'size' => '3046',
368368
'width' => 136,
369369
'height' => 131,

0 commit comments

Comments
 (0)