7
7
8
8
namespace Magento \ImportExport \Model \Source ;
9
9
10
- use Magento \Framework \App \ObjectManager ;
11
- use Magento \Framework \Exception \FileSystemException ;
10
+ use Magento \Framework \App \Filesystem \DirectoryList ;
12
11
use Magento \Framework \Exception \LocalizedException ;
13
- use Magento \Framework \Filesystem \Io \File ;
12
+ use Magento \Framework \Filesystem ;
13
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
14
14
use Magento \Framework \HTTP \Adapter \FileTransferFactory ;
15
15
use Magento \Framework \Math \Random ;
16
16
use Magento \ImportExport \Helper \Data as DataHelper ;
@@ -40,31 +40,39 @@ class Upload
40
40
*/
41
41
private $ random ;
42
42
43
+ /**
44
+ * @var WriteInterface
45
+ */
46
+ protected $ _varDirectory ;
47
+
43
48
/**
44
49
* @param FileTransferFactory $httpFactory
45
50
* @param DataHelper $importExportData
46
51
* @param UploaderFactory $uploaderFactory
47
52
* @param Random|null $random
53
+ * @param Filesystem $filesystem
48
54
*/
49
55
public function __construct (
50
56
FileTransferFactory $ httpFactory ,
51
57
DataHelper $ importExportData ,
52
58
UploaderFactory $ uploaderFactory ,
53
- Random $ random
59
+ Random $ random ,
60
+ Filesystem $ filesystem
54
61
) {
55
62
$ this ->_httpFactory = $ httpFactory ;
56
63
$ this ->_importExportData = $ importExportData ;
57
64
$ this ->_uploaderFactory = $ uploaderFactory ;
58
65
$ this ->random = $ random ;
66
+ $ this ->_varDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::VAR_IMPORT_EXPORT );
59
67
}
60
68
/**
61
69
* Move uploaded file.
62
70
*
63
- * @param Import $import
71
+ * @param string $entity
64
72
* @throws LocalizedException
65
73
* @return array
66
74
*/
67
- public function uploadSource (Import $ import )
75
+ public function uploadSource (string $ entity )
68
76
{
69
77
/** @var $adapter \Zend_File_Transfer_Adapter_Http */
70
78
$ adapter = $ this ->_httpFactory ->create ();
@@ -78,49 +86,17 @@ public function uploadSource(Import $import)
78
86
throw new LocalizedException ($ errorMessage );
79
87
}
80
88
81
- $ entity = $ import ->getEntity ();
82
89
/** @var $uploader Uploader */
83
90
$ uploader = $ this ->_uploaderFactory ->create (['fileId ' => Import::FIELD_NAME_SOURCE_FILE ]);
84
91
$ uploader ->setAllowedExtensions (['csv ' , 'zip ' ]);
85
92
$ uploader ->skipDbProcessing (true );
86
93
$ fileName = $ this ->random ->getRandomString (32 ) . '. ' . $ uploader ->getFileExtension ();
87
94
try {
88
- $ result = $ uploader ->save ($ import -> getWorkingDir ( ), $ fileName );
95
+ $ result = $ uploader ->save ($ this -> _varDirectory -> getAbsolutePath ( ' importexport/ ' ), $ fileName );
89
96
} catch (\Exception $ e ) {
90
97
throw new LocalizedException (__ ('The file cannot be uploaded. ' ));
91
98
}
92
-
93
- $ extension = '' ;
94
- $ uploadedFile = '' ;
95
- if ($ result !== false ) {
96
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
97
- $ extension = pathinfo ($ result ['file ' ], PATHINFO_EXTENSION );
98
- $ uploadedFile = $ result ['path ' ] . $ result ['file ' ];
99
- }
100
-
101
- if (!$ extension ) {
102
- $ import ->getVarDirectory ()->delete ($ uploadedFile );
103
- throw new LocalizedException (__ ('The file you uploaded has no extension. ' ));
104
- }
105
- $ sourceFile = $ import ->getWorkingDir () . $ entity ;
106
-
107
- $ sourceFile .= '. ' . $ extension ;
108
- $ sourceFileRelative = $ import ->getVarDirectory ()->getRelativePath ($ sourceFile );
109
-
110
- if (strtolower ($ uploadedFile ) != strtolower ($ sourceFile )) {
111
- if ($ import ->getVarDirectory ()->isExist ($ sourceFileRelative )) {
112
- $ import ->getVarDirectory ()->delete ($ sourceFileRelative );
113
- }
114
-
115
- try {
116
- $ import ->getVarDirectory ()->renameFile (
117
- $ import ->getVarDirectory ()->getRelativePath ($ uploadedFile ),
118
- $ sourceFileRelative
119
- );
120
- } catch (FileSystemException $ e ) {
121
- throw new LocalizedException (__ ('The source file moving process failed. ' ));
122
- }
123
- }
99
+ $ uploader ->renameFile ($ entity );
124
100
return $ result ;
125
101
}
126
102
}
0 commit comments