Skip to content
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

Remove media gallery assets metadata when a directory removed #26015

Merged

Conversation

rogyar
Copy link
Contributor

@rogyar rogyar commented Dec 12, 2019

Description (*)

This PR introduces a logic for removing meta-information from media gallery (database records) once a directory with media assets has been removed.

Fixed Issues (if relevant)

  1. The image state is not updated when the containing folder has been deleted  adobe-stock-integration#825 : The image state is not updated when the containing folder has been deleted

Manual testing scenarios (*)

  • Install Magento with Adobe Stock Integration
  • Configure the integration in Stores -> Configuration -> System -> Adobe Stock Integration fieldset
  1. From Magento Admin go to Content - Pages, click Add New Page
  2. Expand Content Click Show / Hide Editor, select Insert Image...
  3. Select Storage Root and click Create Folder, name the folder and click OK
  4. Select the previously created folder and Click Search Adobe Stock
  5. Select an image from Adobe Stock and Save Preview, Confirm
  6. Select the previously created folder and click Delete Folder
  7. Click Search Adobe Stock again and observe the previously saved image

The "Save Preview" and "License and Save" buttons should be present and work as expected

@m2-assistant
Copy link

m2-assistant bot commented Dec 12, 2019

Hi @rogyar. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

@magento-engcom-team magento-engcom-team added Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner labels Dec 12, 2019
@rogyar
Copy link
Contributor Author

rogyar commented Dec 12, 2019

Hi @sivaschenko.

Could you, please, review the proof of concept? After your green light, I will proceed with the tests.

Thank you!

Comment on lines 63 to 65
if (substr($directoryPath, -1) !== '/') {
$directoryPath .= '/';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't measured that - but isn't the better option just rtrim($directoryPath, '/').'/'

$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? :-)

try {
$this->deleteMediAssetByDirectoryPath->execute($relativePath);
} catch (\Exception $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.

@rogyar
Copy link
Contributor Author

rogyar commented Dec 15, 2019

Hi @lbajsarowicz. Thank you for your review.
This PR is going to be reworked since it's just a proof of concept. I'm waiting for confirmation from @sivaschenko since we have discussed this approach separately and now working together on a final implementation 😉

@lbajsarowicz
Copy link
Contributor

I saw "WIP" notification so I'm aware of the work that is in progress. Just giving the feedback.

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

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

Hi @rogyar thanks for proof of concept, the approach looks right to me. The only proposition is to involve MediaDirectory instance in DeleteByDirectoryPath service (instead of the plugin) to validate that a valid path inside the media directory is provided (getRelativePath, isExists) before further operations based on provided paths

@rogyar
Copy link
Contributor Author

rogyar commented Dec 26, 2019

Hi @sivaschenko. Thank you for your suggestion. Working on the solution

@rogyar rogyar changed the title [WIP] Remove media gallery assets metadata when a directory removed Remove media gallery assets metadata when a directory removed Jan 4, 2020
Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

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

Hi @rogyar thanks for the update. Please take a look at my comments

public function execute(string $directoryPath): void
{
try {
$this->validateDirectoryPath($directoryPath);
Copy link
Member

Choose a reason for hiding this comment

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

validateDirectoryPath throws a LocalizedException. 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 the LocalizedException to more specific CouldNotDeleteException in validateDirectoryPath)

return $result;
}

$relativePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getRelativePath($path);
Copy link
Member

Choose a reason for hiding this comment

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

getRelativePath throws ValidatorException. I think it should be logged without interrupting the code execution.

Also, it looks like getRelativePath will return the original passed path if an incorrect path was provided, so I'd check if the path exists in media directory not to remove assets if the StorageSubject provided an incorrect path for some reason

@ghost ghost assigned sivaschenko Jan 6, 2020
@rogyar
Copy link
Contributor Author

rogyar commented Jan 10, 2020

@magento run tests

try {
$mediaDirectoryRead = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$relativePath = $mediaDirectoryRead->getRelativePath($path);
if ($mediaDirectoryRead->isExist($relativePath) === false) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this check necessary? Considering that it is afterDeleteDirectory method body?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @sivaschenko.

The idea was the following. If the original method's (deleteDirectory) $path for some reason does not exist, we most likely will not have an exception, just successful method execution (see:

).
After that our plugin will execute the DB query which does not make sense.

This is an edge-case. If you believe that we don't need this check, I'm OK as well.

Thank you!

Copy link
Member

Choose a reason for hiding this comment

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

@rogyar I think this check should be removed as the plugin is afterDeleteDirectory so the directory should not exist when it's executed anyway

@ghost ghost removed the Progress: needs update label Feb 17, 2020
@magento-engcom-team
Copy link
Contributor

Hi @sivaschenko, thank you for the review.
ENGCOM-6918 has been created to process this Pull Request
✳️ @sivaschenko, could you please add one of the following labels to the Pull Request?

Label Description
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests

@engcom-Delta
Copy link
Contributor

Hi @rogyar I am still able to reproduce issue #825 on PR branch with adobe-stock-integration (branch 1.1-develop)
#26015PR

Could you take a look?

@rogyar
Copy link
Contributor Author

rogyar commented Feb 24, 2020

Hi @engcom-Delta it was another issue connected rather to the files rendering than to the issue that was supposed to be fixed in the scope of this PR. Anyway, I have tweaked the rendering process as well in my recent commit. Could you check it out once again, please?

Thank you!

@rogyar
Copy link
Contributor Author

rogyar commented Mar 2, 2020

Hi @engcom-Delta. Did you have a chance to review this PR? Thank you

@rogyar rogyar requested a review from engcom-Delta March 2, 2020 11:05
@engcom-Delta
Copy link
Contributor

Hi @rogyar I rechecked PR with latest changes and now issue is fixed
@lbajsarowicz @sivaschenko Could you review last commit?

@rogyar
Copy link
Contributor Author

rogyar commented Mar 11, 2020

HI @sivaschenko. Could you review this one, please, once again?

Thank you!

@sivaschenko
Copy link
Member

Thanks for updates @rogyar !

@magento-engcom-team
Copy link
Contributor

Hi @sivaschenko, thank you for the review.
ENGCOM-6918 has been created to process this Pull Request
✳️ @sivaschenko, could you please add one of the following labels to the Pull Request?

Label Description
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests

@sivaschenko sivaschenko added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Mar 18, 2020
@sivaschenko
Copy link
Member

Failed B2B MFTF test doesn't seem to be related to the changes

@magento-engcom-team magento-engcom-team merged commit 8a78108 into magento:2.4-develop Mar 19, 2020
@m2-assistant
Copy link

m2-assistant bot commented Mar 19, 2020

Hi @rogyar, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants