Skip to content

Commit d4aef30

Browse files
Create Base64 Upload Processor
1 parent 7d150f1 commit d4aef30

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ImportService\Model\Import\Processor;
9+
10+
use Magento\Framework\Filesystem;
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\ImportService\Exception as ImportServiceException;
13+
14+
/**
15+
* Base64 encoded data processor for asynchronous import
16+
*/
17+
class Base64EncodedDataProcessor implements SourceProcessorInterface
18+
{
19+
/**
20+
* Import Type
21+
*/
22+
const IMPORT_TYPE = 'base64_encoded_data';
23+
24+
/**
25+
* CSV Source Type
26+
*/
27+
const SOURCE_TYPE_CSV = 'csv';
28+
29+
/**
30+
* The destination directory
31+
*/
32+
const DIR_IMPORT_DESTINATION = 'import/';
33+
34+
/**
35+
* @var \Magento\Framework\Filesystem
36+
*/
37+
protected $filesystem;
38+
39+
/**
40+
* LocalPathFileProcessor constructor.
41+
* @param Filesystem $filesystem
42+
*/
43+
public function __construct(
44+
Filesystem $filesystem
45+
) {
46+
$this->filesystem = $filesystem;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $source, \Magento\ImportService\Api\Data\SourceUploadResponseInterface $response)
53+
{
54+
/** @var string $fileName */
55+
$fileName = rand();
56+
57+
/** @var string $contentFilePath */
58+
$contentFilePath = self::DIR_IMPORT_DESTINATION
59+
. $fileName
60+
. '.'
61+
. $source->getSourceType();
62+
63+
/** @var string $content */
64+
$content = base64_decode($source->getImportData());
65+
66+
/** @var Magento\Framework\Filesystem\Directory\Write $var */
67+
$var = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
68+
69+
if(!$var->writeFile($contentFilePath, $content))
70+
{
71+
/** @var array $lastError */
72+
$lastError = error_get_last();
73+
74+
/** @var string $errorMessage */
75+
$errorMessage = isset($lastError['message']) ? $lastError['message'] : '';
76+
77+
throw new ImportServiceException(
78+
__('Cannot copy the remote file: %1', $errorMessage)
79+
);
80+
}
81+
82+
/** Update source's import data */
83+
$source->setImportData($fileName);
84+
85+
return $response->setSource($source)->setSourceId($fileName)->setStatus($response::STATUS_UPLOADED);
86+
}
87+
}

app/code/Magento/ImportService/Model/Import/Processor/LocalPathFileProcessor.php

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
*/
1515
class LocalPathFileProcessor implements SourceProcessorInterface
1616
{
17+
/**
18+
* Import Type
19+
*/
20+
const IMPORT_TYPE = 'local_path';
21+
22+
/**
23+
* CSV Source Type
24+
*/
25+
const SOURCE_TYPE_CSV = 'csv';
1726

1827
/**
1928
* @var \Magento\Framework\Filesystem\Io\File

app/code/Magento/ImportService/Model/Import/SourceProcessorPool.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public function __construct($sourceProcessors = [])
4040
public function getProcessor(\Magento\ImportService\Api\Data\SourceInterface $source)
4141
{
4242
foreach ($this->sourceProcessors as $key => $processorInformation) {
43-
if ($processorInformation['import_type'] === $source->getImportType()) {
43+
if ($processorInformation['import_type'] === $source->getImportType() && in_array($source->getSourceType(), $processorInformation['source_type'])) {
4444
return $processorInformation['processor'];
4545
}
4646
}
4747
throw new ImportServiceException(
48-
__('Specified Import type "%1" is wrong.', $source->getSourceType())
48+
__('Specified Import type "%1" or Source type "%2" is wrong.', $source->getImportType(), $source->getSourceType())
4949
);
5050
}
5151
}

app/code/Magento/ImportService/etc/di.xml

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,29 @@
1212
<preference for="Magento\ImportService\Api\Data\SourceUploadResponseInterface" type="Magento\ImportService\Model\SourceUploadResponse" />
1313
<preference for="Magento\ImportService\Api\SourceRepositoryInterface"
1414
type="Magento\ImportService\Model\SourceRepository"/>
15+
1516
<type name="Magento\ImportService\Model\Import\SourceProcessorPool">
1617
<arguments>
1718
<argument name="sourceProcessors" xsi:type="array">
1819
<item name="local_path" xsi:type="array">
1920
<item name="processor" xsi:type="object">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor\Proxy</item>
20-
<item name="import_type" xsi:type="string">local_path</item>
21+
<item name="import_type" xsi:type="const">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor::IMPORT_TYPE</item>
22+
<item name="source_type" xsi:type="array">
23+
<item name="csv" xsi:type="const">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor::SOURCE_TYPE_CSV</item>
24+
</item>
25+
</item>
26+
</argument>
27+
</arguments>
28+
</type>
29+
<type name="Magento\ImportService\Model\Import\SourceProcessorPool">
30+
<arguments>
31+
<argument name="sourceProcessors" xsi:type="array">
32+
<item name="base64_encoded_data" xsi:type="array">
33+
<item name="processor" xsi:type="object">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor\Proxy</item>
34+
<item name="import_type" xsi:type="const">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::IMPORT_TYPE</item>
35+
<item name="source_type" xsi:type="array">
36+
<item name="csv" xsi:type="const">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::SOURCE_TYPE_CSV</item>
37+
</item>
2138
</item>
2239
</argument>
2340
</arguments>

0 commit comments

Comments
 (0)