Skip to content

Commit 20db8f6

Browse files
authored
Merge pull request #4672 from magento-tango/PR-2019-08-22
[tango] Bugfixes
2 parents 099c639 + ebbbad4 commit 20db8f6

File tree

11 files changed

+373
-255
lines changed

11 files changed

+373
-255
lines changed

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ public function beforeSave($object)
121121

122122
if ($this->fileResidesOutsideCategoryDir($value)) {
123123
// use relative path for image attribute so we know it's outside of category dir when we fetch it
124+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
125+
$value[0]['url'] = parse_url($value[0]['url'], PHP_URL_PATH);
124126
$value[0]['name'] = $value[0]['url'];
125127
}
126128

127129
if ($imageName = $this->getUploadedImageName($value)) {
128-
$imageName = $this->checkUniqueImageName($imageName);
130+
if (!$this->fileResidesOutsideCategoryDir($value)) {
131+
$imageName = $this->checkUniqueImageName($imageName);
132+
}
129133
$object->setData($this->additionalData . $attributeName, $value);
130134
$object->setData($attributeName, $imageName);
131135
} elseif (!is_string($value)) {
@@ -182,7 +186,7 @@ private function fileResidesOutsideCategoryDir($value)
182186
return false;
183187
}
184188

185-
return strpos($fileUrl, $baseMediaDir) === 0;
189+
return strpos($fileUrl, $baseMediaDir) !== false;
186190
}
187191

188192
/**

app/code/Magento/Catalog/Model/Category/FileInfo.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Framework\Filesystem;
1111
use Magento\Framework\Filesystem\Directory\WriteInterface;
1212
use Magento\Framework\Filesystem\Directory\ReadInterface;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Store\Model\StoreManagerInterface;
1315

1416
/**
1517
* Class FileInfo
@@ -48,16 +50,26 @@ class FileInfo
4850
*/
4951
private $pubDirectory;
5052

53+
/**
54+
* Store manager
55+
*
56+
* @var \Magento\Store\Model\StoreManagerInterface
57+
*/
58+
private $storeManager;
59+
5160
/**
5261
* @param Filesystem $filesystem
5362
* @param Mime $mime
63+
* @param StoreManagerInterface $storeManager
5464
*/
5565
public function __construct(
5666
Filesystem $filesystem,
57-
Mime $mime
67+
Mime $mime,
68+
StoreManagerInterface $storeManager
5869
) {
5970
$this->filesystem = $filesystem;
6071
$this->mime = $mime;
72+
$this->storeManager = $storeManager;
6173
}
6274

6375
/**
@@ -152,7 +164,8 @@ public function isExist($fileName)
152164
*/
153165
private function getFilePath($fileName)
154166
{
155-
$filePath = ltrim($fileName, '/');
167+
$filePath = $this->removeStorePath($fileName);
168+
$filePath = ltrim($filePath, '/');
156169

157170
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
158171
$isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);
@@ -177,14 +190,39 @@ private function getFilePath($fileName)
177190
*/
178191
public function isBeginsWithMediaDirectoryPath($fileName)
179192
{
180-
$filePath = ltrim($fileName, '/');
193+
$filePath = $this->removeStorePath($fileName);
194+
$filePath = ltrim($filePath, '/');
181195

182196
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
183197
$isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, (string) $mediaDirectoryRelativeSubpath) === 0;
184198

185199
return $isFileNameBeginsWithMediaDirectoryPath;
186200
}
187201

202+
/**
203+
* Clean store path in case if it's exists
204+
*
205+
* @param string $path
206+
* @return string
207+
*/
208+
private function removeStorePath(string $path): string
209+
{
210+
$result = $path;
211+
try {
212+
$storeUrl = $this->storeManager->getStore()->getBaseUrl();
213+
} catch (NoSuchEntityException $e) {
214+
return $result;
215+
}
216+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
217+
$path = parse_url($path, PHP_URL_PATH);
218+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
219+
$storePath = parse_url($storeUrl, PHP_URL_PATH);
220+
$storePath = rtrim($storePath, '/');
221+
222+
$result = preg_replace('/^' . preg_quote($storePath, '/') . '/', '', $path);
223+
return $result;
224+
}
225+
188226
/**
189227
* Get media directory subpath relative to base directory path
190228
*

app/code/Magento/Catalog/Model/Indexer/Product/Price.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,56 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product;
77

8+
use Magento\Catalog\Model\Category as CategoryModel;
9+
use Magento\Catalog\Model\Indexer\Product\Price\Action\Full as FullAction;
10+
use Magento\Catalog\Model\Indexer\Product\Price\Action\Row as RowAction;
11+
use Magento\Catalog\Model\Indexer\Product\Price\Action\Rows as RowsAction;
12+
use Magento\Catalog\Model\Product as ProductModel;
13+
use Magento\Framework\Indexer\ActionInterface as IndexerActionInterface;
814
use Magento\Framework\Indexer\CacheContext;
15+
use Magento\Framework\Mview\ActionInterface as MviewActionInterface;
916

10-
class Price implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
17+
/**
18+
* Price indexer
19+
*/
20+
class Price implements IndexerActionInterface, MviewActionInterface
1121
{
1222
/**
13-
* @var \Magento\Catalog\Model\Indexer\Product\Price\Action\Row
23+
* @var RowAction
1424
*/
1525
protected $_productPriceIndexerRow;
1626

1727
/**
18-
* @var \Magento\Catalog\Model\Indexer\Product\Price\Action\Rows
28+
* @var RowsAction
1929
*/
2030
protected $_productPriceIndexerRows;
2131

2232
/**
23-
* @var \Magento\Catalog\Model\Indexer\Product\Price\Action\Full
33+
* @var FullAction
2434
*/
2535
protected $_productPriceIndexerFull;
2636

2737
/**
28-
* @var \Magento\Framework\Indexer\CacheContext
38+
* @var CacheContext
2939
*/
3040
private $cacheContext;
3141

3242
/**
33-
* @param Price\Action\Row $productPriceIndexerRow
34-
* @param Price\Action\Rows $productPriceIndexerRows
35-
* @param Price\Action\Full $productPriceIndexerFull
43+
* @param RowAction $productPriceIndexerRow
44+
* @param RowsAction $productPriceIndexerRows
45+
* @param FullAction $productPriceIndexerFull
46+
* @param CacheContext $cacheContext
3647
*/
3748
public function __construct(
38-
\Magento\Catalog\Model\Indexer\Product\Price\Action\Row $productPriceIndexerRow,
39-
\Magento\Catalog\Model\Indexer\Product\Price\Action\Rows $productPriceIndexerRows,
40-
\Magento\Catalog\Model\Indexer\Product\Price\Action\Full $productPriceIndexerFull
49+
RowAction $productPriceIndexerRow,
50+
RowsAction $productPriceIndexerRows,
51+
FullAction $productPriceIndexerFull,
52+
CacheContext $cacheContext
4153
) {
4254
$this->_productPriceIndexerRow = $productPriceIndexerRow;
4355
$this->_productPriceIndexerRows = $productPriceIndexerRows;
4456
$this->_productPriceIndexerFull = $productPriceIndexerFull;
57+
$this->cacheContext = $cacheContext;
4558
}
4659

4760
/**
@@ -53,7 +66,7 @@ public function __construct(
5366
public function execute($ids)
5467
{
5568
$this->_productPriceIndexerRows->execute($ids);
56-
$this->getCacheContext()->registerEntities(\Magento\Catalog\Model\Product::CACHE_TAG, $ids);
69+
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, $ids);
5770
}
5871

5972
/**
@@ -64,10 +77,10 @@ public function execute($ids)
6477
public function executeFull()
6578
{
6679
$this->_productPriceIndexerFull->execute();
67-
$this->getCacheContext()->registerTags(
80+
$this->cacheContext->registerTags(
6881
[
69-
\Magento\Catalog\Model\Category::CACHE_TAG,
70-
\Magento\Catalog\Model\Product::CACHE_TAG
82+
CategoryModel::CACHE_TAG,
83+
ProductModel::CACHE_TAG
7184
]
7285
);
7386
}
@@ -81,6 +94,7 @@ public function executeFull()
8194
public function executeList(array $ids)
8295
{
8396
$this->_productPriceIndexerRows->execute($ids);
97+
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, $ids);
8498
}
8599

86100
/**
@@ -92,20 +106,6 @@ public function executeList(array $ids)
92106
public function executeRow($id)
93107
{
94108
$this->_productPriceIndexerRow->execute($id);
95-
}
96-
97-
/**
98-
* Get cache context
99-
*
100-
* @return \Magento\Framework\Indexer\CacheContext
101-
* @deprecated 100.0.11
102-
*/
103-
protected function getCacheContext()
104-
{
105-
if (!($this->cacheContext instanceof CacheContext)) {
106-
return \Magento\Framework\App\ObjectManager::getInstance()->get(CacheContext::class);
107-
} else {
108-
return $this->cacheContext;
109-
}
109+
$this->cacheContext->registerEntities(ProductModel::CACHE_TAG, [$id]);
110110
}
111111
}

0 commit comments

Comments
 (0)