Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 CouldNotDeleteException
*/
private function validateDirectoryPath(string $directoryPath): void
{
if (!$directoryPath || trim($directoryPath) === '') {
throw new CouldNotDeleteException(__('Cannot remove assets, the directory path does not exist'));
}
}
}
41 changes: 39 additions & 2 deletions app/code/Magento/MediaGallery/Plugin/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +33,11 @@ class Storage
*/
private $deleteMediaAssetByPath;

/**
* @var DeleteByDirectoryPathInterface
*/
private $deleteMediaAssetByDirectoryPath;

/**
* @var Filesystem
*/
Expand All @@ -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;
}
Expand All @@ -69,7 +79,6 @@ public function __construct(
* @param string $target
*
* @return StorageSubject
* @throws ValidatorException
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return $result;
}
}
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' => ['']
];
}
}
Loading