-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Remove media gallery assets metadata when a directory removed #26015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ed0cdb1
f6ac9e7
da9526f
145cb44
8679c9a
847e5bc
c24206e
57a6a4b
dce54ce
1eabf19
5c94848
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGallery\Model\Asset\Command; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Class DeleteByDirectoryPath | ||
*/ | ||
class DeleteByDirectoryPath implements DeleteByDirectoryPathInterface | ||
{ | ||
private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset'; | ||
|
||
private const MEDIA_GALLERY_ASSET_PATH = 'path'; | ||
|
||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* DeleteById constructor. | ||
* | ||
* @param ResourceConnection $resourceConnection | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection, | ||
LoggerInterface $logger | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Delete media asset by path | ||
* | ||
* @param string $directoryPath | ||
* | ||
* @return void | ||
* @throws CouldNotDeleteException | ||
*/ | ||
public function execute(string $directoryPath): void | ||
{ | ||
try { | ||
$this->validateDirectoryPath($directoryPath); | ||
|
||
if (substr($directoryPath, -1) !== '/') { | ||
$directoryPath .= '/'; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't measured that - but isn't the better option just |
||
|
||
/** @var AdapterInterface $connection */ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $this->resourceConnection->getTableName(self::TABLE_MEDIA_GALLERY_ASSET); | ||
$connection->delete($tableName, [self::MEDIA_GALLERY_ASSET_PATH . ' LIKE ?' => $directoryPath . '%']); | ||
} catch (\Exception $exception) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import? :-) |
||
$this->logger->critical($exception); | ||
$message = __( | ||
'Could not delete media assets by path %path: %error', | ||
['path' => $directoryPath, 'error' => $exception->getMessage()] | ||
); | ||
throw new CouldNotDeleteException($message, $exception); | ||
} | ||
} | ||
|
||
/** | ||
* Validate the directory path | ||
* | ||
* @param string $directoryPath | ||
* @throws LocalizedException | ||
*/ | ||
private function validateDirectoryPath(string $directoryPath): void | ||
{ | ||
if (trim($directoryPath) === '') { | ||
throw new LocalizedException(__('The directory path cannot be empty')); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,12 @@ | |
|
||
namespace Magento\MediaGallery\Plugin\Wysiwyg\Images; | ||
|
||
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface; | ||
use Magento\MediaGalleryApi\Model\Asset\Command\GetByPathInterface; | ||
use Magento\MediaGalleryApi\Model\Asset\Command\DeleteByPathInterface; | ||
use Magento\Cms\Model\Wysiwyg\Images\Storage as StorageSubject; | ||
use Magento\Framework\App\Filesystem\DirectoryList; | ||
use Magento\Framework\Filesystem; | ||
use Magento\Framework\Exception\ValidatorException; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
|
@@ -31,6 +31,11 @@ class Storage | |
*/ | ||
private $deleteMediaAssetByPath; | ||
|
||
/** | ||
* @var DeleteByDirectoryPathInterface | ||
*/ | ||
private $deleteMediAssetByDirectoryPath; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
|
@@ -46,17 +51,20 @@ class Storage | |
* | ||
* @param GetByPathInterface $getMediaAssetByPath | ||
* @param DeleteByPathInterface $deleteMediaAssetByPath | ||
* @param DeleteByDirectoryPathInterface $deleteByDirectoryPath | ||
* @param Filesystem $filesystem | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
GetByPathInterface $getMediaAssetByPath, | ||
DeleteByPathInterface $deleteMediaAssetByPath, | ||
DeleteByDirectoryPathInterface $deleteByDirectoryPath, | ||
Filesystem $filesystem, | ||
LoggerInterface $logger | ||
) { | ||
$this->getMediaAssetByPath = $getMediaAssetByPath; | ||
$this->deleteMediaAssetByPath = $deleteMediaAssetByPath; | ||
$this->deleteMediAssetByDirectoryPath = $deleteByDirectoryPath; | ||
$this->filesystem = $filesystem; | ||
$this->logger = $logger; | ||
} | ||
|
@@ -69,7 +77,6 @@ public function __construct( | |
* @param string $target | ||
* | ||
* @return StorageSubject | ||
* @throws ValidatorException | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
|
@@ -92,4 +99,36 @@ public function afterDeleteFile(StorageSubject $subject, StorageSubject $result, | |
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Delete media data after the folder delete action from Wysiwyg | ||
* | ||
* @param StorageSubject $subject | ||
* @param null $result | ||
* @param string $path | ||
* | ||
* @return null | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterDeleteDirectory(StorageSubject $subject, $result, $path) | ||
{ | ||
if (!is_string($path)) { | ||
return $result; | ||
} | ||
|
||
$relativePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, it looks like |
||
|
||
if (!$relativePath) { | ||
return $result; | ||
} | ||
|
||
try { | ||
$this->deleteMediAssetByDirectoryPath->execute($relativePath); | ||
} catch (\Exception $exception) { | ||
$this->logger->critical($exception); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are logging the same exception twice? |
||
} | ||
|
||
return $result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryApi\Model\Asset\Command; | ||
|
||
/** | ||
* A command represents the media gallery assets delete action. A media gallery asset is filtered by directory | ||
* path value. | ||
*/ | ||
interface DeleteByDirectoryPathInterface | ||
{ | ||
/** | ||
* Delete media assets by directory path | ||
* | ||
* @param string $directoryPath | ||
* | ||
* @return void | ||
*/ | ||
public function execute(string $directoryPath): void; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validateDirectoryPath
throws aLocalizedException
. I don't think it's necessary to recapture that exception, I propose to move the call out of try-catch block (also it may be good to change theLocalizedException
to more specificCouldNotDeleteException
invalidateDirectoryPath
)