1818use Magento \Framework \HTTP \Adapter \FileTransferFactory ;
1919use Magento \Framework \Indexer \IndexerRegistry ;
2020use Magento \Framework \Math \Random ;
21+ use Magento \Framework \Message \ManagerInterface ;
2122use Magento \Framework \Stdlib \DateTime \DateTime ;
2223use Magento \ImportExport \Helper \Data as DataHelper ;
2324use Magento \ImportExport \Model \Export \Adapter \CsvFactory ;
2930use Magento \ImportExport \Model \Import \Entity \Factory ;
3031use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingError ;
3132use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
32- use Magento \Framework \Message \ManagerInterface ;
3333use Magento \ImportExport \Model \ResourceModel \Import \Data ;
3434use Magento \ImportExport \Model \Source \Import \AbstractBehavior ;
3535use Magento \ImportExport \Model \Source \Import \Behavior \Factory as BehaviorFactory ;
36- use Magento \MediaStorage \Model \File \ Uploader ;
36+ use Magento \ImportExport \Model \Source \ Upload ;
3737use Magento \MediaStorage \Model \File \UploaderFactory ;
3838use Psr \Log \LoggerInterface ;
3939
@@ -122,6 +122,7 @@ class Import extends AbstractModel
122122 protected $ _entityAdapter ;
123123
124124 /**
125+ * @Deprecated Property isn't used
125126 * @var DataHelper
126127 */
127128 protected $ _importExportData = null ;
@@ -152,6 +153,7 @@ class Import extends AbstractModel
152153 protected $ _csvFactory ;
153154
154155 /**
156+ * @Deprecated Property isn't used
155157 * @var FileTransferFactory
156158 */
157159 protected $ _httpFactory ;
@@ -192,10 +194,16 @@ class Import extends AbstractModel
192194 private $ messageManager ;
193195
194196 /**
197+ * @Deprecated Property isn't used
195198 * @var Random
196199 */
197200 private $ random ;
198201
202+ /**
203+ * @var Upload
204+ */
205+ private $ upload ;
206+
199207 /**
200208 * @param LoggerInterface $logger
201209 * @param Filesystem $filesystem
@@ -214,6 +222,7 @@ class Import extends AbstractModel
214222 * @param array $data
215223 * @param ManagerInterface|null $messageManager
216224 * @param Random|null $random
225+ * @param Upload|null $upload
217226 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
218227 */
219228 public function __construct (
@@ -233,7 +242,8 @@ public function __construct(
233242 DateTime $ localeDate ,
234243 array $ data = [],
235244 ManagerInterface $ messageManager = null ,
236- Random $ random = null
245+ Random $ random = null ,
246+ Upload $ upload = null
237247 ) {
238248 $ this ->_importExportData = $ importExportData ;
239249 $ this ->_coreConfig = $ coreConfig ;
@@ -252,6 +262,8 @@ public function __construct(
252262 ->get (ManagerInterface::class);
253263 $ this ->random = $ random ?: ObjectManager::getInstance ()
254264 ->get (Random::class);
265+ $ this ->upload = $ upload ?: ObjectManager::getInstance ()
266+ ->get (Upload::class);
255267 parent ::__construct ($ logger , $ filesystem , $ data );
256268 }
257269
@@ -303,6 +315,8 @@ protected function _getEntityAdapter()
303315 /**
304316 * Returns source adapter object.
305317 *
318+ * @Deprecated
319+ * @see \Magento\ImportExport\Model\Import\Source\Factory::create()
306320 * @param string $sourceFile Full path to source file
307321 * @return AbstractSource
308322 * @throws FileSystemException
@@ -550,61 +564,12 @@ public function getErrorAggregator()
550564 */
551565 public function uploadSource ()
552566 {
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-
565567 $ 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 ;
592572 $ 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- }
608573 $ this ->_removeBom ($ sourceFile );
609574 $ this ->createHistoryReport ($ sourceFileRelative , $ entity , $ extension , $ result );
610575 return $ sourceFile ;
0 commit comments