Skip to content

Commit 140595d

Browse files
committed
Merge pull request #384 from magento-ogre/PR_Branch
[Ogres] Bug-fixes
2 parents 434fbd0 + c753cd7 commit 140595d

File tree

39 files changed

+458
-590
lines changed

39 files changed

+458
-590
lines changed

app/code/Magento/Catalog/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<system>
5252
<media_storage_configuration>
5353
<allowed_resources>
54+
<tmp_images_folder>tmp</tmp_images_folder>
5455
<catalog_images_folder>catalog</catalog_images_folder>
5556
<product_custom_options_fodler>custom_options</product_custom_options_fodler>
5657
</allowed_resources>

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ define([
88
'ko',
99
'Magento_Customer/js/section-config',
1010
'jquery/jquery-storageapi',
11-
'jquery/jquery-cookie'
11+
'jquery/jquery.cookie'
12+
1213
], function ($, _, ko, sectionConfig) {
1314
'use strict';
1415

app/code/Magento/MediaStorage/App/Media.php

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,158 +7,144 @@
77
*/
88
namespace Magento\MediaStorage\App;
99

10-
use Magento\MediaStorage\Model\File\Storage\Request;
10+
use Magento\Framework\Filesystem;
11+
use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
1112
use Magento\MediaStorage\Model\File\Storage\Response;
1213
use Magento\Framework\App;
1314
use Magento\Framework\App\Filesystem\DirectoryList;
1415
use Magento\Framework\AppInterface;
15-
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
1617

18+
/**
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20+
*/
1721
class Media implements AppInterface
1822
{
19-
/**
20-
* @var \Magento\Framework\ObjectManagerInterface
21-
*/
22-
protected $_objectManager;
23-
24-
/**
25-
* @var \Magento\MediaStorage\Model\File\Storage\Request
26-
*/
27-
protected $_request;
28-
2923
/**
3024
* Authorization function
3125
*
3226
* @var \Closure
3327
*/
34-
protected $_isAllowed;
28+
private $isAllowed;
3529

3630
/**
3731
* Media directory path
3832
*
3933
* @var string
4034
*/
41-
protected $_mediaDirectory;
35+
private $mediaDirectoryPath;
4236

4337
/**
4438
* Configuration cache file path
4539
*
4640
* @var string
4741
*/
48-
protected $_configCacheFile;
42+
private $configCacheFile;
4943

5044
/**
5145
* Requested file name relative to working directory
5246
*
5347
* @var string
5448
*/
55-
protected $_relativeFileName;
49+
private $relativeFileName;
5650

5751
/**
58-
* Working directory
59-
*
60-
* @var string
52+
* @var Response
6153
*/
62-
protected $_workingDirectory;
54+
private $response;
6355

6456
/**
65-
* @var \Magento\MediaStorage\Model\File\Storage\Response
57+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
6658
*/
67-
protected $_response;
59+
private $directory;
6860

6961
/**
70-
* @var \Magento\Framework\Filesystem $filesystem
62+
* @var ConfigFactory
7163
*/
72-
protected $filesystem;
64+
private $configFactory;
7365

7466
/**
75-
* @var \Magento\Framework\Filesystem\Directory\Read $directory
67+
* @var SynchronizationFactory
7668
*/
77-
protected $directory;
69+
private $syncFactory;
7870

7971
/**
80-
* @param ObjectManagerInterface $objectManager
81-
* @param Request $request
72+
* @param ConfigFactory $configFactory
73+
* @param SynchronizationFactory $syncFactory
8274
* @param Response $response
8375
* @param \Closure $isAllowed
84-
* @param string $workingDirectory
8576
* @param string $mediaDirectory
8677
* @param string $configCacheFile
8778
* @param string $relativeFileName
88-
* @param \Magento\Framework\Filesystem $filesystem
79+
* @param Filesystem $filesystem
8980
*/
9081
public function __construct(
91-
ObjectManagerInterface $objectManager,
92-
Request $request,
82+
ConfigFactory $configFactory,
83+
SynchronizationFactory $syncFactory,
9384
Response $response,
9485
\Closure $isAllowed,
95-
$workingDirectory,
9686
$mediaDirectory,
9787
$configCacheFile,
9888
$relativeFileName,
99-
\Magento\Framework\Filesystem $filesystem
89+
Filesystem $filesystem
10090
) {
101-
$this->_objectManager = $objectManager;
102-
$this->_request = $request;
103-
$this->_response = $response;
104-
$this->_isAllowed = $isAllowed;
105-
$this->_workingDirectory = $workingDirectory;
106-
$this->_mediaDirectory = $mediaDirectory;
107-
$this->_configCacheFile = $configCacheFile;
108-
$this->_relativeFileName = $relativeFileName;
109-
$this->filesystem = $filesystem;
110-
$this->directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
91+
$this->response = $response;
92+
$this->isAllowed = $isAllowed;
93+
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
94+
$mediaDirectory = trim($mediaDirectory);
95+
if (!empty($mediaDirectory)) {
96+
$this->mediaDirectoryPath = str_replace('\\', '/', realpath($mediaDirectory));
97+
}
98+
$this->configCacheFile = $configCacheFile;
99+
$this->relativeFileName = $relativeFileName;
100+
$this->configFactory = $configFactory;
101+
$this->syncFactory = $syncFactory;
111102
}
112103

113104
/**
114105
* Run application
115106
*
116-
* @return \Magento\Framework\App\ResponseInterface
107+
* @return Response
117108
* @throws \LogicException
118109
*/
119110
public function launch()
120111
{
121-
if (!$this->_mediaDirectory) {
122-
$config = $this->_objectManager->create(
123-
'Magento\MediaStorage\Model\File\Storage\Config',
124-
['cacheFile' => $this->_configCacheFile]
125-
);
112+
if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) {
113+
// Path to media directory changed or absent - update the config
114+
/** @var \Magento\MediaStorage\Model\File\Storage\Config $config */
115+
$config = $this->configFactory->create(['cacheFile' => $this->configCacheFile]);
126116
$config->save();
127-
$this->_mediaDirectory = str_replace($this->_workingDirectory, '', $config->getMediaDirectory());
117+
$this->mediaDirectoryPath = $config->getMediaDirectory();
128118
$allowedResources = $config->getAllowedResources();
129-
$this->_relativeFileName = str_replace(
130-
$this->_mediaDirectory . '/',
131-
'',
132-
$this->_request->getPathInfo()
133-
);
134-
$isAllowed = $this->_isAllowed;
135-
if (!$isAllowed($this->_relativeFileName, $allowedResources)) {
119+
$isAllowed = $this->isAllowed;
120+
if (!$isAllowed($this->relativeFileName, $allowedResources)) {
136121
throw new \LogicException('The specified path is not allowed.');
137122
}
138123
}
139124

140-
if (0 !== stripos($this->_request->getPathInfo(), $this->_mediaDirectory . '/')) {
141-
throw new \LogicException('The specified path is not within media directory.');
142-
}
143-
144-
$sync = $this->_objectManager->get('Magento\MediaStorage\Model\File\Storage\Synchronization');
145-
$sync->synchronize($this->_relativeFileName, $this->_request->getFilePath());
125+
/** @var \Magento\MediaStorage\Model\File\Storage\Synchronization $sync */
126+
$sync = $this->syncFactory->create(['directory' => $this->directory]);
127+
$sync->synchronize($this->relativeFileName);
146128

147-
if ($this->directory->isReadable($this->directory->getRelativePath($this->_request->getFilePath()))) {
148-
$this->_response->setFilePath($this->_request->getFilePath());
129+
if ($this->directory->isReadable($this->relativeFileName)) {
130+
$this->response->setFilePath($this->directory->getAbsolutePath($this->relativeFileName));
149131
} else {
150-
$this->_response->setHttpResponseCode(404);
132+
$this->response->setHttpResponseCode(404);
151133
}
152-
return $this->_response;
134+
return $this->response;
153135
}
154136

155137
/**
156138
* {@inheritdoc}
157139
*/
158140
public function catchException(App\Bootstrap $bootstrap, \Exception $exception)
159141
{
160-
$this->_response->setHttpResponseCode(404);
161-
$this->_response->sendHeaders();
142+
$this->response->setHttpResponseCode(404);
143+
if ($bootstrap->isDeveloperMode()) {
144+
$this->response->setHeader('Content-Type', 'text/plain');
145+
$this->response->setBody($exception->getMessage() . "\n" . $exception->getTraceAsString());
146+
}
147+
$this->response->sendResponse();
162148
return true;
163149
}
164150
}

app/code/Magento/MediaStorage/Model/File/Storage/Config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Config
3131
*
3232
* @var DirectoryWrite
3333
*/
34-
protected $pubDirectory;
34+
protected $rootDirectory;
3535

3636
/**
3737
* @param \Magento\MediaStorage\Model\File\Storage $storage
@@ -44,7 +44,7 @@ public function __construct(
4444
$cacheFile
4545
) {
4646
$this->config = $storage->getScriptConfig();
47-
$this->pubDirectory = $filesystem->getDirectoryWrite(DirectoryList::PUB);
47+
$this->rootDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
4848
$this->cacheFilePath = $cacheFile;
4949
}
5050

@@ -76,7 +76,7 @@ public function getAllowedResources()
7676
public function save()
7777
{
7878
/** @var Write $file */
79-
$file = $this->pubDirectory->openFile($this->pubDirectory->getRelativePath($this->cacheFilePath), 'w');
79+
$file = $this->rootDirectory->openFile($this->rootDirectory->getRelativePath($this->cacheFilePath), 'w');
8080
try {
8181
$file->lock();
8282
$file->write(json_encode($this->config));

app/code/Magento/MediaStorage/Model/File/Storage/Database/AbstractDatabase.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
abstract class AbstractDatabase extends \Magento\Framework\Model\AbstractModel
1212
{
1313
/**
14-
* Store media base directory path
15-
*
16-
* @var string
14+
* Default connection
1715
*/
18-
protected $_mediaBaseDirectory = null;
16+
const CONNECTION_DEFAULT = 'default_setup';
1917

2018
/**
2119
* Core file storage database
@@ -36,6 +34,13 @@ abstract class AbstractDatabase extends \Magento\Framework\Model\AbstractModel
3634
*/
3735
protected $_configuration;
3836

37+
/**
38+
* Connection name
39+
*
40+
* @var string
41+
*/
42+
private $connectionName = self::CONNECTION_DEFAULT;
43+
3944
/**
4045
* @param \Magento\Framework\Model\Context $context
4146
* @param \Magento\Framework\Registry $registry
@@ -81,7 +86,7 @@ public function getConfigConnectionName()
8186
'default'
8287
);
8388
if (empty($connectionName)) {
84-
$connectionName = 'default_setup';
89+
$connectionName = self::CONNECTION_DEFAULT;
8590
}
8691
return $connectionName;
8792
}
@@ -120,10 +125,20 @@ public function prepareStorage()
120125
public function setConnectionName($connectionName)
121126
{
122127
if (!empty($connectionName)) {
123-
$this->setData('connection_name', $connectionName);
124-
$this->_getResource()->setConnectionName($connectionName);
128+
$this->connectionName = $connectionName;
129+
$this->_getResource()->setConnectionName($this->connectionName);
125130
}
126131

127132
return $this;
128133
}
134+
135+
/**
136+
* Get connection name
137+
*
138+
* @return null|string
139+
*/
140+
public function getConnectionName()
141+
{
142+
return $this->connectionName;
143+
}
129144
}

app/code/Magento/MediaStorage/Model/File/Storage/Request.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,14 @@ class Request
1414
*
1515
* @var string
1616
*/
17-
protected $_pathInfo;
17+
private $pathInfo;
1818

1919
/**
20-
* Requested file path
21-
*
22-
* @var string
23-
*/
24-
protected $_filePath;
25-
26-
/**
27-
* @param string $workingDir
2820
* @param HttpRequest $request
2921
*/
30-
public function __construct($workingDir, HttpRequest $request = null)
22+
public function __construct(HttpRequest $request)
3123
{
32-
$request = $request ?: new HttpRequest(
33-
new \Magento\Framework\Stdlib\Cookie\PhpCookieReader()
34-
);
35-
$this->_pathInfo = str_replace('..', '', ltrim($request->getPathInfo(), '/'));
36-
$this->_filePath = $workingDir . '/' . $this->_pathInfo;
24+
$this->pathInfo = str_replace('..', '', ltrim($request->getPathInfo(), '/'));
3725
}
3826

3927
/**
@@ -43,16 +31,6 @@ public function __construct($workingDir, HttpRequest $request = null)
4331
*/
4432
public function getPathInfo()
4533
{
46-
return $this->_pathInfo;
47-
}
48-
49-
/**
50-
* Retrieve file path
51-
*
52-
* @return string
53-
*/
54-
public function getFilePath()
55-
{
56-
return $this->_filePath;
34+
return $this->pathInfo;
5735
}
5836
}

0 commit comments

Comments
 (0)