Skip to content

404 missing file while downloading file product custom option expired by Quote Lifetime & Enable Clear Shopping Cart #39890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: 2.4-develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Option/Type/File.php
Original file line number Diff line number Diff line change
@@ -6,13 +6,14 @@

namespace Magento\Catalog\Model\Product\Option\Type;

use Magento\Catalog\Model\Product\Exception as ProductException;
use Magento\Catalog\Helper\Product as ProductHelper;
use Magento\Catalog\Model\Product\Exception as ProductException;
use Magento\Catalog\Model\Product\Type\AbstractType;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\App\ObjectManager;

/**
* Catalog product option file type
@@ -353,10 +354,17 @@ public function getFormattedOptionValue($optionValue)
if ($value === null) {
return $optionValue;
}
$optionId = str_replace(
AbstractType::OPTION_PREFIX,
'',
(string)$this->getConfigurationItemOption()->getCode()
);
$customOptionUrlParams = $this->getCustomOptionUrlParams()
? $this->getCustomOptionUrlParams()
: [
'id' => $this->getConfigurationItemOption()->getId(),
'item' => $this->getConfigurationItemOption()->getItemId(),
'option' => $optionId,
'key' => $value['secret_key']
];

92 changes: 77 additions & 15 deletions app/code/Magento/Sales/Controller/Download/DownloadCustomOption.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2014 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Sales\Controller\Download;

use Magento\Catalog\Model\Product\Type\AbstractType;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Action\Context;
use Magento\Catalog\Model\Product\Type\AbstractType;
use Magento\Framework\Controller\Result\ForwardFactory;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory;

/**
* Download Custom Option Controller
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DownloadCustomOption extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
{
/**
@@ -39,19 +45,26 @@ class DownloadCustomOption extends \Magento\Framework\App\Action\Action implemen
*/
private $serializer;

/**
* @var CollectionFactory
*/
private $itemCollectionFactory;

/**
* @param Context $context
* @param ForwardFactory $resultForwardFactory
* @param \Magento\Sales\Model\Download $download
* @param \Magento\Framework\Unserialize\Unserialize $unserialize
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
* @param CollectionFactory $itemCollectionFactory
*/
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory,
\Magento\Sales\Model\Download $download,
\Magento\Framework\Unserialize\Unserialize $unserialize,
?\Magento\Framework\Serialize\Serializer\Json $serializer = null
?\Magento\Framework\Serialize\Serializer\Json $serializer = null,
?CollectionFactory $itemCollectionFactory = null
) {
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
@@ -60,6 +73,9 @@ public function __construct(
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(
\Magento\Framework\Serialize\Serializer\Json::class
);
$this->itemCollectionFactory = $itemCollectionFactory ?: ObjectManager::getInstance()->get(
CollectionFactory::class
);
}

/**
@@ -72,6 +88,8 @@ public function __construct(
public function execute()
{
$quoteItemOptionId = $this->getRequest()->getParam('id');
$quoteItemId = $this->getRequest()->getParam('item');
$optionId = $this->getRequest()->getParam('option');
/** @var $option \Magento\Quote\Model\Quote\Item\Option */
$option = $this->_objectManager->create(
\Magento\Quote\Model\Quote\Item\Option::class
@@ -80,9 +98,35 @@ public function execute()
$resultForward = $this->resultForwardFactory->create();

if (!$option->getId()) {
$optionData = $this->getOptionFromSalesItem($quoteItemId, $optionId);
} else {
$optionData = $this->getOptionFromQuoteItem($option);
}

if ($optionData) {
try {
$info = $this->serializer->unserialize($optionData);
if ($this->getRequest()->getParam('key') != $info['secret_key']) {
return $resultForward->forward('noroute');
}
return $this->download->createResponse($info);
} catch (\Exception $e) {
return $resultForward->forward('noroute');
}
} else {
return $resultForward->forward('noroute');
}
$this->endExecute();
}

/**
* Get custom option data from quote item option
*
* @param \Magento\Quote\Model\Quote\Item\Option $option
* @return string|null
*/
protected function getOptionFromQuoteItem($option)
{
$optionId = null;
if ($option->getCode() && strpos($option->getCode(), AbstractType::OPTION_PREFIX) === 0) {
$optionId = str_replace(AbstractType::OPTION_PREFIX, '', $option->getCode());
@@ -100,19 +144,37 @@ public function execute()
}

if ($productOption->getId() && $productOption->getType() != 'file') {
return $resultForward->forward('noroute');
return null;
}
return $option->getValue();
}

try {
$info = $this->serializer->unserialize($option->getValue());
if ($this->getRequest()->getParam('key') != $info['secret_key']) {
return $resultForward->forward('noroute');
/**
* Get custom option data from sales item
*
* @param string|null $quoteItemId
* @param string|null $optionId
* @return string|null
*/
protected function getOptionFromSalesItem($quoteItemId, $optionId)
{
if (!$quoteItemId || !$optionId) {
return null;
}
$collection = $this->itemCollectionFactory->create();
$quoteItem = $collection->addFieldToFilter(OrderItemInterface::QUOTE_ITEM_ID, $quoteItemId)->getFirstItem();
if ($quoteItem && $quoteItem->getId()) {
try {
$options = $quoteItem->getProductOptionByCode('options');
$key = array_search($optionId, array_column($options, 'option_id'));
if ($key !== false && $options[$key]['option_value'] && $options[$key]['option_type'] == 'file') {
return $options[$key]['option_value'];
}
} catch (\Exception $e) {
return null;
}
return $this->download->createResponse($info);
} catch (\Exception $e) {
return $resultForward->forward('noroute');
}
$this->endExecute();
return null;
}

/**
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2015 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

@@ -17,6 +17,7 @@
use Magento\Quote\Model\Quote\Item\Option;
use Magento\Sales\Controller\Download\DownloadCustomOption;
use Magento\Sales\Model\Download;
use Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

@@ -77,6 +78,11 @@ class DownloadCustomOptionTest extends TestCase
*/
protected $downloadMock;

/**
* @var CollectionFactory|MockObject
*/
protected $itemCollectionFactoryMock;

/**
* @var DownloadCustomOption|MockObject
*/
@@ -104,6 +110,11 @@ protected function setUp(): void
->onlyMethods(['serialize', 'unserialize'])
->getMock();

$this->itemCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$requestMock = $this->getMockBuilder(Http::class)
->disableOriginalConstructor()
->onlyMethods(['getParam'])
@@ -160,7 +171,8 @@ protected function setUp(): void
'resultForwardFactory' => $resultForwardFactoryMock,
'download' => $this->downloadMock,
'unserialize' => $this->createMock(Unserialize::class),
'serializer' => $this->serializerMock
'serializer' => $this->serializerMock,
'itemCollectionFactory' => $this->itemCollectionFactoryMock
]
)
->getMock();
3 changes: 3 additions & 0 deletions app/code/Magento/Sales/etc/db_schema.xml
Original file line number Diff line number Diff line change
@@ -604,6 +604,9 @@
<index referenceId="SALES_ORDER_ITEM_STORE_ID" indexType="btree">
<column name="store_id"/>
</index>
<index referenceId="SALES_ORDER_ITEM_QUOTE_ITEM_ID" indexType="btree">
<column name="quote_item_id"/>
</index>
</table>
<table name="sales_order_payment" resource="sales" engine="innodb" comment="Sales Flat Order Payment">
<column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true"
3 changes: 2 additions & 1 deletion app/code/Magento/Sales/etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
@@ -336,7 +336,8 @@
},
"index": {
"SALES_ORDER_ITEM_ORDER_ID": true,
"SALES_ORDER_ITEM_STORE_ID": true
"SALES_ORDER_ITEM_STORE_ID": true,
"SALES_ORDER_ITEM_QUOTE_ITEM_ID": true
},
"constraint": {
"PRIMARY": true,