18
18
use Magento \Framework \HTTP \Adapter \FileTransferFactory ;
19
19
use Magento \Framework \Indexer \IndexerRegistry ;
20
20
use Magento \Framework \Math \Random ;
21
+ use Magento \Framework \Message \ManagerInterface ;
21
22
use Magento \Framework \Stdlib \DateTime \DateTime ;
22
23
use Magento \ImportExport \Helper \Data as DataHelper ;
23
24
use Magento \ImportExport \Model \Export \Adapter \CsvFactory ;
29
30
use Magento \ImportExport \Model \Import \Entity \Factory ;
30
31
use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingError ;
31
32
use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
32
- use Magento \Framework \Message \ManagerInterface ;
33
33
use Magento \ImportExport \Model \ResourceModel \Import \Data ;
34
34
use Magento \ImportExport \Model \Source \Import \AbstractBehavior ;
35
35
use Magento \ImportExport \Model \Source \Import \Behavior \Factory as BehaviorFactory ;
36
- use Magento \MediaStorage \Model \File \ Uploader ;
36
+ use Magento \ImportExport \Model \Source \ Upload ;
37
37
use Magento \MediaStorage \Model \File \UploaderFactory ;
38
38
use Psr \Log \LoggerInterface ;
39
39
@@ -122,6 +122,7 @@ class Import extends AbstractModel
122
122
protected $ _entityAdapter ;
123
123
124
124
/**
125
+ * @Deprecated Property isn't used
125
126
* @var DataHelper
126
127
*/
127
128
protected $ _importExportData = null ;
@@ -152,6 +153,7 @@ class Import extends AbstractModel
152
153
protected $ _csvFactory ;
153
154
154
155
/**
156
+ * @Deprecated Property isn't used
155
157
* @var FileTransferFactory
156
158
*/
157
159
protected $ _httpFactory ;
@@ -192,10 +194,16 @@ class Import extends AbstractModel
192
194
private $ messageManager ;
193
195
194
196
/**
197
+ * @Deprecated Property isn't used
195
198
* @var Random
196
199
*/
197
200
private $ random ;
198
201
202
+ /**
203
+ * @var Upload
204
+ */
205
+ private $ upload ;
206
+
199
207
/**
200
208
* @param LoggerInterface $logger
201
209
* @param Filesystem $filesystem
@@ -214,6 +222,7 @@ class Import extends AbstractModel
214
222
* @param array $data
215
223
* @param ManagerInterface|null $messageManager
216
224
* @param Random|null $random
225
+ * @param Upload|null $upload
217
226
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
218
227
*/
219
228
public function __construct (
@@ -233,7 +242,8 @@ public function __construct(
233
242
DateTime $ localeDate ,
234
243
array $ data = [],
235
244
ManagerInterface $ messageManager = null ,
236
- Random $ random = null
245
+ Random $ random = null ,
246
+ Upload $ upload = null
237
247
) {
238
248
$ this ->_importExportData = $ importExportData ;
239
249
$ this ->_coreConfig = $ coreConfig ;
@@ -252,6 +262,8 @@ public function __construct(
252
262
->get (ManagerInterface::class);
253
263
$ this ->random = $ random ?: ObjectManager::getInstance ()
254
264
->get (Random::class);
265
+ $ this ->upload = $ upload ?: ObjectManager::getInstance ()
266
+ ->get (Upload::class);
255
267
parent ::__construct ($ logger , $ filesystem , $ data );
256
268
}
257
269
@@ -303,6 +315,8 @@ protected function _getEntityAdapter()
303
315
/**
304
316
* Returns source adapter object.
305
317
*
318
+ * @Deprecated
319
+ * @see \Magento\ImportExport\Model\Import\Source\Factory::create()
306
320
* @param string $sourceFile Full path to source file
307
321
* @return AbstractSource
308
322
* @throws FileSystemException
@@ -550,61 +564,12 @@ public function getErrorAggregator()
550
564
*/
551
565
public function uploadSource ()
552
566
{
553
- /** @var $adapter \Zend_File_Transfer_Adapter_Http */
554
- $ adapter = $ this ->_httpFactory ->create ();
555
- if (!$ adapter ->isValid (self ::FIELD_NAME_SOURCE_FILE )) {
556
- $ errors = $ adapter ->getErrors ();
557
- if ($ errors [0 ] == \Zend_Validate_File_Upload::INI_SIZE ) {
558
- $ errorMessage = $ this ->_importExportData ->getMaxUploadSizeMessage ();
559
- } else {
560
- $ errorMessage = __ ('The file was not uploaded. ' );
561
- }
562
- throw new LocalizedException ($ errorMessage );
563
- }
564
-
565
567
$ entity = $ this ->getEntity ();
566
- /** @var $uploader Uploader */
567
- $ uploader = $ this ->_uploaderFactory ->create (['fileId ' => self ::FIELD_NAME_SOURCE_FILE ]);
568
- $ uploader ->setAllowedExtensions (['csv ' , 'zip ' ]);
569
- $ uploader ->skipDbProcessing (true );
570
- $ fileName = $ this ->random ->getRandomString (32 ) . '. ' . $ uploader ->getFileExtension ();
571
- try {
572
- $ result = $ uploader ->save ($ this ->getWorkingDir (), $ fileName );
573
- } catch (\Exception $ e ) {
574
- throw new LocalizedException (__ ('The file cannot be uploaded. ' ));
575
- }
576
-
577
- $ extension = '' ;
578
- $ uploadedFile = '' ;
579
- if ($ result !== false ) {
580
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
581
- $ extension = pathinfo ($ result ['file ' ], PATHINFO_EXTENSION );
582
- $ uploadedFile = $ result ['path ' ] . $ result ['file ' ];
583
- }
584
-
585
- if (!$ extension ) {
586
- $ this ->_varDirectory ->delete ($ uploadedFile );
587
- throw new LocalizedException (__ ('The file you uploaded has no extension. ' ));
588
- }
589
- $ sourceFile = $ this ->getWorkingDir () . $ entity ;
590
-
591
- $ sourceFile .= '. ' . $ extension ;
568
+ $ result = $ this ->upload ->uploadSource ($ entity );
569
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
570
+ $ extension = pathinfo ($ result ['file ' ], PATHINFO_EXTENSION );
571
+ $ sourceFile = $ this ->getWorkingDir () . $ entity . '. ' . $ extension ;
592
572
$ sourceFileRelative = $ this ->_varDirectory ->getRelativePath ($ sourceFile );
593
-
594
- if (strtolower ($ uploadedFile ) != strtolower ($ sourceFile )) {
595
- if ($ this ->_varDirectory ->isExist ($ sourceFileRelative )) {
596
- $ this ->_varDirectory ->delete ($ sourceFileRelative );
597
- }
598
-
599
- try {
600
- $ this ->_varDirectory ->renameFile (
601
- $ this ->_varDirectory ->getRelativePath ($ uploadedFile ),
602
- $ sourceFileRelative
603
- );
604
- } catch (FileSystemException $ e ) {
605
- throw new LocalizedException (__ ('The source file moving process failed. ' ));
606
- }
607
- }
608
573
$ this ->_removeBom ($ sourceFile );
609
574
$ this ->createHistoryReport ($ sourceFileRelative , $ entity , $ extension , $ result );
610
575
return $ sourceFile ;
0 commit comments