Skip to content

Commit 4296a2a

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

File tree

6 files changed

+60
-114
lines changed

6 files changed

+60
-114
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Magento\Framework\Exception\NoSuchEntityException;
2424
use Magento\Framework\Filesystem;
2525
use Magento\Framework\Filesystem\Driver\File;
26-
use Magento\Framework\Filesystem\DriverPool;
2726
use Magento\Framework\Intl\DateTimeFactory;
2827
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
2928
use Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface;
@@ -227,7 +226,7 @@ class Product extends AbstractEntity
227226
* Links attribute name-to-link type ID.
228227
*
229228
* @deprecated 101.1.0 use DI for LinkProcessor class if you want to add additional types
230-
*
229+
* @see LinkProcessor
231230
* @var array
232231
*/
233232
protected $_linkNameToId = [

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

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

36-
/**
37-
* @var string
38-
*/
39-
private $csvData;
40-
4136
/**
4237
* @inheritdoc
4338
*/
@@ -70,14 +65,6 @@ public function getAllowedErrorCount(): string
7065
return $this->allowedErrorCount;
7166
}
7267

73-
/**
74-
* @inheritDoc
75-
*/
76-
public function getCsvData()
77-
{
78-
return $this->csvData;
79-
}
80-
8168
/**
8269
* @inheritDoc
8370
*/
@@ -117,4 +104,13 @@ public function setCsvData($csvData)
117104
{
118105
return $this->setData(self::PAYLOAD, $csvData);
119106
}
107+
108+
/**
109+
* @inheritDoc
110+
*/
111+
public function getCsvData()
112+
{
113+
return $this->csvData;
114+
}
115+
120116
}

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ protected function _getSourceAdapter($sourceFile)
336336
protected function _getSourceAdapterForApi()
337337
{
338338
return Adapter::findAdapterForData(
339-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
340339
base64_decode($this->getData('csvData')),
341340
$this->getData(self::FIELD_FIELD_SEPARATOR)
342341
);
@@ -576,15 +575,7 @@ public function getErrorAggregator()
576575
*/
577576
public function uploadSource()
578577
{
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;
578+
return $this->upload->uploadSource($this);
588579
}
589580

590581
/**
@@ -608,8 +599,6 @@ public function uploadFileAndGetSource()
608599
}
609600

610601
/**
611-
* Get Source adapter object
612-
*
613602
* @return AbstractSource
614603
*/
615604
public function getSourceForApiData()
@@ -634,7 +623,7 @@ public function getVarDirectory()
634623
* @return $this
635624
* @throws FileSystemException
636625
*/
637-
protected function _removeBom($sourceFile)
626+
public function _removeBom($sourceFile)
638627
{
639628
$driver = $this->_varDirectory->getDriver();
640629
$string = $driver->fileGetContents($this->_varDirectory->getAbsolutePath($sourceFile));
@@ -817,7 +806,7 @@ public function isReportEntityType($entity = null)
817806
* @return $this
818807
* @throws LocalizedException
819808
*/
820-
protected function createHistoryReport($sourceFileRelative, $entity, $extension = null, $result = null)
809+
public function createHistoryReport($sourceFileRelative, $entity, $extension = null, $result = null)
821810
{
822811
if ($this->isReportEntityType($entity)) {
823812
if (is_array($sourceFileRelative)) {

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

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

1212
class Data extends AbstractSource
1313
{
14-
/**
15-
* @var array
16-
*/
1714
private $rows;
1815

1916
/**
@@ -22,17 +19,10 @@ class Data extends AbstractSource
2219
protected $_delimiter = ',';
2320

2421
/**
25-
* Field Enclosure character
26-
*
2722
* @var string
2823
*/
2924
protected $_enclosure = '';
3025

31-
/**
32-
* Read Data and detect column names
33-
*
34-
* @param string $data
35-
*/
3626
public function __construct(string $data)
3727
{
3828
$rowsData = preg_split("/\r\n|\n|\r/", $data);
@@ -41,11 +31,6 @@ public function __construct(string $data)
4131
parent::__construct($colNames);
4232
}
4333

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

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

Lines changed: 46 additions & 19 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\Filesystem\DirectoryList;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\FileSystemException;
1112
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\Filesystem;
13-
use Magento\Framework\Filesystem\Directory\WriteInterface;
13+
use Magento\Framework\Filesystem\Io\File;
1414
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
1515
use Magento\Framework\Math\Random;
1616
use Magento\ImportExport\Helper\Data as DataHelper;
@@ -40,39 +40,32 @@ class Upload
4040
*/
4141
private $random;
4242

43-
/**
44-
* @var WriteInterface
45-
*/
46-
protected $_varDirectory;
47-
4843
/**
4944
* @param FileTransferFactory $httpFactory
5045
* @param DataHelper $importExportData
5146
* @param UploaderFactory $uploaderFactory
5247
* @param Random|null $random
53-
* @param Filesystem $filesystem
5448
*/
5549
public function __construct(
5650
FileTransferFactory $httpFactory,
5751
DataHelper $importExportData,
5852
UploaderFactory $uploaderFactory,
59-
Random $random,
60-
Filesystem $filesystem
53+
Random $random
6154
) {
6255
$this->_httpFactory = $httpFactory;
6356
$this->_importExportData = $importExportData;
6457
$this->_uploaderFactory = $uploaderFactory;
65-
$this->random = $random;
66-
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
58+
$this->random = $random ?: ObjectManager::getInstance()
59+
->get(Random::class);
6760
}
6861
/**
6962
* Move uploaded file.
7063
*
71-
* @param string $entity
64+
* @param Import $import
7265
* @throws LocalizedException
73-
* @return array
66+
* @return string Source file path
7467
*/
75-
public function uploadSource(string $entity)
68+
public function uploadSource(Import $import)
7669
{
7770
/** @var $adapter \Zend_File_Transfer_Adapter_Http */
7871
$adapter = $this->_httpFactory->create();
@@ -86,17 +79,51 @@ public function uploadSource(string $entity)
8679
throw new LocalizedException($errorMessage);
8780
}
8881

82+
$entity = $import->getEntity();
8983
/** @var $uploader Uploader */
9084
$uploader = $this->_uploaderFactory->create(['fileId' => Import::FIELD_NAME_SOURCE_FILE]);
9185
$uploader->setAllowedExtensions(['csv', 'zip']);
9286
$uploader->skipDbProcessing(true);
9387
$fileName = $this->random->getRandomString(32) . '.' . $uploader->getFileExtension();
9488
try {
95-
$result = $uploader->save($this->_varDirectory->getAbsolutePath('importexport/'), $fileName);
89+
$result = $uploader->save($import->getWorkingDir(), $fileName);
9690
} catch (\Exception $e) {
9791
throw new LocalizedException(__('The file cannot be uploaded.'));
9892
}
99-
$uploader->renameFile($entity);
100-
return $result;
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;
101128
}
102129
}

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

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

77
namespace Magento\MediaStorage\Model\File;
88

9-
use Magento\Framework\App\Filesystem\DirectoryList;
109
use Magento\Framework\App\ObjectManager;
11-
use Magento\Framework\Exception\FileSystemException;
12-
use Magento\Framework\Exception\LocalizedException;
1310
use Magento\Framework\Validation\ValidationException;
1411
use Magento\MediaStorage\Model\File\Validator\Image;
1512

@@ -52,29 +49,21 @@ class Uploader extends \Magento\Framework\File\Uploader
5249
*/
5350
private $imageValidator;
5451

55-
/**
56-
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
57-
*/
58-
protected $_varDirectory;
59-
6052
/**
6153
* @param string $fileId
6254
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
6355
* @param \Magento\MediaStorage\Helper\File\Storage $coreFileStorage
6456
* @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
65-
* @param \Magento\Framework\Filesystem $filesystem
6657
*/
6758
public function __construct(
6859
$fileId,
6960
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
7061
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
71-
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator,
72-
\Magento\Framework\Filesystem $filesystem
62+
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
7363
) {
7464
$this->_coreFileStorageDb = $coreFileStorageDb;
7565
$this->_coreFileStorage = $coreFileStorage;
7666
$this->_validator = $validator;
77-
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
7867
parent::__construct($fileId);
7968
}
8069

@@ -151,45 +140,6 @@ public function validateFile()
151140
return $this->_file;
152141
}
153142

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-
193143
/**
194144
* @inheritDoc
195145
* @since 100.4.0

0 commit comments

Comments
 (0)