14
14
use Magento \Framework \Exception \LocalizedException ;
15
15
use Magento \Framework \File \UploaderFactory ;
16
16
use Magento \Framework \Filesystem ;
17
+ use Magento \Framework \Filesystem \Io \File as IoFile ;
17
18
18
19
/**
19
20
* Processes files that are save for customer.
@@ -62,6 +63,11 @@ class File extends AbstractData
62
63
*/
63
64
protected $ fileProcessorFactory ;
64
65
66
+ /**
67
+ * @var IoFile|null
68
+ */
69
+ private $ ioFile ;
70
+
65
71
/**
66
72
* Constructor
67
73
*
@@ -77,6 +83,7 @@ class File extends AbstractData
77
83
* @param Filesystem $fileSystem
78
84
* @param UploaderFactory $uploaderFactory
79
85
* @param \Magento\Customer\Model\FileProcessorFactory|null $fileProcessorFactory
86
+ * @param IoFile|null $ioFile
80
87
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
81
88
*/
82
89
public function __construct (
@@ -91,7 +98,8 @@ public function __construct(
91
98
\Magento \MediaStorage \Model \File \Validator \NotProtectedExtension $ fileValidator ,
92
99
Filesystem $ fileSystem ,
93
100
UploaderFactory $ uploaderFactory ,
94
- \Magento \Customer \Model \FileProcessorFactory $ fileProcessorFactory = null
101
+ \Magento \Customer \Model \FileProcessorFactory $ fileProcessorFactory = null ,
102
+ IoFile $ ioFile = null
95
103
) {
96
104
$ value = $ this ->prepareFileValue ($ value );
97
105
parent ::__construct ($ localeDate , $ logger , $ attribute , $ localeResolver , $ value , $ entityTypeCode , $ isAjax );
@@ -102,6 +110,8 @@ public function __construct(
102
110
$ this ->fileProcessorFactory = $ fileProcessorFactory ?: ObjectManager::getInstance ()
103
111
->get (FileProcessorFactory::class);
104
112
$ this ->fileProcessor = $ this ->fileProcessorFactory ->create (['entityTypeCode ' => $ this ->_entityTypeCode ]);
113
+ $ this ->ioFile = $ ioFile ?: ObjectManager::getInstance ()
114
+ ->get (IoFile::class);
105
115
}
106
116
107
117
/**
@@ -183,7 +193,7 @@ protected function _validateByRules($value)
183
193
{
184
194
$ label = $ value ['name ' ];
185
195
$ rules = $ this ->getAttribute ()->getValidationRules ();
186
- $ extension = $ this ->getFileExtension ($ value ['name ' ]);
196
+ $ extension = $ this ->ioFile -> getPathInfo ($ value ['name ' ])[ ' extension ' ] ;
187
197
$ fileExtensions = ArrayObjectSearch::getArrayElementByName (
188
198
$ rules ,
189
199
'file_extensions '
@@ -221,28 +231,6 @@ protected function _validateByRules($value)
221
231
return [];
222
232
}
223
233
224
- /**
225
- * Get file extension from the file if it exists, otherwise, get from filename.
226
- *
227
- * @param string $fileName
228
- * @return string
229
- */
230
- private function getFileExtension (string $ fileName ): string
231
- {
232
- return pathinfo ($ fileName , PATHINFO_EXTENSION );
233
- }
234
-
235
- /**
236
- * Get file basename from the file if it exists, otherwise, get from filename.
237
- *
238
- * @param string $fileName
239
- * @return string
240
- */
241
- private function getFileBasename (string $ fileName ): string
242
- {
243
- return pathinfo ($ fileName , PATHINFO_BASENAME );
244
- }
245
-
246
234
/**
247
235
* Helper function that checks if the file was uploaded.
248
236
*
@@ -259,7 +247,7 @@ protected function _isUploadedFile($filename)
259
247
}
260
248
261
249
// This case is required for file uploader UI component
262
- $ temporaryFile = FileProcessor::TMP_DIR . '/ ' . $ this ->getFileBasename ($ filename );
250
+ $ temporaryFile = FileProcessor::TMP_DIR . '/ ' . $ this ->ioFile -> getPathInfo ($ filename )[ ' basename ' ] ;
263
251
if ($ this ->fileProcessor ->isExist ($ temporaryFile )) {
264
252
return true ;
265
253
}
0 commit comments