-
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
Merged
magento-engcom-team
merged 11 commits into
magento:2.4-develop
from
rogyar:825-adobestock-remove-directory
Mar 19, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed0cdb1
[WIP] Remove media gallery assets metadata when a directory removed
rogyar f6ac9e7
Merge branch '2.4-develop' into 825-adobestock-remove-directory
rogyar da9526f
Refactoring and unit test coverage
rogyar 145cb44
Added an extended class purpose description
rogyar 8679c9a
Changed DocBlock description of the parameter type
rogyar 847e5bc
Errors handling refactoring and additional test coverage
rogyar c24206e
Merge branch '2.4-develop' into 825-adobestock-remove-directory
sivaschenko 57a6a4b
Merge branch '2.4-develop' into 825-adobestock-remove-directory
sivaschenko dce54ce
Removed unnecessary check from the plugin
rogyar 1eabf19
Merge branch '2.4-develop' into 825-adobestock-remove-directory
rogyar 5c94848
Refresh file list after a directory has been removed
rogyar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
app/code/Magento/MediaGallery/Model/Asset/Command/DeleteByDirectoryPath.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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\MediaGalleryApi\Model\Asset\Command\DeleteByDirectoryPathInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Class DeleteByDirectoryPath | ||
* | ||
* Remove asset(s) that correspond the provided directory path | ||
*/ | ||
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(s) by path | ||
* | ||
* @param string $directoryPath | ||
* | ||
* @return void | ||
* | ||
* @throws CouldNotDeleteException | ||
*/ | ||
public function execute(string $directoryPath): void | ||
{ | ||
$this->validateDirectoryPath($directoryPath); | ||
try { | ||
// Make sure that the path has a trailing slash | ||
$directoryPath = rtrim($directoryPath, '/') . '/'; | ||
|
||
/** @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) { | ||
$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 CouldNotDeleteException | ||
*/ | ||
private function validateDirectoryPath(string $directoryPath): void | ||
{ | ||
if (!$directoryPath || trim($directoryPath) === '') { | ||
throw new CouldNotDeleteException(__('Cannot remove assets, the directory path does not exist')); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,15 @@ | |
|
||
namespace Magento\MediaGallery\Plugin\Wysiwyg\Images; | ||
|
||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
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; | ||
use Magento\Framework\Exception\ValidatorException; | ||
|
||
/** | ||
* Ensures that metadata is removed from the database when a file is deleted and it is an image | ||
|
@@ -31,6 +33,11 @@ class Storage | |
*/ | ||
private $deleteMediaAssetByPath; | ||
|
||
/** | ||
* @var DeleteByDirectoryPathInterface | ||
*/ | ||
private $deleteMediaAssetByDirectoryPath; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
|
@@ -46,17 +53,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->deleteMediaAssetByDirectoryPath = $deleteByDirectoryPath; | ||
$this->filesystem = $filesystem; | ||
$this->logger = $logger; | ||
} | ||
|
@@ -69,7 +79,6 @@ public function __construct( | |
* @param string $target | ||
* | ||
* @return StorageSubject | ||
* @throws ValidatorException | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
|
@@ -92,4 +101,32 @@ public function afterDeleteFile(StorageSubject $subject, StorageSubject $result, | |
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Delete media data after the folder delete action from Wysiwyg | ||
* | ||
* @param StorageSubject $subject | ||
* @param mixed $result | ||
* @param string $path | ||
* | ||
* @return null | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterDeleteDirectory(StorageSubject $subject, $result, $path) | ||
{ | ||
if (!is_string($path)) { | ||
return $result; | ||
} | ||
|
||
try { | ||
$mediaDirectoryRead = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); | ||
$relativePath = $mediaDirectoryRead->getRelativePath($path); | ||
$this->deleteMediaAssetByDirectoryPath->execute($relativePath); | ||
} catch (ValidatorException $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; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
app/code/Magento/MediaGallery/Test/Unit/Model/Asset/Command/DeleteByDirectoryPathTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGallery\Test\Unit\Model\Asset\Command; | ||
|
||
use Magento\MediaGallery\Model\Asset\Command\DeleteByDirectoryPath; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Test the DeleteByDirectoryPath command model | ||
*/ | ||
class DeleteByDirectoryPathTest extends TestCase | ||
{ | ||
private const TABLE_NAME = 'media_gallery_asset'; | ||
private const DIRECTORY_PATH = 'test-directory-path/'; | ||
|
||
/** | ||
* @var ResourceConnection|MockObject | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @var DeleteByDirectoryPath | ||
*/ | ||
private $deleteMediaAssetByDirectoryPath; | ||
|
||
/** | ||
* @var AdapterInterface|MockObject | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* @var LoggerInterface|MockObject | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* Initialize basic test class mocks | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
$this->resourceConnection = $this->createMock(ResourceConnection::class); | ||
|
||
$this->deleteMediaAssetByDirectoryPath = (new ObjectManager($this))->getObject( | ||
DeleteByDirectoryPath::class, | ||
[ | ||
'resourceConnection' => $this->resourceConnection, | ||
'logger' => $this->logger, | ||
] | ||
); | ||
|
||
$this->adapter = $this->createMock(AdapterInterface::class); | ||
} | ||
|
||
/** | ||
* Test delete media asset by path command | ||
* | ||
* @param string $directoryPath | ||
* @throws CouldNotDeleteException | ||
* @dataProvider directoryPathDataProvider | ||
*/ | ||
public function testDeleteByDirectoryPath(string $directoryPath): void | ||
{ | ||
if (!empty($directoryPath)) { | ||
$this->resourceConnection->expects($this->once()) | ||
->method('getConnection') | ||
->willReturn($this->adapter); | ||
$this->resourceConnection->expects($this->once()) | ||
->method('getTableName') | ||
->with(self::TABLE_NAME) | ||
->willReturn('prefix_' . self::TABLE_NAME); | ||
$this->adapter->expects($this->once()) | ||
->method('delete') | ||
->with('prefix_' . self::TABLE_NAME, ['path LIKE ?' => self::DIRECTORY_PATH . '%']); | ||
} else { | ||
self::expectException('\Magento\Framework\Exception\CouldNotDeleteException'); | ||
} | ||
|
||
$this->deleteMediaAssetByDirectoryPath->execute($directoryPath); | ||
} | ||
|
||
/** | ||
* Data provider for directory path | ||
* | ||
* @return array | ||
*/ | ||
public function directoryPathDataProvider(): array | ||
{ | ||
return [ | ||
'Existing path' => [self::DIRECTORY_PATH], | ||
'Empty path' => [''] | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Import? :-)