forked from magedevel/async-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from pmarjan/2.3-develop
Remote file upload processor (#33)
- Loading branch information
Showing
9 changed files
with
495 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
app/code/Magento/ImportService/Model/Import/Processor/ExternalFileProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ImportService\Model\Import\Processor; | ||
|
||
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\Source\Validator; | ||
|
||
/** | ||
* CSV files processor for asynchronous import | ||
*/ | ||
class ExternalFileProcessor implements SourceProcessorInterface | ||
{ | ||
/** | ||
* @var \Magento\Framework\Filesystem | ||
*/ | ||
private $fileSystem; | ||
|
||
/** | ||
* @var \Magento\ImportService\Model\Source\Validator | ||
*/ | ||
private $validator; | ||
|
||
/** | ||
* LocalPathFileProcessor constructor | ||
* | ||
* @param FileSystem $fileSystem | ||
* @param Validator $validator | ||
*/ | ||
public function __construct( | ||
FileSystem $fileSystem, | ||
Validator $validator | ||
) { | ||
$this->fileSystem = $fileSystem; | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $source, \Magento\ImportService\Api\Data\SourceUploadResponseInterface $response) | ||
{ | ||
/** Validate the $source object */ | ||
if ($errors = $this->validator->validateRequest($source)) { | ||
throw new ImportServiceException( | ||
__('Invalid request: %1', implode(", ", $errors)) | ||
); | ||
} | ||
|
||
/** Check if the domain exists and the file within that domain exists */ | ||
if (!$this->validator->checkIfRemoteFileExists($source->getImportData())) { | ||
throw new ImportServiceException( | ||
__('Remote file %1 does not exist.', $source->getImportData()) | ||
); | ||
} | ||
|
||
/** Validate the remote file content type */ | ||
if (!$this->validator->validateMimeTypeForRemoteFile($source->getImportData())) { | ||
throw new ImportServiceException( | ||
__('Invalid mime type, expected is one of: %1', implode(", ", $this->validator->getAllowedMimeTypes())) | ||
); | ||
} | ||
|
||
/** @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); | ||
|
||
/** @var string $copyFileFullPath*/ | ||
$copyFileFullPath = $writeInterface->getAbsolutePath($workingDirectory) . $fileName; | ||
|
||
/** Attempt a copy, may throw \Magento\Framework\Exception\FileSystemException */ | ||
$writeInterface->getDriver()->copy($source->getImportData(), $copyFileFullPath); | ||
|
||
return $response->setSource($source->setImportData($fileName)) | ||
->setStatus($response::STATUS_UPLOADED); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
app/code/Magento/ImportService/Model/Source/Validator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\ImportService\Model\Source; | ||
|
||
use Magento\Framework\File\Mime\Proxy as Mime; | ||
use Magento\Framework\Filesystem\Driver\Http\Proxy as Http; | ||
|
||
/** | ||
* Class Validator | ||
*/ | ||
class Validator | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $allowedMimeTypes; | ||
|
||
/** | ||
* @var \Magento\Framework\File\Mime\Proxy | ||
*/ | ||
private $mime; | ||
|
||
/** | ||
* @var \Magento\Framework\Filesystem\Driver\Http | ||
*/ | ||
private $httpDriver; | ||
|
||
/** | ||
* @param string[] $allowedMimeTypes | ||
* @param Mime $mime | ||
* @param Http $httpDriver | ||
*/ | ||
public function __construct( | ||
array $allowedMimeTypes, | ||
Mime $mime, | ||
Http $httpDriver | ||
) { | ||
$this->allowedMimeTypes = $allowedMimeTypes; | ||
$this->mime = $mime; | ||
$this->httpDriver = $httpDriver; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getAllowedMimeTypes() | ||
{ | ||
return $this->allowedMimeTypes; | ||
} | ||
|
||
/** | ||
* @param \Magento\ImportService\Model\Source $source | ||
* @return array|null | ||
*/ | ||
public function validateRequest(\Magento\ImportService\Model\Source $source) | ||
{ | ||
$errors = []; | ||
|
||
if (!$this->validateSourceType($source)) { | ||
$errors[] = __('%1 cannot be empty', $source::SOURCE_TYPE); | ||
} | ||
|
||
if (!$this->validateImportData($source)) { | ||
$errors[] = __('%1 cannot be empty', $source::IMPORT_DATA); | ||
} | ||
|
||
if (count($errors) > 0) { | ||
return $errors; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param \Magento\ImportService\Model\Source $source | ||
* @return bool | ||
*/ | ||
public function validateSourceType(\Magento\ImportService\Model\Source $source) | ||
{ | ||
if (!$source->getSourceType()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param \Magento\ImportService\Model\Source $source | ||
* @return bool | ||
*/ | ||
public function validateImportData(\Magento\ImportService\Model\Source $source) | ||
{ | ||
if (!$source->getImportData()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $absoluteFilePath | ||
* @return bool | ||
*/ | ||
public function validateMimeTypeForLocalFile(string $absoluteFilePath) | ||
{ | ||
/** @var string $mimeType */ | ||
$mimeType = $this->mime->getMimeType($absoluteFilePath); | ||
|
||
if (!in_array($mimeType, $this->allowedMimeTypes)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @return bool | ||
*/ | ||
public function validateMimeTypeForRemoteFile(string $url) | ||
{ | ||
/** @var array $stat */ | ||
$stat = $this->httpDriver->stat($this->getSourceLocation($url)); | ||
|
||
if (!isset($stat['type']) || !in_array($stat['type'], $this->getAllowedMimeTypes())) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @return bool | ||
* @throws \Magento\Framework\Exception\FileSystemException | ||
*/ | ||
public function checkIfRemoteFileExists($url) | ||
{ | ||
if (!$this->httpDriver->isExists($this->getSourceLocation($url))) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @return string | ||
*/ | ||
private function getSourceLocation($url) | ||
{ | ||
return preg_replace("(^https?://)", "", $url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ImportService,ImportService | ||
"Remote file %1 does not exist", "Remote file %1 does not exist" | ||
"Invalid mime type, expected is one of: %1", "Invalid mime type, expected is one of: %1" | ||
"Invalid request: %1", "Invalid request: %1" | ||
"%1 cannot be empty", "%1 cannot be empty" |
Oops, something went wrong.