Skip to content

Commit 3c5e02a

Browse files
committed
ACPT-493: Upload csv with API request parameter
1 parent 2112d35 commit 3c5e02a

File tree

3 files changed

+68
-42
lines changed

3 files changed

+68
-42
lines changed

app/code/Magento/ImportExport/Model/Import.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ public function getErrorAggregator()
576576
*/
577577
public function uploadSource()
578578
{
579-
$result = $this->upload->uploadSource($this);
580579
$entity = $this->getEntity();
580+
$result = $this->upload->uploadSource($entity);
581581
// phpcs:ignore Magento2.Functions.DiscouragedFunction
582582
$extension = pathinfo($result['file'], PATHINFO_EXTENSION);
583583
$sourceFile = $this->getWorkingDir() . $entity . '.' . $extension;

app/code/Magento/ImportExport/Model/Source/Upload.php

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Magento\ImportExport\Model\Source;
99

10-
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\Exception\FileSystemException;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
1211
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Framework\Filesystem\Io\File;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1414
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
1515
use Magento\Framework\Math\Random;
1616
use Magento\ImportExport\Helper\Data as DataHelper;
@@ -40,31 +40,39 @@ class Upload
4040
*/
4141
private $random;
4242

43+
/**
44+
* @var WriteInterface
45+
*/
46+
protected $_varDirectory;
47+
4348
/**
4449
* @param FileTransferFactory $httpFactory
4550
* @param DataHelper $importExportData
4651
* @param UploaderFactory $uploaderFactory
4752
* @param Random|null $random
53+
* @param Filesystem $filesystem
4854
*/
4955
public function __construct(
5056
FileTransferFactory $httpFactory,
5157
DataHelper $importExportData,
5258
UploaderFactory $uploaderFactory,
53-
Random $random
59+
Random $random,
60+
Filesystem $filesystem
5461
) {
5562
$this->_httpFactory = $httpFactory;
5663
$this->_importExportData = $importExportData;
5764
$this->_uploaderFactory = $uploaderFactory;
5865
$this->random = $random;
66+
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
5967
}
6068
/**
6169
* Move uploaded file.
6270
*
63-
* @param Import $import
71+
* @param string $entity
6472
* @throws LocalizedException
6573
* @return array
6674
*/
67-
public function uploadSource(Import $import)
75+
public function uploadSource(string $entity)
6876
{
6977
/** @var $adapter \Zend_File_Transfer_Adapter_Http */
7078
$adapter = $this->_httpFactory->create();
@@ -78,49 +86,17 @@ public function uploadSource(Import $import)
7886
throw new LocalizedException($errorMessage);
7987
}
8088

81-
$entity = $import->getEntity();
8289
/** @var $uploader Uploader */
8390
$uploader = $this->_uploaderFactory->create(['fileId' => Import::FIELD_NAME_SOURCE_FILE]);
8491
$uploader->setAllowedExtensions(['csv', 'zip']);
8592
$uploader->skipDbProcessing(true);
8693
$fileName = $this->random->getRandomString(32) . '.' . $uploader->getFileExtension();
8794
try {
88-
$result = $uploader->save($import->getWorkingDir(), $fileName);
95+
$result = $uploader->save($this->_varDirectory->getAbsolutePath('importexport/'), $fileName);
8996
} catch (\Exception $e) {
9097
throw new LocalizedException(__('The file cannot be uploaded.'));
9198
}
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);
124100
return $result;
125101
}
126102
}

app/code/Magento/MediaStorage/Model/File/Uploader.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
namespace Magento\MediaStorage\Model\File;
88

9+
use Magento\Framework\App\Filesystem\DirectoryList;
910
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\FileSystemException;
12+
use Magento\Framework\Exception\LocalizedException;
1013
use Magento\Framework\Validation\ValidationException;
1114
use Magento\MediaStorage\Model\File\Validator\Image;
1215

@@ -49,21 +52,29 @@ class Uploader extends \Magento\Framework\File\Uploader
4952
*/
5053
private $imageValidator;
5154

55+
/**
56+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
57+
*/
58+
protected $_varDirectory;
59+
5260
/**
5361
* @param string $fileId
5462
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
5563
* @param \Magento\MediaStorage\Helper\File\Storage $coreFileStorage
5664
* @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
65+
* @param \Magento\Framework\Filesystem $filesystem
5766
*/
5867
public function __construct(
5968
$fileId,
6069
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
6170
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
62-
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
71+
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator,
72+
\Magento\Framework\Filesystem $filesystem
6373
) {
6474
$this->_coreFileStorageDb = $coreFileStorageDb;
6575
$this->_coreFileStorage = $coreFileStorage;
6676
$this->_validator = $validator;
77+
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
6778
parent::__construct($fileId);
6879
}
6980

@@ -140,6 +151,45 @@ public function validateFile()
140151
return $this->_file;
141152
}
142153

154+
/**
155+
* @return void
156+
* @throws LocalizedException
157+
*/
158+
public function renameFile(string $entity)
159+
{
160+
$extension = '';
161+
$uploadedFile = '';
162+
if ($this->_result !== false) {
163+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
164+
$extension = pathinfo($this->_result['file'], PATHINFO_EXTENSION);
165+
$uploadedFile = $this->_result['path'] . $this->_result['file'];
166+
}
167+
168+
if (!$extension) {
169+
$this->_varDirectory->delete($uploadedFile);
170+
throw new LocalizedException(__('The file you uploaded has no extension.'));
171+
}
172+
$sourceFile = $this->_varDirectory->getAbsolutePath('importexport/') . $entity;
173+
174+
$sourceFile .= '.' . $extension;
175+
$sourceFileRelative = $this->_varDirectory->getRelativePath($sourceFile);
176+
177+
if (strtolower($uploadedFile) != strtolower($sourceFile)) {
178+
if ($this->_varDirectory->isExist($sourceFileRelative)) {
179+
$this->_varDirectory->delete($sourceFileRelative);
180+
}
181+
182+
try {
183+
$this->_varDirectory->renameFile(
184+
$this->_varDirectory->getRelativePath($uploadedFile),
185+
$sourceFileRelative
186+
);
187+
} catch (FileSystemException $e) {
188+
throw new LocalizedException(__('The source file moving process failed.'));
189+
}
190+
}
191+
}
192+
143193
/**
144194
* @inheritDoc
145195
* @since 100.4.0

0 commit comments

Comments
 (0)