diff --git a/app/code/Magento/ImportService/Model/Import/Processor/Base64EncodedDataProcessor.php b/app/code/Magento/ImportService/Model/Import/Processor/Base64EncodedDataProcessor.php
index d97dcc758da..f6aaa7dfe6a 100644
--- a/app/code/Magento/ImportService/Model/Import/Processor/Base64EncodedDataProcessor.php
+++ b/app/code/Magento/ImportService/Model/Import/Processor/Base64EncodedDataProcessor.php
@@ -9,7 +9,8 @@
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
-use Magento\ImportService\Exception as ImportServiceException;
+use Magento\ImportService\Api\Data\SourceInterface;
+use Magento\ImportService\Api\Data\SourceUploadResponseInterface;
/**
* Base64 encoded data processor for asynchronous import
@@ -22,66 +23,31 @@ class Base64EncodedDataProcessor implements SourceProcessorInterface
const IMPORT_TYPE = 'base64_encoded_data';
/**
- * CSV Source Type
+ * @var PersistentSourceProcessor
*/
- const SOURCE_TYPE_CSV = 'csv';
+ private $persistantUploader;
/**
- * The destination directory
- */
- const DIR_IMPORT_DESTINATION = 'import/';
-
- /**
- * @var \Magento\Framework\Filesystem
- */
- private $filesystem;
-
- /**
- * LocalPathFileProcessor constructor.
- * @param Filesystem $filesystem
+ * @param PersistentSourceProcessor $persistantUploader
*/
public function __construct(
- Filesystem $filesystem
+ PersistentSourceProcessor $persistantUploader
) {
- $this->filesystem = $filesystem;
+ $this->persistantUploader = $persistantUploader;
}
/**
* {@inheritdoc}
*/
- public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $source, \Magento\ImportService\Api\Data\SourceUploadResponseInterface $response)
+ public function processUpload(SourceInterface $source, SourceUploadResponseInterface $response)
{
- /** @var string $fileName */
- $fileName = rand();
-
- /** @var string $contentFilePath */
- $contentFilePath = self::DIR_IMPORT_DESTINATION
- . $fileName
- . '.'
- . $source->getSourceType();
-
/** @var string $content */
$content = base64_decode($source->getImportData());
- /** @var Magento\Framework\Filesystem\Directory\Write $var */
- $var = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
-
- if(!$var->writeFile($contentFilePath, $content))
- {
- /** @var array $lastError */
- $lastError = error_get_last();
-
- /** @var string $errorMessage */
- $errorMessage = isset($lastError['message']) ? $lastError['message'] : '';
-
- throw new ImportServiceException(
- __('Cannot copy the remote file: %1', $errorMessage)
- );
- }
-
- /** Update source's import data */
- $source->setImportData($fileName);
+ /** Set downloaded data */
+ $source->setImportData($content);
- return $response->setSource($source)->setSourceId($fileName)->setStatus($response::STATUS_UPLOADED);
+ /** process source and get response details */
+ return $this->persistantUploader->processUpload($source, $response);
}
}
diff --git a/app/code/Magento/ImportService/Model/Import/Processor/ExternalFileProcessor.php b/app/code/Magento/ImportService/Model/Import/Processor/ExternalFileProcessor.php
index 43b4938cd7d..617d8f10936 100644
--- a/app/code/Magento/ImportService/Model/Import/Processor/ExternalFileProcessor.php
+++ b/app/code/Magento/ImportService/Model/Import/Processor/ExternalFileProcessor.php
@@ -7,11 +7,14 @@
namespace Magento\ImportService\Model\Import\Processor;
+use Magento\Framework\Filesystem\Io\File;
+use Magento\ImportService\Api\Data\SourceInterface;
+use Magento\ImportService\Api\Data\SourceUploadResponseInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
-use Magento\ImportService\Exception as ImportServiceException;
-use Magento\ImportService\Model\Import\SourceProcessorPool;
+use Magento\ImportService\Model\Import\SourceTypePool;
use Magento\ImportService\Model\Source\Validator;
+use Magento\ImportService\ImportServiceException;
/**
* CSV files processor for asynchronous import
@@ -19,25 +22,38 @@
class ExternalFileProcessor implements SourceProcessorInterface
{
/**
- * @var \Magento\Framework\Filesystem
+ * Import Type
+ */
+ const IMPORT_TYPE = 'external';
+
+ /**
+ * @var PersistentSourceProcessor
+ */
+ private $persistantUploader;
+
+ /**
+ * @var Filesystem
*/
private $fileSystem;
/**
- * @var \Magento\ImportService\Model\Source\Validator
+ * @var Validator
*/
private $validator;
/**
* LocalPathFileProcessor constructor
*
- * @param FileSystem $fileSystem
+ * @param PersistentSourceProcessor $persistantUploader
+ * @param Filesystem $fileSystem
* @param Validator $validator
*/
public function __construct(
- FileSystem $fileSystem,
+ PersistentSourceProcessor $persistantUploader,
+ Filesystem $fileSystem,
Validator $validator
) {
+ $this->persistantUploader = $persistantUploader;
$this->fileSystem = $fileSystem;
$this->validator = $validator;
}
@@ -68,25 +84,16 @@ public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $s
);
}
- /** @var string $workingDirectory */
- $workingDirectory = SourceProcessorPool::WORKING_DIR;
-
- /** @var string $fileName */
- $fileName = uniqid() . '.' . $source->getSourceType();
-
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $writeInterface */
- $writeInterface = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
-
- /** If the directory is not present, it will be created */
- $writeInterface->create($workingDirectory);
+ $writeInterface = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT);
- /** @var string $copyFileFullPath*/
- $copyFileFullPath = $writeInterface->getAbsolutePath($workingDirectory) . $fileName;
+ /** read content from external link */
+ $content = $writeInterface->getDriver()->fileGetContents($source->getImportData());
- /** Attempt a copy, may throw \Magento\Framework\Exception\FileSystemException */
- $writeInterface->getDriver()->copy($source->getImportData(), $copyFileFullPath);
+ /** Set downloaded data */
+ $source->setImportData($content);
- return $response->setSource($source->setImportData($fileName))
- ->setStatus($response::STATUS_UPLOADED);
+ /** process source and get response details */
+ return $this->persistantUploader->processUpload($source, $response);
}
}
diff --git a/app/code/Magento/ImportService/Model/Import/Processor/LocalPathFileProcessor.php b/app/code/Magento/ImportService/Model/Import/Processor/LocalPathFileProcessor.php
index e178d81fa15..756a533b764 100644
--- a/app/code/Magento/ImportService/Model/Import/Processor/LocalPathFileProcessor.php
+++ b/app/code/Magento/ImportService/Model/Import/Processor/LocalPathFileProcessor.php
@@ -7,17 +7,12 @@
namespace Magento\ImportService\Model\Import\Processor;
-use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Filesystem\Io\File;
use Magento\ImportService\Api\Data\SourceInterface;
use Magento\ImportService\Api\Data\SourceUploadResponseInterface;
-use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
-use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem;
-use Magento\ImportService\Api\SourceRepositoryInterface;
-use Magento\ImportService\ImportServiceException;
-use Magento\ImportService\Model\Import\SourceTypesValidatorInterface;
+use Magento\ImportService\Model\Import\SourceTypePool;
/**
* CSV files processor for asynchronous import
@@ -30,14 +25,9 @@ class LocalPathFileProcessor implements SourceProcessorInterface
const IMPORT_TYPE = 'local_path';
/**
- * CSV Source Type
+ * @var PersistentSourceProcessor
*/
- const SOURCE_TYPE_CSV = 'csv';
-
- /**
- * @var SourceTypesValidatorInterface
- */
- private $sourceTypesValidator;
+ private $persistantUploader;
/**
* @var File
@@ -49,158 +39,41 @@ class LocalPathFileProcessor implements SourceProcessorInterface
*/
private $fileSystem;
- /**
- * @var WriteInterface
- */
- private $directoryWrite;
-
- /**
- * @var SourceRepositoryInterface
- */
- private $sourceRepository;
-
- /**
- * @var string
- */
- private $newFileName;
-
- /**
- * @var SourceInterface
- */
- private $source;
-
/**
* LocalPathFileProcessor constructor
*
+ * @param PersistentSourceProcessor $persistantUploader
* @param File $fileSystemIo
* @param Filesystem $fileSystem
- * @param SourceTypesValidatorInterface $sourceTypesValidator
- * @param SourceRepositoryInterface $sourceRepository
*/
public function __construct(
+ PersistentSourceProcessor $persistantUploader,
File $fileSystemIo,
- Filesystem $fileSystem,
- SourceTypesValidatorInterface $sourceTypesValidator,
- SourceRepositoryInterface $sourceRepository
+ Filesystem $fileSystem
) {
+ $this->persistantUploader = $persistantUploader;
$this->fileSystemIo = $fileSystemIo;
- $this->sourceTypesValidator = $sourceTypesValidator;
$this->fileSystem = $fileSystem;
- $this->sourceRepository = $sourceRepository;
}
/**
- * Uploads process
- *
- * @inheritdoc
- * @throws FileSystemException
- * @throws ImportServiceException
+ * {@inheritdoc}
*/
public function processUpload(SourceInterface $source, SourceUploadResponseInterface $response)
{
- $this->source = $source;
- try {
- $this->validateSource();
- $this->saveFile();
- $source = $this->saveSource();
- $response->setStatus($source->getStatus());
- $response->setSourceId($source->getSourceId());
- } catch (CouldNotSaveException $e) {
- $this->removeFile($source->getImportData());
- throw new ImportServiceException(__($e->getMessage()));
- }
-
- return $response;
- }
-
- /**
- * Saves source in DB
- *
- * @return SourceInterface
- */
- private function saveSource()
- {
- $this->source->setImportData($this->getNewFileName());
- $this->source->setStatus(SourceInterface::STATUS_UPLOADED);
-
- return $this->sourceRepository->save($this->source);
- }
-
- /**
- * Saves file at the storage
- *
- * @return string
- * @throws FileSystemException
- */
- private function saveFile()
- {
- $this->directoryWrite->copyFile(
- $this->source->getImportData(),
- $this->getNewFileName()
- );
+ /** @var \Magento\Framework\Filesystem\Directory\Write $write */
+ $write = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT);
- return $this->getNewFileName();
- }
-
- /**
- * Generates new file name
- *
- * @return string
- */
- private function getNewFileName()
- {
- if (!$this->newFileName) {
- $this->newFileName = self::IMPORT_SOURCE_FILE_PATH . '/'
- . uniqid()
- . '.' . $this->source->getSourceType();
- }
-
- return $this->newFileName;
- }
-
- /**
- * Provides configured directoryWrite
- *
- * @return WriteInterface
- * @throws FileSystemException
- */
- private function getDirectoryWrite()
- {
- if (!$this->directoryWrite) {
- $this->directoryWrite = $this->fileSystem
- ->getDirectoryWrite(DirectoryList::ROOT);
- }
+ /** create absolute path */
+ $absoluteSourcePath = $write->getAbsolutePath($source->getImportData());
- return $this->directoryWrite;
- }
+ /** read content from system */
+ $content = $this->fileSystemIo->read($absoluteSourcePath);
- /**
- * Validates source
- *
- * @throws FileSystemException
- * @throws ImportServiceException
- */
- private function validateSource()
- {
- $absoluteSourcePath = $this->getDirectoryWrite()
- ->getAbsolutePath($this->source->getImportData());
- if (!$this->fileSystemIo->read($absoluteSourcePath)) {
- throw new ImportServiceException(
- __("Cannot read from file system. File not existed or cannot be read")
- );
- }
- $this->sourceTypesValidator->execute($this->source);
- }
+ /** Set downloaded data */
+ $source->setImportData($content);
- /**
- * Removes source
- *
- * @param string $filename
- * @return bool
- * @throws FileSystemException
- */
- private function removeFile($filename)
- {
- return $this->getDirectoryWrite()->delete($filename);
+ /** process source and get response details */
+ return $this->persistantUploader->processUpload($source, $response);
}
}
diff --git a/app/code/Magento/ImportService/Model/Import/Processor/PersistentSourceProcessor.php b/app/code/Magento/ImportService/Model/Import/Processor/PersistentSourceProcessor.php
new file mode 100644
index 00000000000..c3892146211
--- /dev/null
+++ b/app/code/Magento/ImportService/Model/Import/Processor/PersistentSourceProcessor.php
@@ -0,0 +1,51 @@
+sourceTypePool = $sourceTypePool;
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @throws ImportServiceException
+ * @return SourceTypeInterface
+ */
+ public function processUpload(SourceInterface $source, SourceUploadResponseInterface $response)
+ {
+ /** @var \Magento\ImportService\Model\Import\Type\SourceTypeInterface $sourceType */
+ $sourceType = $this->sourceTypePool->getSourceType($source);
+
+ /** save processed content get updated source object */
+ $source = $sourceType->save($source);
+
+ /** return response with details */
+ return $response->setSource($source)->setSourceId($source->getSourceId())->setStatus($source->getStatus());
+ }
+}
diff --git a/app/code/Magento/ImportService/Model/Import/Processor/SourceProcessorInterface.php b/app/code/Magento/ImportService/Model/Import/Processor/SourceProcessorInterface.php
index 5864feb6dfc..90f8a45700b 100644
--- a/app/code/Magento/ImportService/Model/Import/Processor/SourceProcessorInterface.php
+++ b/app/code/Magento/ImportService/Model/Import/Processor/SourceProcessorInterface.php
@@ -17,9 +17,6 @@
*/
interface SourceProcessorInterface
{
- // todo discuss the name of constant
- const IMPORT_SOURCE_FILE_PATH = "var/import";
-
/**
* @param SourceInterface $source
* @param SourceUploadResponseInterface $response
diff --git a/app/code/Magento/ImportService/Model/Import/SourceProcessorPool.php b/app/code/Magento/ImportService/Model/Import/SourceProcessorPool.php
index dcf844df019..a37edb70045 100644
--- a/app/code/Magento/ImportService/Model/Import/SourceProcessorPool.php
+++ b/app/code/Magento/ImportService/Model/Import/SourceProcessorPool.php
@@ -16,13 +16,6 @@
*/
class SourceProcessorPool
{
- /**
- * Working directory
- *
- * @var string
- */
- const WORKING_DIR = 'importservice/';
-
/**
* @var array
*/
@@ -47,12 +40,12 @@ public function __construct($sourceProcessors = [])
public function getProcessor(SourceInterface $source)
{
foreach ($this->sourceProcessors as $key => $processorInformation) {
- if ($processorInformation['import_type'] === $source->getImportType() && in_array($source->getSourceType(), $processorInformation['source_type'])) {
+ if ($processorInformation['import_type'] === $source->getImportType()) {
return $processorInformation['processor'];
}
}
throw new ImportServiceException(
- __('Specified Import type "%1" or Source type "%2" is wrong.', $source->getImportType(), $source->getSourceType())
+ __('Specified Import type "%1" is wrong.', $source->getImportType())
);
}
}
diff --git a/app/code/Magento/ImportService/Model/Import/SourceTypePool.php b/app/code/Magento/ImportService/Model/Import/SourceTypePool.php
new file mode 100644
index 00000000000..d99889fd28e
--- /dev/null
+++ b/app/code/Magento/ImportService/Model/Import/SourceTypePool.php
@@ -0,0 +1,51 @@
+sourceTypes = $sourceTypes;
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @throws ImportServiceException
+ * @return SourceTypeInterface
+ */
+ public function getSourceType(SourceInterface $source)
+ {
+ foreach ($this->sourceTypes as $key => $object) {
+ if ($source->getSourceType() == $key) {
+ return $object;
+ }
+ }
+ throw new ImportServiceException(
+ __('Specified Source type "%1" is wrong.', $source->getSourceType())
+ );
+ }
+}
diff --git a/app/code/Magento/ImportService/Model/Import/Type/FileSourceType.php b/app/code/Magento/ImportService/Model/Import/Type/FileSourceType.php
new file mode 100644
index 00000000000..fabbc53ae7b
--- /dev/null
+++ b/app/code/Magento/ImportService/Model/Import/Type/FileSourceType.php
@@ -0,0 +1,110 @@
+sourceRepository = $sourceRepository;
+ $this->filesystem = $filesystem;
+ $this->sourceType = $sourceType;
+ $this->mime = $mime;
+ }
+
+ /**
+ * generate file name with source type
+ *
+ * @return string
+ */
+ private function generateFileName()
+ {
+ return uniqid() . '.' . $this->sourceType;
+ }
+
+ /**
+ * save source content
+ *
+ * @param SourceInterface $source
+ * @throws ImportServiceException
+ * @return SourceInterface
+ */
+ public function save(SourceInterface $source)
+ {
+ /** @var string $fileName */
+ $fileName = $this->generateFileName();
+
+ /** @var string $contentFilePath */
+ $contentFilePath = SourceTypeInterface::IMPORT_SOURCE_FILE_PATH . $fileName;
+
+ /** @var Magento\Framework\Filesystem\Directory\Write $var */
+ $var = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
+
+ if(!$var->writeFile($contentFilePath, $source->getImportData()))
+ {
+ /** @var array $lastError */
+ $lastError = error_get_last();
+
+ /** @var string $errorMessage */
+ $errorMessage = isset($lastError['message']) ? $lastError['message'] : '';
+
+ throw new ImportServiceException(
+ __('Cannot create file with given source: %1', $errorMessage)
+ );
+ }
+
+ /** set updated data to source */
+ $source->setImportData($fileName)->setStatus(SourceInterface::STATUS_UPLOADED);
+
+ /** save processed source with status */
+ $source = $this->sourceRepository->save($source);
+
+ return $source;
+ }
+}
diff --git a/app/code/Magento/ImportService/Model/Import/Type/SourceTypeInterface.php b/app/code/Magento/ImportService/Model/Import/Type/SourceTypeInterface.php
new file mode 100644
index 00000000000..39741206445
--- /dev/null
+++ b/app/code/Magento/ImportService/Model/Import/Type/SourceTypeInterface.php
@@ -0,0 +1,29 @@
+
-
+
+
+ csv
+ text/csv
+
+
+
+
+
+ - Magento\ImportService\Model\Import\Type\Csv
+
+
+
-
- Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor\Proxy
- Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor::IMPORT_TYPE
- -
-
- Magento\ImportService\Model\Import\Processor\LocalPathFileProcessor::SOURCE_TYPE_CSV
-
-
- Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor\Proxy
- Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::IMPORT_TYPE
- -
-
- Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::SOURCE_TYPE_CSV
-
-
- Magento\ImportService\Model\Import\Processor\ExternalFileProcessor\Proxy
- - external
- -
-
- Magento\ImportService\Model\Import\Processor\Base64EncodedDataProcessor::SOURCE_TYPE_CSV
-
+ - Magento\ImportService\Model\Import\Processor\ExternalFileProcessor::IMPORT_TYPE