Skip to content

Commit 412c747

Browse files
author
Hwashiang Yu
committed
MC-31357: Customer file uploader update
- Updated customer metadata form image logic - Updated corresponding unit and integration tests
1 parent 782acf6 commit 412c747

File tree

4 files changed

+448
-53
lines changed

4 files changed

+448
-53
lines changed

app/code/Magento/Customer/Model/Metadata/Form/Image.php

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,33 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Customer\Model\Metadata\Form;
710

811
use Magento\Customer\Api\AddressMetadataInterface;
912
use Magento\Customer\Api\CustomerMetadataInterface;
13+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1014
use Magento\Customer\Model\FileProcessor;
15+
use Magento\Customer\Model\FileProcessorFactory;
1116
use Magento\Framework\Api\ArrayObjectSearch;
1217
use Magento\Framework\Api\Data\ImageContentInterface;
1318
use Magento\Framework\Api\Data\ImageContentInterfaceFactory;
1419
use Magento\Framework\App\ObjectManager;
20+
use Magento\Framework\Exception\FileSystemException;
21+
use Magento\Framework\Exception\LocalizedException;
1522
use Magento\Framework\File\UploaderFactory;
1623
use Magento\Framework\Filesystem;
24+
use Magento\Framework\Filesystem\Directory\WriteInterface;
25+
use Magento\Framework\Filesystem\Io\File as IoFileSystem;
26+
use Magento\Framework\App\Filesystem\DirectoryList;
27+
use Magento\Framework\Filesystem\Directory\WriteFactory;
28+
use Magento\Framework\Locale\ResolverInterface;
29+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
30+
use Magento\Framework\Url\EncoderInterface;
31+
use Magento\MediaStorage\Model\File\Validator\NotProtectedExtension;
32+
use Psr\Log\LoggerInterface;
1733

1834
/**
1935
* Metadata for form image field
@@ -27,38 +43,55 @@ class Image extends File
2743
*/
2844
private $imageContentFactory;
2945

46+
/**
47+
* @var IoFileSystem
48+
*/
49+
private $ioFileSystem;
50+
51+
/**
52+
* @var WriteInterface
53+
*/
54+
private $mediaEntityTmpDirectory;
55+
3056
/**
3157
* Constructor
3258
*
33-
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
34-
* @param \Psr\Log\LoggerInterface $logger
35-
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
36-
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
59+
* @param TimezoneInterface $localeDate
60+
* @param LoggerInterface $logger
61+
* @param AttributeMetadataInterface $attribute
62+
* @param ResolverInterface $localeResolver
3763
* @param null|string $value
3864
* @param string $entityTypeCode
3965
* @param bool $isAjax
40-
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
41-
* @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator
66+
* @param EncoderInterface $urlEncoder
67+
* @param NotProtectedExtension $fileValidator
4268
* @param Filesystem $fileSystem
4369
* @param UploaderFactory $uploaderFactory
44-
* @param \Magento\Customer\Model\FileProcessorFactory|null $fileProcessorFactory
45-
* @param \Magento\Framework\Api\Data\ImageContentInterfaceFactory|null $imageContentInterfaceFactory
70+
* @param FileProcessorFactory|null $fileProcessorFactory
71+
* @param ImageContentInterfaceFactory|null $imageContentInterfaceFactory
72+
* @param IoFileSystem|null $ioFileSystem
73+
* @param DirectoryList|null $directoryList
74+
* @param WriteFactory|null $writeFactory
4675
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
76+
* @throws FileSystemException
4777
*/
4878
public function __construct(
49-
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
50-
\Psr\Log\LoggerInterface $logger,
51-
\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
52-
\Magento\Framework\Locale\ResolverInterface $localeResolver,
79+
TimezoneInterface $localeDate,
80+
LoggerInterface $logger,
81+
AttributeMetadataInterface $attribute,
82+
ResolverInterface $localeResolver,
5383
$value,
5484
$entityTypeCode,
5585
$isAjax,
56-
\Magento\Framework\Url\EncoderInterface $urlEncoder,
57-
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $fileValidator,
86+
EncoderInterface $urlEncoder,
87+
NotProtectedExtension $fileValidator,
5888
Filesystem $fileSystem,
5989
UploaderFactory $uploaderFactory,
60-
\Magento\Customer\Model\FileProcessorFactory $fileProcessorFactory = null,
61-
\Magento\Framework\Api\Data\ImageContentInterfaceFactory $imageContentInterfaceFactory = null
90+
FileProcessorFactory $fileProcessorFactory = null,
91+
ImageContentInterfaceFactory $imageContentInterfaceFactory = null,
92+
IoFileSystem $ioFileSystem = null,
93+
?DirectoryList $directoryList = null,
94+
?WriteFactory $writeFactory = null
6295
) {
6396
parent::__construct(
6497
$localeDate,
@@ -75,7 +108,16 @@ public function __construct(
75108
$fileProcessorFactory
76109
);
77110
$this->imageContentFactory = $imageContentInterfaceFactory ?: ObjectManager::getInstance()
78-
->get(\Magento\Framework\Api\Data\ImageContentInterfaceFactory::class);
111+
->get(ImageContentInterfaceFactory::class);
112+
$this->ioFileSystem = $ioFileSystem ?: ObjectManager::getInstance()
113+
->get(IoFileSystem::class);
114+
$writeFactory = $writeFactory ?? ObjectManager::getInstance()->get(WriteFactory::class);
115+
$directoryList = $directoryList ?? ObjectManager::getInstance()->get(DirectoryList::class);
116+
$this->mediaEntityTmpDirectory = $writeFactory->create(
117+
$directoryList->getPath($directoryList::MEDIA)
118+
. '/' . $this->_entityTypeCode
119+
. '/' . FileProcessor::TMP_DIR
120+
);
79121
}
80122

81123
/**
@@ -85,6 +127,7 @@ public function __construct(
85127
*
86128
* @param array $value
87129
* @return string[]
130+
* @throws LocalizedException
88131
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
89132
* @SuppressWarnings(PHPMD.NPathComplexity)
90133
*/
@@ -93,7 +136,11 @@ protected function _validateByRules($value)
93136
$label = $value['name'];
94137
$rules = $this->getAttribute()->getValidationRules();
95138

96-
$imageProp = @getimagesize($value['tmp_name']);
139+
try {
140+
$imageProp = getimagesize($value['tmp_name']);
141+
} catch (\Throwable $e) {
142+
$imageProp = false;
143+
}
97144

98145
if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) {
99146
return [__('"%1" is not a valid file.', $label)];
@@ -106,9 +153,11 @@ protected function _validateByRules($value)
106153
}
107154

108155
// modify image name
109-
$extension = pathinfo($value['name'], PATHINFO_EXTENSION);
156+
$extension = $this->ioFileSystem->getPathInfo($value['name'])['extension'];
110157
if ($extension != $allowImageTypes[$imageProp[2]]) {
111-
$value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
158+
$value['name'] = $this->ioFileSystem->getPathInfo($value['name'])['filename']
159+
. '.'
160+
. $allowImageTypes[$imageProp[2]];
112161
}
113162

114163
$maxFileSize = ArrayObjectSearch::getArrayElementByName(
@@ -153,6 +202,7 @@ protected function _validateByRules($value)
153202
*
154203
* @param array $value
155204
* @return bool|int|ImageContentInterface|string
205+
* @throws LocalizedException
156206
*/
157207
protected function processUiComponentValue(array $value)
158208
{
@@ -174,32 +224,43 @@ protected function processUiComponentValue(array $value)
174224
*
175225
* @param array $value
176226
* @return string
227+
* @throws LocalizedException
177228
*/
178229
protected function processCustomerAddressValue(array $value)
179230
{
180-
$result = $this->getFileProcessor()->moveTemporaryFile($value['file']);
181-
return $result;
231+
$fileName = $this->mediaEntityTmpDirectory
232+
->getDriver()
233+
->getRealPathSafety(
234+
$this->mediaEntityTmpDirectory->getAbsolutePath(
235+
ltrim(
236+
$value['file'],
237+
'/'
238+
)
239+
)
240+
);
241+
return $this->getFileProcessor()->moveTemporaryFile(
242+
$this->mediaEntityTmpDirectory->getRelativePath($fileName)
243+
);
182244
}
183245

184246
/**
185247
* Process file uploader UI component data for customer entity
186248
*
187249
* @param array $value
188250
* @return bool|int|ImageContentInterface|string
251+
* @throws LocalizedException
189252
*/
190253
protected function processCustomerValue(array $value)
191254
{
192-
$temporaryFile = FileProcessor::TMP_DIR . '/' . ltrim($value['file'], '/');
193-
194-
if ($this->getFileProcessor()->isExist($temporaryFile)) {
255+
$file = ltrim($value['file'], '/');
256+
if ($this->mediaEntityTmpDirectory->isExist($file)) {
257+
$temporaryFile = FileProcessor::TMP_DIR . '/' . $file;
195258
$base64EncodedData = $this->getFileProcessor()->getBase64EncodedData($temporaryFile);
196-
197259
/** @var ImageContentInterface $imageContentDataObject */
198260
$imageContentDataObject = $this->imageContentFactory->create()
199261
->setName($value['name'])
200262
->setBase64EncodedData($base64EncodedData)
201263
->setType($value['type']);
202-
203264
// Remove temporary file
204265
$this->getFileProcessor()->removeUploadedFile($temporaryFile);
205266

0 commit comments

Comments
 (0)