|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Copyright © Magento, Inc. All rights reserved. |
4 | | - * See COPYING.txt for license details. |
| 3 | + * Copyright 2019 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */ |
6 | 6 | declare(strict_types=1); |
7 | 7 |
|
|
10 | 10 | use Magento\Framework\Api\Filter; |
11 | 11 | use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; |
12 | 12 | use Magento\Framework\Data\Collection\AbstractDb; |
| 13 | +use Psr\Log\LoggerInterface; |
13 | 14 |
|
14 | 15 | class Directory implements CustomFilterInterface |
15 | 16 | { |
| 17 | + /** |
| 18 | + * @var LoggerInterface |
| 19 | + */ |
| 20 | + private $logger; |
| 21 | + |
| 22 | + public function __construct(LoggerInterface $logger) |
| 23 | + { |
| 24 | + $this->logger = $logger; |
| 25 | + } |
| 26 | + |
16 | 27 | /** |
17 | 28 | * @inheritDoc |
18 | 29 | */ |
19 | 30 | public function apply(Filter $filter, AbstractDb $collection): bool |
20 | 31 | { |
21 | 32 | $value = $filter->getValue() !== null ? str_replace('%', '', $filter->getValue()) : ''; |
22 | 33 |
|
23 | | - /** |
24 | | - * Use BINARY comparison for case-sensitive path filtering. |
25 | | - * Without BINARY, MySQL's default case-insensitive comparison would match |
26 | | - * directories like "Testing" and "testing" as the same, leading to incorrect |
27 | | - * file visibility across directories with different case variations. |
28 | | - * The regex '^{path}/[^\/]*$' ensures we only match files directly in the |
29 | | - * specified directory, not in subdirectories. |
30 | | - */ |
31 | | - $collection->getSelect()->where('BINARY path REGEXP ? ', '^' . $value . '/[^\/]*$'); |
| 34 | + try { |
| 35 | + /** |
| 36 | + * Use BINARY comparison for case-sensitive path filtering. |
| 37 | + * Without BINARY, MySQL's default case-insensitive comparison would match |
| 38 | + * directories like "Testing" and "testing" as the same, leading to incorrect |
| 39 | + * file visibility across directories with different case variations. |
| 40 | + * The regex '^{path}/[^\/]*$' ensures we only match files directly in the |
| 41 | + * specified directory, not in subdirectories. |
| 42 | + */ |
| 43 | + $collection->getSelect()->where('BINARY path REGEXP ? ', '^' . $value . '/[^\/]*$'); |
| 44 | + } catch (\Exception $e) { |
| 45 | + // Log the error for debugging but continue with case-insensitive fallback |
| 46 | + // Note: This fallback means directory filtering will not be case-sensitive |
| 47 | + $this->logger->error('MediaGallery Directory Filter: BINARY REGEXP not supported, using case-insensitive fallback: ' . $e->getMessage()); |
| 48 | + $collection->getSelect()->where('path REGEXP ? ', '^' . $value . '/[^\/]*$'); |
| 49 | + } |
32 | 50 |
|
33 | 51 | return true; |
34 | 52 | } |
|
0 commit comments