Skip to content

Commit

Permalink
Merge pull request magento#4282 from magento-tsg/2.2.10-develop-pr100
Browse files Browse the repository at this point in the history
[TSG] Fixes for 2.2 (pr100) (2.2.10-develop)
  • Loading branch information
zakdma authored May 30, 2019
2 parents e7e3650 + 1467929 commit 5f9b6be
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
*/
namespace Magento\Downloadable\Controller\Adminhtml\Product\Initialization\Helper\Plugin;

use Magento\Framework\App\RequestInterface;
use Magento\Downloadable\Api\Data\LinkInterfaceFactory;
use Magento\Downloadable\Api\Data\SampleInterfaceFactory;
use Magento\Downloadable\Helper\Download;
use Magento\Downloadable\Model\Link;
use Magento\Downloadable\Model\Link\Builder as LinkBuilder;
use Magento\Downloadable\Model\Product\Type;
use Magento\Downloadable\Model\ResourceModel\Sample\Collection;
use Magento\Downloadable\Model\Sample\Builder as SampleBuilder;
use Magento\Downloadable\Api\Data\SampleInterfaceFactory;
use Magento\Downloadable\Api\Data\LinkInterfaceFactory;
use Magento\Framework\App\RequestInterface;

/**
* Class for initialization downloadable info from request.
Expand Down Expand Up @@ -42,8 +46,6 @@ class Downloadable
private $linkBuilder;

/**
* Constructor
*
* @param RequestInterface $request
* @param LinkBuilder $linkBuilder
* @param SampleBuilder $sampleBuilder
Expand Down Expand Up @@ -79,14 +81,20 @@ public function afterInitialize(
\Magento\Catalog\Model\Product $product
) {
if ($downloadable = $this->request->getPost('downloadable')) {
$product->setTypeId(Type::TYPE_DOWNLOADABLE);
$product->setDownloadableData($downloadable);
$extension = $product->getExtensionAttributes();
/** @var \Magento\Downloadable\Model\Product\Type $type */
$type = $product->getTypeInstance();
$productLinks = $type->getLinks($product);
$productSamples = $type->getSamples($product);
if (isset($downloadable['link']) && is_array($downloadable['link'])) {
$links = [];
foreach ($downloadable['link'] as $linkData) {
if (!$linkData || (isset($linkData['is_delete']) && $linkData['is_delete'])) {
continue;
} else {
$linkData = $this->processLink($linkData, $productLinks);
$links[] = $this->linkBuilder->setData(
$linkData
)->build(
Expand All @@ -104,6 +112,7 @@ public function afterInitialize(
if (!$sampleData || (isset($sampleData['is_delete']) && (bool)$sampleData['is_delete'])) {
continue;
} else {
$sampleData = $this->processSample($sampleData, $productSamples);
$samples[] = $this->sampleBuilder->setData(
$sampleData
)->build(
Expand All @@ -124,4 +133,69 @@ public function afterInitialize(
}
return $product;
}

/**
* Check Links type and status.
*
* @param array $linkData
* @param Link[] $productLinks
* @return array
*/
private function processLink(array $linkData, array $productLinks): array
{
$linkId = $linkData['link_id'] ?? null;
if ($linkId && isset($productLinks[$linkId])) {
$linkData = $this->processFileStatus($linkData, $productLinks[$linkId]->getLinkFile());
$linkData['sample'] = $this->processFileStatus(
$linkData['sample'] ?? [],
$productLinks[$linkId]->getSampleFile()
);
} else {
$linkData = $this->processFileStatus($linkData, null);
$linkData['sample'] = $this->processFileStatus($linkData['sample'] ?? [], null);
}

return $linkData;
}

/**
* Check Sample type and status.
*
* @param array $sampleData
* @param Collection $productSamples
* @return array
*/
private function processSample(array $sampleData, Collection $productSamples): array
{
$sampleId = $sampleData['sample_id'] ?? null;
/** @var \Magento\Downloadable\Model\Sample $productSample */
$productSample = $sampleId ? $productSamples->getItemById($sampleId) : null;
if ($sampleId && $productSample) {
$sampleData = $this->processFileStatus($sampleData, $productSample->getSampleFile());
} else {
$sampleData = $this->processFileStatus($sampleData, null);
}

return $sampleData;
}

/**
* Compare file path from request with DB and set status.
*
* @param array $data
* @param string|null $file
* @return array
*/
private function processFileStatus(array $data, $file): array
{
if (isset($data['type']) && $data['type'] === Download::LINK_TYPE_FILE && isset($data['file']['0']['file'])) {
if ($data['file'][0]['file'] !== $file) {
$data['file'][0]['status'] = 'new';
} else {
$data['file'][0]['status'] = 'old';
}
}

return $data;
}
}
4 changes: 3 additions & 1 deletion app/code/Magento/Downloadable/Model/Link/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ private function buildSample(\Magento\Downloadable\Api\Data\LinkInterface $link,
),
\Magento\Downloadable\Api\Data\LinkInterface::class
);
if ($link->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
if ($link->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE
&& isset($sample['file'])
) {
$linkSampleFileName = $this->downloadableFile->moveFileFromTmp(
$this->getComponent()->getBaseSampleTmpPath(),
$this->getComponent()->getBaseSamplePath(),
Expand Down
80 changes: 55 additions & 25 deletions app/code/Magento/Downloadable/Model/Link/ContentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
namespace Magento\Downloadable\Model\Link;

use Magento\Downloadable\Api\Data\LinkInterface;
use Magento\Downloadable\Helper\File;
use Magento\Downloadable\Model\File\ContentValidator as FileContentValidator;
use Magento\Framework\Exception\InputException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Url\Validator as UrlValidator;

/**
* Class to validate Link Content.
*/
class ContentValidator
{
/**
Expand All @@ -22,20 +28,28 @@ class ContentValidator
*/
protected $urlValidator;

/**
* @var File
*/
private $fileHelper;

/**
* @param FileContentValidator $fileContentValidator
* @param UrlValidator $urlValidator
* @param File|null $fileHelper
*/
public function __construct(
FileContentValidator $fileContentValidator,
UrlValidator $urlValidator
UrlValidator $urlValidator,
File $fileHelper = null
) {
$this->fileContentValidator = $fileContentValidator;
$this->urlValidator = $urlValidator;
$this->fileHelper = $fileHelper ?? ObjectManager::getInstance()->get(File::class);
}

/**
* Check if link content is valid
* Check if link content is valid.
*
* @param LinkInterface $link
* @param bool $validateLinkContent
Expand Down Expand Up @@ -63,50 +77,66 @@ public function isValid(LinkInterface $link, $validateLinkContent = true, $valid
if ($validateSampleContent) {
$this->validateSampleResource($link);
}

return true;
}

/**
* Validate link resource (file or URL)
* Validate link resource (file or URL).
*
* @param LinkInterface $link
* @throws InputException
* @return void
* @throws InputException
*/
protected function validateLinkResource(LinkInterface $link)
{
if ($link->getLinkType() == 'url'
&& !$this->urlValidator->isValid($link->getLinkUrl())
) {
throw new InputException(__('Link URL must have valid format.'));
}
if ($link->getLinkType() == 'file'
&& (!$link->getLinkFileContent()
|| !$this->fileContentValidator->isValid($link->getLinkFileContent()))
) {
throw new InputException(__('Provided file content must be valid base64 encoded data.'));
if ($link->getLinkType() === 'url') {
if (!$this->urlValidator->isValid($link->getLinkUrl())) {
throw new InputException(__('Link URL must have valid format.'));
}
} elseif ($link->getLinkFileContent()) {
if (!$this->fileContentValidator->isValid($link->getLinkFileContent())) {
throw new InputException(__('Provided file content must be valid base64 encoded data.'));
}
} elseif (!$this->isFileValid($link->getBasePath() . $link->getLinkFile())) {
throw new InputException(__('Link file not found. Please try again.'));
}
}

/**
* Validate sample resource (file or URL)
* Validate sample resource (file or URL).
*
* @param LinkInterface $link
* @throws InputException
* @return void
* @throws InputException
*/
protected function validateSampleResource(LinkInterface $link)
{
if ($link->getSampleType() == 'url'
&& !$this->urlValidator->isValid($link->getSampleUrl())
) {
throw new InputException(__('Sample URL must have valid format.'));
if ($link->getSampleType() === 'url') {
if (!$this->urlValidator->isValid($link->getSampleUrl())) {
throw new InputException(__('Sample URL must have valid format.'));
}
} elseif ($link->getSampleFileContent()) {
if (!$this->fileContentValidator->isValid($link->getSampleFileContent())) {
throw new InputException(__('Provided file content must be valid base64 encoded data.'));
}
} elseif (!$this->isFileValid($link->getBaseSamplePath() . $link->getSampleFile())) {
throw new InputException(__('Link sample file not found. Please try again.'));
}
if ($link->getSampleType() == 'file'
&& (!$link->getSampleFileContent()
|| !$this->fileContentValidator->isValid($link->getSampleFileContent()))
) {
throw new InputException(__('Provided file content must be valid base64 encoded data.'));
}

/**
* Check that Links File or Sample is valid.
*
* @param string $file
* @return bool
*/
private function isFileValid(string $file): bool
{
try {
return $this->fileHelper->ensureFileInFilesystem($file);
} catch (ValidatorException $e) {
return false;
}
}
}
Loading

0 comments on commit 5f9b6be

Please sign in to comment.