Skip to content

Commit ed0cdb1

Browse files
committed
[WIP] Remove media gallery assets metadata when a directory removed
1 parent f540160 commit ed0cdb1

File tree

4 files changed

+160
-2
lines changed

4 files changed

+160
-2
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGallery\Model\Asset\Command;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\Exception\CouldNotDeleteException;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Class DeleteByDirectoryPath
19+
*/
20+
class DeleteByDirectoryPath implements DeleteByDirectoryPathInterface
21+
{
22+
private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset';
23+
24+
private const MEDIA_GALLERY_ASSET_PATH = 'path';
25+
26+
/**
27+
* @var ResourceConnection
28+
*/
29+
private $resourceConnection;
30+
31+
/**
32+
* @var LoggerInterface
33+
*/
34+
private $logger;
35+
36+
/**
37+
* DeleteById constructor.
38+
*
39+
* @param ResourceConnection $resourceConnection
40+
* @param LoggerInterface $logger
41+
*/
42+
public function __construct(
43+
ResourceConnection $resourceConnection,
44+
LoggerInterface $logger
45+
) {
46+
$this->resourceConnection = $resourceConnection;
47+
$this->logger = $logger;
48+
}
49+
50+
/**
51+
* Delete media asset by path
52+
*
53+
* @param string $directoryPath
54+
*
55+
* @return void
56+
* @throws CouldNotDeleteException
57+
*/
58+
public function execute(string $directoryPath): void
59+
{
60+
try {
61+
$this->validateDirectoryPath($directoryPath);
62+
63+
if (substr($directoryPath, -1) !== '/') {
64+
$directoryPath .= '/';
65+
}
66+
67+
/** @var AdapterInterface $connection */
68+
$connection = $this->resourceConnection->getConnection();
69+
$tableName = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET);
70+
$connection->delete($tableName, [self::MEDIA_GALLERY_ASSET_PATH . ' LIKE ?' => $directoryPath . '%']);
71+
} catch (\Exception $exception) {
72+
$this->logger->critical($exception);
73+
$message = __(
74+
'Could not delete media assets by path %path: %error',
75+
['path' => $directoryPath, 'error' => $exception->getMessage()]
76+
);
77+
throw new CouldNotDeleteException($message, $exception);
78+
}
79+
}
80+
81+
/**
82+
* Validate the directory path
83+
*
84+
* @param string $directoryPath
85+
* @throws LocalizedException
86+
*/
87+
private function validateDirectoryPath(string $directoryPath): void
88+
{
89+
if (trim($directoryPath) === '') {
90+
throw new LocalizedException(__('The directory path cannot be empty'));
91+
}
92+
}
93+
}

app/code/Magento/MediaGallery/Plugin/Wysiwyg/Images/Storage.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
namespace Magento\MediaGallery\Plugin\Wysiwyg\Images;
1010

11+
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface;
1112
use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface;
1213
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface;
1314
use Magento\Cms\Model\Wysiwyg\Images\Storage as StorageSubject;
1415
use Magento\Framework\App\Filesystem\DirectoryList;
1516
use Magento\Framework\Filesystem;
16-
use Magento\Framework\Exception\ValidatorException;
1717
use Psr\Log\LoggerInterface;
1818

1919
/**
@@ -31,6 +31,11 @@ class Storage
3131
*/
3232
private $deleteMediaAssetByPath;
3333

34+
/**
35+
* @var DeleteByDirectoryPathInterface
36+
*/
37+
private $deleteMediAssetByDirectoryPath;
38+
3439
/**
3540
* @var Filesystem
3641
*/
@@ -46,17 +51,20 @@ class Storage
4651
*
4752
* @param GetByPathInterface $getMediaAssetByPath
4853
* @param DeleteByPathInterface $deleteMediaAssetByPath
54+
* @param DeleteByDirectoryPathInterface $deleteByDirectoryPath
4955
* @param Filesystem $filesystem
5056
* @param LoggerInterface $logger
5157
*/
5258
public function __construct(
5359
GetByPathInterface $getMediaAssetByPath,
5460
DeleteByPathInterface $deleteMediaAssetByPath,
61+
DeleteByDirectoryPathInterface $deleteByDirectoryPath,
5562
Filesystem $filesystem,
5663
LoggerInterface $logger
5764
) {
5865
$this->getMediaAssetByPath = $getMediaAssetByPath;
5966
$this->deleteMediaAssetByPath = $deleteMediaAssetByPath;
67+
$this->deleteMediAssetByDirectoryPath = $deleteByDirectoryPath;
6068
$this->filesystem = $filesystem;
6169
$this->logger = $logger;
6270
}
@@ -69,7 +77,6 @@ public function __construct(
6977
* @param string $target
7078
*
7179
* @return StorageSubject
72-
* @throws ValidatorException
7380
*
7481
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7582
*/
@@ -92,4 +99,36 @@ public function afterDeleteFile(StorageSubject $subject, StorageSubject $result,
9299

93100
return $result;
94101
}
102+
103+
/**
104+
* Delete media data after the folder delete action from Wysiwyg
105+
*
106+
* @param StorageSubject $subject
107+
* @param null $result
108+
* @param string $path
109+
*
110+
* @return null
111+
*
112+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
113+
*/
114+
public function afterDeleteDirectory(StorageSubject $subject, $result, $path)
115+
{
116+
if (!is_string($path)) {
117+
return $result;
118+
}
119+
120+
$relativePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path);
121+
122+
if (!$relativePath) {
123+
return $result;
124+
}
125+
126+
try {
127+
$this->deleteMediAssetByDirectoryPath->execute($relativePath);
128+
} catch (\Exception $exception) {
129+
$this->logger->critical($exception);
130+
}
131+
132+
return $result;
133+
}
95134
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<preference for="Magento\MediaGalleryApi\Model\Asset\Command\SaveInterface" type="Magento\MediaGallery\Model\Asset\Command\Save"/>
1414
<preference for="Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface" type="Magento\MediaGallery\Model\Asset\Command\GetByPath"/>
1515
<preference for="Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface" type="Magento\MediaGallery\Model\Asset\Command\DeleteByPath"/>
16+
<preference for="Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface" type="Magento\MediaGallery\Model\Asset\Command\DeleteByDirectoryPath"/>
1617

1718
<preference for="Magento\MediaGalleryApi\Model\Keyword\Command\GetAssetKeywordsInterface" type="Magento\MediaGallery\Model\Keyword\Command\GetAssetKeywords"/>
1819
<preference for="Magento\MediaGalleryApi\Model\Keyword\Command\SaveAssetKeywordsInterface" type="Magento\MediaGallery\Model\Keyword\Command\SaveAssetKeywords"/>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\MediaGalleryApi\Model\Asset\Command;
10+
11+
/**
12+
* A command represents the media gallery assets delete action. A media gallery asset is filtered by directory
13+
* path value.
14+
*/
15+
interface DeleteByDirectoryPathInterface
16+
{
17+
/**
18+
* Delete media assets by directory path
19+
*
20+
* @param string $directoryPath
21+
*
22+
* @return void
23+
*/
24+
public function execute(string $directoryPath): void;
25+
}

0 commit comments

Comments
 (0)