Skip to content

Commit 460a7e3

Browse files
committed
ACP2E-1627: [Cloud] Magento Product Recommendation/Data Services Causing File Download Warning
1 parent 859268a commit 460a7e3

File tree

4 files changed

+363
-134
lines changed

4 files changed

+363
-134
lines changed

lib/internal/Magento/Framework/App/Response/File.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\App\Http\Context;
1313
use Magento\Framework\App\PageCache\NotCacheableInterface;
1414
use Magento\Framework\App\Request\Http as HttpRequest;
15-
use Magento\Framework\Exception\FileSystemException;
1615
use Magento\Framework\Filesystem;
1716
use Magento\Framework\Filesystem\Driver\File\Mime;
1817
use Magento\Framework\Session\Config\ConfigInterface;
@@ -43,74 +42,72 @@ class File extends Http implements NotCacheableInterface
4342
/**
4443
* @var array
4544
*/
46-
private array $fileOptions = [
45+
private array $options = [
4746
'directoryCode' => DirectoryList::ROOT,
4847
'filePath' => null,
4948
// File name to send to the client
5049
'fileName' => null,
5150
'contentType' => null,
5251
'contentLength' => null,
53-
// Whether to remove after file is sent to the client
52+
// Whether to remove the file after it is sent to the client
5453
'remove' => false,
5554
];
5655

5756
/**
5857
* @param HttpRequest $request
59-
* @param Http $response
6058
* @param CookieManagerInterface $cookieManager
6159
* @param CookieMetadataFactory $cookieMetadataFactory
6260
* @param Context $context
6361
* @param DateTime $dateTime
6462
* @param ConfigInterface $sessionConfig
63+
* @param Http $response
6564
* @param Filesystem $filesystem
6665
* @param Mime $mime
67-
* @param array $fileOptions
66+
* @param array $options
6867
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
69-
* @throws FileSystemException
7068
*/
7169
public function __construct(
7270
HttpRequest $request,
73-
Http $response,
7471
CookieManagerInterface $cookieManager,
7572
CookieMetadataFactory $cookieMetadataFactory,
7673
Context $context,
7774
DateTime $dateTime,
7875
ConfigInterface $sessionConfig,
76+
Http $response,
7977
Filesystem $filesystem,
8078
Mime $mime,
81-
array $fileOptions = []
79+
array $options = []
8280
) {
8381
parent::__construct($request, $cookieManager, $cookieMetadataFactory, $context, $dateTime, $sessionConfig);
84-
$this->filesystem = $filesystem;
8582
$this->response = $response;
83+
$this->filesystem = $filesystem;
8684
$this->mime = $mime;
87-
$this->fileOptions = array_merge($this->fileOptions, $fileOptions);
88-
if (!isset($this->fileOptions['filePath'])) {
89-
throw new InvalidArgumentException("File path is required");
90-
}
91-
$dir = $this->filesystem->getDirectoryRead($this->fileOptions['directoryCode']);
92-
if (!$dir->isExist($this->fileOptions['filePath'])) {
93-
throw new InvalidArgumentException("File '{$this->fileOptions['filePath']}' does not exists.");
94-
}
85+
$this->options = array_merge($this->options, $options);
9586
}
9687

9788
/**
9889
* @inheritDoc
9990
*/
10091
public function sendResponse()
10192
{
102-
$dir = $this->filesystem->getDirectoryWrite($this->fileOptions['directoryCode']);
103-
$filePath = $this->fileOptions['filePath'];
104-
$contentType = $this->fileOptions['contentType']
93+
$dir = $this->filesystem->getDirectoryWrite($this->options['directoryCode']);
94+
if (!isset($this->options['filePath'])) {
95+
throw new InvalidArgumentException("File path is required.");
96+
}
97+
if (!$dir->isExist($this->options['filePath'])) {
98+
throw new InvalidArgumentException("File '{$this->options['filePath']}' does not exists.");
99+
}
100+
$filePath = $this->options['filePath'];
101+
$contentType = $this->options['contentType']
105102
?? $dir->stat($filePath)['mimeType']
106103
?? $this->mime->getMimeType($dir->getAbsolutePath($filePath));
107-
$contentLength = $this->fileOptions['contentLength']
104+
$contentLength = $this->options['contentLength']
108105
?? $dir->stat($filePath)['size'];
109-
$fileName = $this->fileOptions['fileName']
106+
$fileName = $this->options['fileName']
110107
?? basename($filePath);
111108
$this->response->setHttpResponseCode(200);
112109
$this->response->setHeader('Content-type', $contentType, true)
113-
->setHeader('Content-Length', $contentLength)
110+
->setHeader('Content-Length', $contentLength, true)
114111
->setHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"', true)
115112
->setHeader('Pragma', 'public', true)
116113
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
@@ -125,7 +122,7 @@ public function sendResponse()
125122
echo $stream->read(1024);
126123
}
127124
$stream->close();
128-
if ($this->fileOptions['remove']) {
125+
if ($this->options['remove']) {
129126
$dir->delete($filePath);
130127
}
131128
$this->response->clearBody();

lib/internal/Magento/Framework/App/Response/Http/FileFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FileFactory
3939
/**
4040
* @param ResponseInterface $response
4141
* @param Filesystem $filesystem
42-
* @param \Magento\Framework\App\Response\FileFactory $fileResponseFactory
42+
* @param \Magento\Framework\App\Response\FileFactory|null $fileResponseFactory
4343
*/
4444
public function __construct(
4545
\Magento\Framework\App\ResponseInterface $response,
@@ -101,7 +101,7 @@ public function create(
101101
}
102102
}
103103
return $this->fileResponseFactory->create([
104-
'fileOptions' => [
104+
'options' => [
105105
'filePath' => $file,
106106
'fileName' => $fileName,
107107
'contentType' => $contentType,

0 commit comments

Comments
 (0)