Skip to content

Commit 3599559

Browse files
committed
ACPT-493: Upload csv with API request parameter
1 parent 4296a2a commit 3599559

File tree

6 files changed

+114
-60
lines changed

6 files changed

+114
-60
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Magento\Framework\Exception\NoSuchEntityException;
2424
use Magento\Framework\Filesystem;
2525
use Magento\Framework\Filesystem\Driver\File;
26+
use Magento\Framework\Filesystem\DriverPool;
2627
use Magento\Framework\Intl\DateTimeFactory;
2728
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
2829
use Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface;
@@ -226,7 +227,7 @@ class Product extends AbstractEntity
226227
* Links attribute name-to-link type ID.
227228
*
228229
* @deprecated 101.1.0 use DI for LinkProcessor class if you want to add additional types
229-
* @see LinkProcessor
230+
*
230231
* @var array
231232
*/
232233
protected $_linkNameToId = [

app/code/Magento/ImportCsv/Model/SourceData.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class SourceData extends AbstractSimpleObject implements SourceDataInterface
3333
*/
3434
private $allowedErrorCount;
3535

36+
/**
37+
* @var string
38+
*/
39+
private $csvData;
40+
3641
/**
3742
* @inheritdoc
3843
*/
@@ -65,6 +70,14 @@ public function getAllowedErrorCount(): string
6570
return $this->allowedErrorCount;
6671
}
6772

73+
/**
74+
* @inheritDoc
75+
*/
76+
public function getCsvData()
77+
{
78+
return $this->csvData;
79+
}
80+
6881
/**
6982
* @inheritDoc
7083
*/
@@ -104,13 +117,4 @@ public function setCsvData($csvData)
104117
{
105118
return $this->setData(self::PAYLOAD, $csvData);
106119
}
107-
108-
/**
109-
* @inheritDoc
110-
*/
111-
public function getCsvData()
112-
{
113-
return $this->csvData;
114-
}
115-
116120
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ protected function _getSourceAdapter($sourceFile)
336336
protected function _getSourceAdapterForApi()
337337
{
338338
return Adapter::findAdapterForData(
339+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
339340
base64_decode($this->getData('csvData')),
340341
$this->getData(self::FIELD_FIELD_SEPARATOR)
341342
);
@@ -575,7 +576,15 @@ public function getErrorAggregator()
575576
*/
576577
public function uploadSource()
577578
{
578-
return $this->upload->uploadSource($this);
579+
$entity = $this->getEntity();
580+
$result = $this->upload->uploadSource($entity);
581+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
582+
$extension = pathinfo($result['file'], PATHINFO_EXTENSION);
583+
$sourceFile = $this->getWorkingDir() . $entity . '.' . $extension;
584+
$sourceFileRelative = $this->getVarDirectory()->getRelativePath($sourceFile);
585+
$this->_removeBom($sourceFile);
586+
$this->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
587+
return $sourceFile;
579588
}
580589

581590
/**
@@ -599,6 +608,8 @@ public function uploadFileAndGetSource()
599608
}
600609

601610
/**
611+
* Get Source adapter object
612+
*
602613
* @return AbstractSource
603614
*/
604615
public function getSourceForApiData()
@@ -623,7 +634,7 @@ public function getVarDirectory()
623634
* @return $this
624635
* @throws FileSystemException
625636
*/
626-
public function _removeBom($sourceFile)
637+
protected function _removeBom($sourceFile)
627638
{
628639
$driver = $this->_varDirectory->getDriver();
629640
$string = $driver->fileGetContents($this->_varDirectory->getAbsolutePath($sourceFile));
@@ -806,7 +817,7 @@ public function isReportEntityType($entity = null)
806817
* @return $this
807818
* @throws LocalizedException
808819
*/
809-
public function createHistoryReport($sourceFileRelative, $entity, $extension = null, $result = null)
820+
protected function createHistoryReport($sourceFileRelative, $entity, $extension = null, $result = null)
810821
{
811822
if ($this->isReportEntityType($entity)) {
812823
if (is_array($sourceFileRelative)) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
class Data extends AbstractSource
1313
{
14+
/**
15+
* @var array
16+
*/
1417
private $rows;
1518

1619
/**
@@ -19,10 +22,17 @@ class Data extends AbstractSource
1922
protected $_delimiter = ',';
2023

2124
/**
25+
* Field Enclosure character
26+
*
2227
* @var string
2328
*/
2429
protected $_enclosure = '';
2530

31+
/**
32+
* Read Data and detect column names
33+
*
34+
* @param string $data
35+
*/
2636
public function __construct(string $data)
2737
{
2838
$rowsData = preg_split("/\r\n|\n|\r/", $data);
@@ -31,6 +41,11 @@ public function __construct(string $data)
3141
parent::__construct($colNames);
3242
}
3343

44+
/**
45+
* Read next line from CSV data
46+
*
47+
* @return array
48+
*/
3449
protected function _getNextRow()
3550
{
3651
if ($this->_key===count($this->rows)) {

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

Lines changed: 19 additions & 46 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,32 +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;
58-
$this->random = $random ?: ObjectManager::getInstance()
59-
->get(Random::class);
65+
$this->random = $random;
66+
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
6067
}
6168
/**
6269
* Move uploaded file.
6370
*
64-
* @param Import $import
71+
* @param string $entity
6572
* @throws LocalizedException
66-
* @return string Source file path
73+
* @return array
6774
*/
68-
public function uploadSource(Import $import)
75+
public function uploadSource(string $entity)
6976
{
7077
/** @var $adapter \Zend_File_Transfer_Adapter_Http */
7178
$adapter = $this->_httpFactory->create();
@@ -79,51 +86,17 @@ public function uploadSource(Import $import)
7986
throw new LocalizedException($errorMessage);
8087
}
8188

82-
$entity = $import->getEntity();
8389
/** @var $uploader Uploader */
8490
$uploader = $this->_uploaderFactory->create(['fileId' => Import::FIELD_NAME_SOURCE_FILE]);
8591
$uploader->setAllowedExtensions(['csv', 'zip']);
8692
$uploader->skipDbProcessing(true);
8793
$fileName = $this->random->getRandomString(32) . '.' . $uploader->getFileExtension();
8894
try {
89-
$result = $uploader->save($import->getWorkingDir(), $fileName);
95+
$result = $uploader->save($this->_varDirectory->getAbsolutePath('importexport/'), $fileName);
9096
} catch (\Exception $e) {
9197
throw new LocalizedException(__('The file cannot be uploaded.'));
9298
}
93-
94-
$extension = '';
95-
$uploadedFile = '';
96-
if ($result !== false) {
97-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
98-
$extension = pathinfo($result['file'], PATHINFO_EXTENSION);
99-
$uploadedFile = $result['path'] . $result['file'];
100-
}
101-
102-
if (!$extension) {
103-
$import->getVarDirectory()->delete($uploadedFile);
104-
throw new LocalizedException(__('The file you uploaded has no extension.'));
105-
}
106-
$sourceFile = $import->getWorkingDir() . $entity;
107-
108-
$sourceFile .= '.' . $extension;
109-
$sourceFileRelative = $import->getVarDirectory()->getRelativePath($sourceFile);
110-
111-
if (strtolower($uploadedFile) != strtolower($sourceFile)) {
112-
if ($import->getVarDirectory()->isExist($sourceFileRelative)) {
113-
$import->getVarDirectory()->delete($sourceFileRelative);
114-
}
115-
116-
try {
117-
$import->getVarDirectory()->renameFile(
118-
$import->getVarDirectory()->getRelativePath($uploadedFile),
119-
$sourceFileRelative
120-
);
121-
} catch (FileSystemException $e) {
122-
throw new LocalizedException(__('The source file moving process failed.'));
123-
}
124-
}
125-
$import->_removeBom($sourceFile);
126-
$import->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
127-
return $sourceFile;
99+
$uploader->renameFile($entity);
100+
return $result;
128101
}
129102
}

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)