Skip to content

Commit ce1b57c

Browse files
committed
ACPT-493: Upload csv with request parameter
1 parent d6b7ac7 commit ce1b57c

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Filesystem;
1818
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
1919
use Magento\Framework\Indexer\IndexerRegistry;
20+
use Magento\Framework\Math\Random;
2021
use Magento\Framework\Message\ManagerInterface;
2122
use Magento\Framework\Stdlib\DateTime\DateTime;
2223
use Magento\ImportExport\Helper\Data as DataHelper;
@@ -29,7 +30,6 @@
2930
use Magento\ImportExport\Model\Import\Entity\Factory;
3031
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
3132
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
32-
use Magento\ImportExport\Model\Import\Source\Factory as SourceFactory;
3333
use Magento\ImportExport\Model\ResourceModel\Import\Data;
3434
use Magento\ImportExport\Model\Source\Import\AbstractBehavior;
3535
use Magento\ImportExport\Model\Source\Import\Behavior\Factory as BehaviorFactory;
@@ -193,6 +193,12 @@ class Import extends AbstractModel
193193
*/
194194
private $messageManager;
195195

196+
/**
197+
* @Deprecated Property isn't used
198+
* @var Random
199+
*/
200+
private $random;
201+
196202
/**
197203
* @var Upload
198204
*/
@@ -215,6 +221,7 @@ class Import extends AbstractModel
215221
* @param DateTime $localeDate
216222
* @param array $data
217223
* @param ManagerInterface|null $messageManager
224+
* @param Random|null $random
218225
* @param Upload|null $upload
219226
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
220227
*/
@@ -235,6 +242,7 @@ public function __construct(
235242
DateTime $localeDate,
236243
array $data = [],
237244
ManagerInterface $messageManager = null,
245+
Random $random = null,
238246
Upload $upload = null
239247
) {
240248
$this->_importExportData = $importExportData;
@@ -252,6 +260,8 @@ public function __construct(
252260
$this->localeDate = $localeDate;
253261
$this->messageManager = $messageManager ?: ObjectManager::getInstance()
254262
->get(ManagerInterface::class);
263+
$this->random = $random ?: ObjectManager::getInstance()
264+
->get(Random::class);
255265
$this->upload = $upload ?: ObjectManager::getInstance()
256266
->get(Upload::class);
257267
parent::__construct($logger, $filesystem, $data);
@@ -559,7 +569,7 @@ public function uploadSource()
559569
// phpcs:ignore Magento2.Functions.DiscouragedFunction
560570
$extension = pathinfo($result['file'], PATHINFO_EXTENSION);
561571
$sourceFile = $this->getWorkingDir() . $entity . '.' . $extension;
562-
$sourceFileRelative = $this->getVarDirectory()->getRelativePath($sourceFile);
572+
$sourceFileRelative = $this->_varDirectory->getRelativePath($sourceFile);
563573
$this->_removeBom($sourceFile);
564574
$this->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
565575
return $sourceFile;
@@ -585,16 +595,6 @@ public function uploadFileAndGetSource()
585595
return $source;
586596
}
587597

588-
/**
589-
* Get Var Directory instance
590-
*
591-
* @return Filesystem\Directory\WriteInterface
592-
*/
593-
public function getVarDirectory()
594-
{
595-
return $this->_varDirectory;
596-
}
597-
598598
/**
599599
* Remove BOM from a file
600600
*
@@ -848,14 +848,4 @@ public function getDeletedItemsCount()
848848
{
849849
return $this->_getEntityAdapter()->getDeletedItemsCount();
850850
}
851-
852-
/**
853-
* Get Upload Instance
854-
*
855-
* @return Upload
856-
*/
857-
public function getUpload()
858-
{
859-
return $this->upload;
860-
}
861851
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class Base64EncodedCsvData extends AbstractSource
1919
/**
2020
* @var string
2121
*/
22-
protected $_delimiter = ',';
22+
private $delimiter = ',';
2323

2424
/**
2525
* Field Enclosure character
2626
*
2727
* @var string
2828
*/
29-
protected $_enclosure = '';
29+
private $enclosure = '';
3030

3131
/**
3232
* Read Data and detect column names
@@ -38,7 +38,7 @@ public function __construct(string $file)
3838
// phpcs:ignore Magento2.Functions.DiscouragedFunction
3939
$source = trim(base64_decode($file));
4040
$rowsData = preg_split("/\r\n|\n|\r/", $source);
41-
$colNames = explode(',', $rowsData[0]);
41+
$colNames = explode($this->delimiter, $rowsData[0]);
4242
$this->rows = array_splice($rowsData, 1);
4343
parent::__construct($colNames);
4444
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ class Upload
2323
/**
2424
* @var FileTransferFactory
2525
*/
26-
protected $_httpFactory;
26+
private $httpFactory;
2727

2828
/**
2929
* @var DataHelper
3030
*/
31-
protected $_importExportData = null;
31+
private $importExportData = null;
3232

3333
/**
3434
* @var UploaderFactory
3535
*/
36-
protected $_uploaderFactory;
36+
private $uploaderFactory;
3737

3838
/**
3939
* @var Random
@@ -59,11 +59,11 @@ public function __construct(
5959
Random $random,
6060
Filesystem $filesystem
6161
) {
62-
$this->_httpFactory = $httpFactory;
63-
$this->_importExportData = $importExportData;
64-
$this->_uploaderFactory = $uploaderFactory;
62+
$this->httpFactory = $httpFactory;
63+
$this->importExportData = $importExportData;
64+
$this->uploaderFactory = $uploaderFactory;
6565
$this->random = $random;
66-
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
66+
$this->varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
6767
}
6868
/**
6969
* Move uploaded file.
@@ -75,24 +75,24 @@ public function __construct(
7575
public function uploadSource(string $entity)
7676
{
7777
/** @var $adapter \Zend_File_Transfer_Adapter_Http */
78-
$adapter = $this->_httpFactory->create();
78+
$adapter = $this->httpFactory->create();
7979
if (!$adapter->isValid(Import::FIELD_NAME_SOURCE_FILE)) {
8080
$errors = $adapter->getErrors();
8181
if ($errors[0] == \Zend_Validate_File_Upload::INI_SIZE) {
82-
$errorMessage = $this->_importExportData->getMaxUploadSizeMessage();
82+
$errorMessage = $this->importExportData->getMaxUploadSizeMessage();
8383
} else {
8484
$errorMessage = __('The file was not uploaded.');
8585
}
8686
throw new LocalizedException($errorMessage);
8787
}
8888

8989
/** @var $uploader Uploader */
90-
$uploader = $this->_uploaderFactory->create(['fileId' => Import::FIELD_NAME_SOURCE_FILE]);
90+
$uploader = $this->uploaderFactory->create(['fileId' => Import::FIELD_NAME_SOURCE_FILE]);
9191
$uploader->setAllowedExtensions(['csv', 'zip']);
9292
$uploader->skipDbProcessing(true);
9393
$fileName = $this->random->getRandomString(32) . '.' . $uploader->getFileExtension();
9494
try {
95-
$result = $uploader->save($this->_varDirectory->getAbsolutePath('importexport/'), $fileName);
95+
$result = $uploader->save($this->varDirectory->getAbsolutePath('importexport/'), $fileName);
9696
} catch (\Exception $e) {
9797
throw new LocalizedException(__('The file cannot be uploaded.'));
9898
}

app/code/Magento/ImportExport/Test/Unit/Helper/ReportTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ public function testGetSummaryStats()
221221
$localeDate,
222222
[],
223223
null,
224+
null,
224225
$upload
225226
);
226227
$import->setData('entity', 'catalog_product');

app/code/Magento/ImportExport/Test/Unit/Model/ImportTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ protected function setUp(): void
251251
$this->dateTime,
252252
[],
253253
null,
254+
null,
254255
$this->upload
255256
]
256257
)
@@ -568,6 +569,7 @@ public function testInvalidateIndex()
568569
$this->dateTime,
569570
[],
570571
null,
572+
null,
571573
$this->upload
572574
);
573575

@@ -604,6 +606,7 @@ public function testInvalidateIndexWithoutIndexers()
604606
$this->dateTime,
605607
[],
606608
null,
609+
null,
607610
$this->upload
608611
);
609612

@@ -637,6 +640,7 @@ public function testGetKnownEntity()
637640
$this->dateTime,
638641
[],
639642
null,
643+
null,
640644
$this->upload
641645
);
642646

@@ -676,6 +680,7 @@ public function testGetUnknownEntity($entity)
676680
$this->dateTime,
677681
[],
678682
null,
683+
null,
679684
$this->upload
680685
);
681686

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Exception\FileSystemException;
1212
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Filesystem;
1314
use Magento\Framework\Validation\ValidationException;
1415
use Magento\MediaStorage\Model\File\Validator\Image;
1516

@@ -55,26 +56,28 @@ class Uploader extends \Magento\Framework\File\Uploader
5556
/**
5657
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
5758
*/
58-
protected $_varDirectory;
59+
private $varDirectory;
5960

6061
/**
6162
* @param string $fileId
6263
* @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
6364
* @param \Magento\MediaStorage\Helper\File\Storage $coreFileStorage
6465
* @param \Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator
65-
* @param \Magento\Framework\Filesystem $filesystem
66+
* @param \Magento\Framework\Filesystem|null $filesystem
6667
*/
6768
public function __construct(
6869
$fileId,
6970
\Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
7071
\Magento\MediaStorage\Helper\File\Storage $coreFileStorage,
7172
\Magento\MediaStorage\Model\File\Validator\NotProtectedExtension $validator,
72-
\Magento\Framework\Filesystem $filesystem
73+
\Magento\Framework\Filesystem $filesystem = null
7374
) {
7475
$this->_coreFileStorageDb = $coreFileStorageDb;
7576
$this->_coreFileStorage = $coreFileStorage;
7677
$this->_validator = $validator;
77-
$this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
78+
$filesystem = $filesystem ?: ObjectManager::getInstance()
79+
->get(\Magento\Framework\Filesystem::class);
80+
$this->varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_IMPORT_EXPORT);
7881
parent::__construct($fileId);
7982
}
8083

@@ -169,22 +172,22 @@ public function renameFile(string $entity)
169172
}
170173

171174
if (!$extension) {
172-
$this->_varDirectory->delete($uploadedFile);
175+
$this->varDirectory->delete($uploadedFile);
173176
throw new LocalizedException(__('The file you uploaded has no extension.'));
174177
}
175-
$sourceFile = $this->_varDirectory->getAbsolutePath('importexport/') . $entity;
178+
$sourceFile = $this->varDirectory->getAbsolutePath('importexport/') . $entity;
176179

177180
$sourceFile .= '.' . $extension;
178-
$sourceFileRelative = $this->_varDirectory->getRelativePath($sourceFile);
181+
$sourceFileRelative = $this->varDirectory->getRelativePath($sourceFile);
179182

180183
if (strtolower($uploadedFile) != strtolower($sourceFile)) {
181-
if ($this->_varDirectory->isExist($sourceFileRelative)) {
182-
$this->_varDirectory->delete($sourceFileRelative);
184+
if ($this->varDirectory->isExist($sourceFileRelative)) {
185+
$this->varDirectory->delete($sourceFileRelative);
183186
}
184187

185188
try {
186-
$this->_varDirectory->renameFile(
187-
$this->_varDirectory->getRelativePath($uploadedFile),
189+
$this->varDirectory->renameFile(
190+
$this->varDirectory->getRelativePath($uploadedFile),
188191
$sourceFileRelative
189192
);
190193
} catch (FileSystemException $e) {

0 commit comments

Comments
 (0)