Skip to content

Commit 0a57486

Browse files
committed
Add configuration for MediaGallery/Directory
1 parent 83e7e37 commit 0a57486

File tree

8 files changed

+294
-22
lines changed

8 files changed

+294
-22
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\MediaGallery\Model\Directory;
8+
9+
use Magento\Framework\Config\DataInterface;
10+
11+
/**
12+
* Config of Magento Media Gallery Directory.
13+
*/
14+
class Config
15+
{
16+
/**
17+
* @var DataInterface
18+
*/
19+
private $data;
20+
21+
/**
22+
* @param DataInterface $data
23+
*/
24+
public function __construct(DataInterface $data)
25+
{
26+
$this->data = $data;
27+
}
28+
29+
/**
30+
* Get config value by key.
31+
*
32+
* @param string|null $key
33+
* @param string|null $default
34+
* @return array
35+
*/
36+
public function get($key = null, $default = null)
37+
{
38+
return $this->data->get($key, $default);
39+
}
40+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MediaGallery\Model\Directory\Config;
7+
8+
use Magento\Framework\Config\ConverterInterface;
9+
10+
/**
11+
* Class Converter
12+
*/
13+
class Converter implements ConverterInterface
14+
{
15+
/**
16+
* Blacklist tag name
17+
*/
18+
private CONST BLACKLIST_TAG_NAME = 'blacklist';
19+
20+
/**
21+
* Patterns tag name
22+
*/
23+
private CONST PATTERNS_TAG_NAME = 'patterns';
24+
25+
/**
26+
* Pattern tag name
27+
*/
28+
private CONST PATTERN_TAG_NAME = 'pattern';
29+
30+
/**
31+
* Convert dom node to array
32+
*
33+
* @param \DOMDocument $source
34+
* @return array
35+
*/
36+
public function convert($source) : array
37+
{
38+
$result = [];
39+
40+
if (!$source instanceof \DOMDocument) {
41+
return $result;
42+
}
43+
44+
foreach ($source->getElementsByTagName(self::BLACKLIST_TAG_NAME) as $blacklist) {
45+
$result[self::BLACKLIST_TAG_NAME] = [];
46+
foreach ($blacklist->getElementsByTagName(self::PATTERNS_TAG_NAME) as $patterns) {
47+
$result[self::BLACKLIST_TAG_NAME][self::PATTERNS_TAG_NAME] = [];
48+
foreach ($patterns->getElementsByTagName(self::PATTERN_TAG_NAME) as $pattern) {
49+
$result[self::BLACKLIST_TAG_NAME][self::PATTERNS_TAG_NAME]
50+
[$pattern->attributes->getNamedItem('name')->nodeValue] = $pattern->nodeValue;
51+
}
52+
}
53+
}
54+
55+
return $result;
56+
}
57+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MediaGallery\Model\Directory\Config;
7+
8+
use Magento\Framework\App\Area;
9+
use Magento\Framework\Config\ReaderInterface;
10+
use Magento\Framework\Config\Reader\Filesystem;
11+
use Magento\Framework\Config\FileResolverInterface;
12+
use Magento\Framework\Config\ValidationStateInterface;
13+
use Magento\Framework\Config\Dom;
14+
15+
class Reader extends Filesystem implements ReaderInterface
16+
{
17+
/**
18+
* List of id attributes for merge
19+
*
20+
* @var array
21+
*/
22+
protected $_idAttributes = [
23+
'/config/patterns' => 'patterns',
24+
'/config/patterns/pattern' => 'name',
25+
];
26+
27+
/**
28+
* XML Configuration file name
29+
*/
30+
private const XML_FILE_NAME = 'directory.xml';
31+
32+
/**
33+
* Construct the FileSystem Reader Class
34+
*
35+
* @param \Magento\Framework\Config\FileResolverInterface $fileResolver
36+
* @param Converter $converter
37+
* @param SchemaLocator $schemaLocator
38+
* @param \Magento\Framework\Config\ValidationStateInterface $validationState
39+
* @param string $fileName
40+
* @param array $idAttributes
41+
* @param string $domDocumentClass
42+
* @param string $defaultScope
43+
*/
44+
public function __construct(
45+
FileResolverInterface $fileResolver,
46+
Converter $converter,
47+
SchemaLocator $schemaLocator,
48+
ValidationStateInterface $validationState,
49+
$fileName = self::XML_FILE_NAME,
50+
$idAttributes = [],
51+
$domDocumentClass = Dom::class,
52+
$defaultScope = Area::AREA_GLOBAL
53+
) {
54+
parent::__construct(
55+
$fileResolver,
56+
$converter,
57+
$schemaLocator,
58+
$validationState,
59+
$fileName,
60+
$idAttributes,
61+
$domDocumentClass,
62+
$defaultScope
63+
);
64+
}
65+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MediaGallery\Model\Directory\Config;
7+
8+
use Magento\Framework\Module\Dir;
9+
use Magento\Framework\Module\Dir\Reader;
10+
use Magento\Framework\Config\SchemaLocatorInterface;
11+
12+
class SchemaLocator implements SchemaLocatorInterface
13+
{
14+
/**
15+
* Path to corresponding XSD file with validation rules for both individual and merged configs
16+
*
17+
* @var string
18+
*/
19+
private $schema;
20+
21+
/**
22+
* @param Reader $moduleReader
23+
*/
24+
public function __construct(Reader $moduleReader)
25+
{
26+
$this->schema = $moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_MediaGallery') . '/directory.xsd';
27+
}
28+
29+
/**
30+
* Get path to merged config schema
31+
*
32+
* @return string|null
33+
*/
34+
public function getSchema()
35+
{
36+
return $this->schema;
37+
}
38+
39+
/**
40+
* Get path to per file validation schema
41+
*
42+
* @return string|null
43+
*/
44+
public function getPerFileSchema()
45+
{
46+
return $this->schema;
47+
}
48+
}

app/code/Magento/MediaGallery/Model/Directory/IsBlacklisted.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
class IsBlacklisted implements IsBlacklistedInterface
2020
{
2121
/**
22-
* @var array
22+
* @var Config
2323
*/
24-
private $patterns;
24+
private $config;
2525

2626
/**
27-
* @param array $patterns
27+
* @param Config $config
2828
*/
29-
public function __construct(
30-
array $patterns
31-
) {
32-
$this->patterns = $patterns;
29+
public function __construct(Config $config) {
30+
$this->config = $config;
3331
}
3432

3533
/**
@@ -40,7 +38,7 @@ public function __construct(
4038
*/
4139
public function execute(string $path): bool
4240
{
43-
foreach ($this->patterns as $pattern) {
41+
foreach ($this->config->get('blacklist/patterns') as $pattern) {
4442
if (empty($pattern)) {
4543
continue;
4644
}

app/code/Magento/MediaGallery/etc/di.xml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828

2929
<preference for="Magento\MediaGalleryApi\Model\File\Command\DeleteByAssetIdInterface" type="Magento\MediaGallery\Model\File\Command\DeleteByAssetId"/>
3030

31-
<type name="Magento\MediaGallery\Model\Directory\IsBlacklisted">
32-
<arguments>
33-
<argument name="patterns" xsi:type="array">
34-
<item name="captcha" xsi:type="string">/pub\/media\/captcha/</item>
35-
<item name="catalog" xsi:type="string">/pub\/media\/catalog\/product/</item>
36-
<item name="customer" xsi:type="string">/pub\/media\/customer/</item>
37-
<item name="downloadable" xsi:type="string">/pub\/media\/downloadable/</item>
38-
<item name="import" xsi:type="string">/pub\/media\/import/</item>
39-
<item name="theme" xsi:type="string">/pub\/media\/theme/</item>
40-
<item name="theme_customization" xsi:type="string">/pub\/media\/theme_customization/</item>
41-
<item name="tmp" xsi:type="string">/pub\/media\/tmp/</item>
42-
</argument>
43-
</arguments>
44-
</type>
4531
<type name="Magento\Catalog\Model\Product\Gallery\Processor">
4632
<plugin name="media_gallery_image_remove_metadata" type="Magento\MediaGallery\Plugin\Product\Gallery\Processor"
4733
sortOrder="10" disabled="false"/>
@@ -50,4 +36,22 @@
5036
<plugin name="media_gallery_image_remove_metadata_after_wysiwyg" type="Magento\MediaGallery\Plugin\Wysiwyg\Images\Storage"
5137
sortOrder="10" disabled="false"/>
5238
</type>
39+
<type name="Magento\MediaGallery\Model\Directory\Config\Reader">
40+
<arguments>
41+
<argument name="fileName" xsi:type="string">directory.xml</argument>
42+
<argument name="converter" xsi:type="object">Magento\MediaGallery\Model\Directory\Config\Converter</argument>
43+
<argument name="schemaLocator" xsi:type="object">Magento\MediaGallery\Model\Directory\Config\SchemaLocator</argument>
44+
</arguments>
45+
</type>
46+
<virtualType name="Magento\MediaGallery\Model\Directory\Config\Data" type="Magento\Framework\Config\Data">
47+
<arguments>
48+
<argument name="reader" xsi:type="object">Magento\MediaGallery\Model\Directory\Config\Reader</argument>
49+
<argument name="cacheId" xsi:type="string">Media_Gallery_Patterns_CacheId</argument>
50+
</arguments>
51+
</virtualType>
52+
<type name="Magento\MediaGallery\Model\Directory\Config">
53+
<arguments>
54+
<argument name="data" xsi:type="object">Magento\MediaGallery\Model\Directory\Config\Data</argument>
55+
</arguments>
56+
</type>
5357
</config>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_MediaGallery:etc/directory.xsd">
9+
<blacklist>
10+
<patterns>
11+
<pattern name="captcha">/pub\/media\/captcha/</pattern>
12+
<pattern name="catalog">/pub\/media\/catalog\/product/</pattern>
13+
<pattern name="customer">/pub\/media\/customer/</pattern>
14+
<pattern name="downloadable">/pub\/media\/downloadable/</pattern>
15+
<pattern name="import">/pub\/media\/import/</pattern>
16+
<pattern name="theme">/pub\/media\/theme/</pattern>
17+
<pattern name="theme_customization">/pub\/media\/theme_customization/</pattern>
18+
<pattern name="tmp">/pub\/media\/tmp/</pattern>
19+
<pattern name="directories-with-dots">/^\./</pattern>
20+
</patterns>
21+
</blacklist>
22+
</config>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
9+
<xs:element name="config" type="configType">
10+
</xs:element>
11+
12+
<xs:complexType name="configType">
13+
<xs:sequence>
14+
<xs:element type="blacklistType" name="blacklist" maxOccurs="unbounded" minOccurs="1"/>
15+
</xs:sequence>
16+
</xs:complexType>
17+
18+
<xs:complexType name="blacklistType">
19+
<xs:sequence>
20+
<xs:element type="patternsType" name="patterns" maxOccurs="unbounded" minOccurs="0"/>
21+
</xs:sequence>
22+
</xs:complexType>
23+
24+
<xs:complexType name="patternsType">
25+
<xs:sequence>
26+
<xs:element type="patternType" name="pattern" maxOccurs="unbounded" minOccurs="0"/>
27+
</xs:sequence>
28+
</xs:complexType>
29+
30+
<xs:complexType name="patternType">
31+
<xs:simpleContent>
32+
<xs:extension base="xs:string">
33+
<xs:attribute type="xs:string" name="name" use="required"/>
34+
<xs:attribute type="xs:string" name="example" use="optional"/>
35+
</xs:extension>
36+
</xs:simpleContent>
37+
</xs:complexType>
38+
</xs:schema>

0 commit comments

Comments
 (0)