Skip to content

Commit c543b0f

Browse files
author
Volodymyr Kublytskyi
authored
Merge pull request #45 from hitarthpattani/base64-upload-processor
Create Base64 Upload Processor
2 parents f16221f + f5a2bc8 commit c543b0f

File tree

4 files changed

+116
-6
lines changed

4 files changed

+116
-6
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+
private $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

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
*/
2525
class LocalPathFileProcessor implements SourceProcessorInterface
2626
{
27+
/**
28+
* Import Type
29+
*/
30+
const IMPORT_TYPE = 'local_path';
31+
32+
/**
33+
* CSV Source Type
34+
*/
35+
const SOURCE_TYPE_CSV = 'csv';
36+
2737
/**
2838
* @var SourceTypesValidatorInterface
2939
*/
@@ -32,7 +42,7 @@ class LocalPathFileProcessor implements SourceProcessorInterface
3242
/**
3343
* @var File
3444
*/
35-
protected $fileSystemIo;
45+
private $fileSystemIo;
3646

3747
/**
3848
* @var Filesystem

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public function __construct($sourceProcessors = [])
4747
public function getProcessor(SourceInterface $source)
4848
{
4949
foreach ($this->sourceProcessors as $key => $processorInformation) {
50-
if ($processorInformation['import_type'] === $source->getImportType()) {
50+
if ($processorInformation['import_type'] === $source->getImportType() && in_array($source->getSourceType(), $processorInformation['source_type'])) {
5151
return $processorInformation['processor'];
5252
}
5353
}
5454
throw new ImportServiceException(
55-
__('Specified Import type "%1" is wrong.', $source->getImportType())
55+
__('Specified Import type "%1" or Source type "%2" is wrong.', $source->getImportType(), $source->getSourceType())
5656
);
5757
}
5858
}

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,30 @@
1515
type="Magento\ImportService\Model\SourceUploadResponse" />
1616
<preference for="Magento\ImportService\Api\SourceRepositoryInterface"
1717
type="Magento\ImportService\Model\SourceRepository"/>
18+
1819
<type name="Magento\ImportService\Model\Import\SourceProcessorPool">
1920
<arguments>
2021
<argument name="sourceProcessors" xsi:type="array">
2122
<item name="local_path" xsi:type="array">
22-
<item name="processor"
23-
xsi:type="object">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor\Proxy</item>
24-
<item name="import_type" xsi:type="string">local_path</item>
23+
<item name="processor" xsi:type="object">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor\Proxy</item>
24+
<item name="import_type" xsi:type="const">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor::IMPORT_TYPE</item>
25+
<item name="source_type" xsi:type="array">
26+
<item name="csv" xsi:type="const">Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor::SOURCE_TYPE_CSV</item>
27+
</item>
28+
</item>
29+
<item name="base64_encoded_data" xsi:type="array">
30+
<item name="processor" xsi:type="object">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor\Proxy</item>
31+
<item name="import_type" xsi:type="const">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::IMPORT_TYPE</item>
32+
<item name="source_type" xsi:type="array">
33+
<item name="csv" xsi:type="const">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::SOURCE_TYPE_CSV</item>
34+
</item>
2535
</item>
2636
<item name="external" xsi:type="array">
2737
<item name="processor" xsi:type="object">Magento\ImportService\Model\Import\Processor\ExternalFileProcessor\Proxy</item>
2838
<item name="import_type" xsi:type="string">external</item>
39+
<item name="source_type" xsi:type="array">
40+
<item name="csv" xsi:type="const">Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::SOURCE_TYPE_CSV</item>
41+
</item>
2942
</item>
3043
</argument>
3144
</arguments>

0 commit comments

Comments
 (0)