Skip to content

Commit

Permalink
Refactor: move Driver/Http to Model/Source/Validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarjan committed Feb 18, 2019
1 parent 57fb1bd commit 5b112e2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\Http;
use Magento\ImportService\Exception as ImportServiceException;
use Magento\ImportService\Model\Import\SourceProcessorPool;
use Magento\ImportService\Model\Source\Validator;
Expand All @@ -24,11 +23,6 @@ class ExternalFileProcessor implements SourceProcessorInterface
*/
protected $fileSystem;

/**
* @var \Magento\Framework\Filesystem\Driver\Http
*/
protected $httpDriver;

/**
* @var \Magento\ImportService\Model\Source\Validator
*/
Expand All @@ -38,16 +32,13 @@ class ExternalFileProcessor implements SourceProcessorInterface
* LocalPathFileProcessor constructor
*
* @param FileSystem $fileSystem
* @param Http $httpDriver
* @param Validator $validator
*/
public function __construct(
FileSystem $fileSystem,
Http $httpDriver,
Validator $validator
) {
$this->fileSystem = $fileSystem;
$this->httpDriver = $httpDriver;
$this->validator = $validator;
}

Expand All @@ -63,20 +54,15 @@ public function processUpload(\Magento\ImportService\Api\Data\SourceInterface $s
);
}

/** @var string $sourceLocation */
$sourceLocation = preg_replace("(^https?://)", "", $source->getImportData());

/** Check if the domain exists and the file within that domain exists */
if (!$this->httpDriver->isExists($sourceLocation)) {
if (!$this->validator->checkIfRemoteFileExists($source->getImportData())) {
throw new ImportServiceException(
__('Remote file %1 does not exist.', $source->getImportData())
);
}

/** Validate the remote file content type */
$stat = $this->httpDriver->stat($sourceLocation);
if (isset($stat['type']) && !in_array($stat['type'], $this->validator->getAllowedMimeTypes())) {

if (!$this->validator->validateMimeTypeForRemoteFile($source->getImportData())) {
throw new ImportServiceException(
__('Invalid mime type, expected is one of: %1', implode(", ", $this->validator->getAllowedMimeTypes()))
);
Expand Down
50 changes: 49 additions & 1 deletion app/code/Magento/ImportService/Model/Source/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\ImportService\Model\Source;

use Magento\Framework\File\Mime\Proxy as Mime;
use Magento\Framework\Filesystem\Driver\Http\Proxy as Http;

/**
* Class Validator
Expand All @@ -22,16 +23,24 @@ class Validator
*/
protected $mime;

/**
* @var \Magento\Framework\Filesystem\Driver\Http
*/
protected $httpDriver;

/**
* @param string[] $allowedMimeTypes
* @param Mime $mime
* @param Http $httpDriver
*/
public function __construct(
array $allowedMimeTypes,
Mime $mime
Mime $mime,
Http $httpDriver
) {
$this->allowedMimeTypes = $allowedMimeTypes;
$this->mime = $mime;
$this->httpDriver = $httpDriver;
}

/**
Expand Down Expand Up @@ -106,4 +115,43 @@ public function validateMimeTypeForLocalFile(string $absoluteFilePath)

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);
}
}

0 comments on commit 5b112e2

Please sign in to comment.