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

Avoid Infinite Derivatives. #920

Merged
merged 3 commits into from
Dec 14, 2022
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
5 changes: 5 additions & 0 deletions src/EventGenerator/EmitEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\islandora\Event\StompHeaderEvent;
use Drupal\islandora\Event\StompHeaderEventException;
use Drupal\islandora\Exception\IslandoraDerivativeException;
use Stomp\Exception\StompException;
use Stomp\StatefulStomp;
use Stomp\Transport\Message;
Expand Down Expand Up @@ -168,6 +169,10 @@ public function execute($entity = NULL) {
$event->getHeaders()->all()
);
}
catch (IslandoraDerivativeException $e) {
$this->logger->info($e->getMessage());
return;
}
catch (StompHeaderEventException $e) {
$this->logger->error($e->getMessage());
$this->messenger->addError($e->getMessage());
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/IslandoraDerivativeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Drupal\islandora\Exception;

/**
* Islandora exceptions.
*
* @package islandora
*/
class IslandoraDerivativeException extends \RuntimeException {
}
8 changes: 8 additions & 0 deletions src/Plugin/Action/AbstractGenerateDerivative.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\islandora\Exception\IslandoraDerivativeException;

/**
* Emits a Node event.
Expand Down Expand Up @@ -60,6 +61,13 @@ protected function generateData(EntityInterface $entity) {
throw new \RuntimeException("Could not locate taxonomy term with uri: " . $this->configuration['derivative_term_uri'], 500);
}

// See if there is a destination media already set, and abort if it's the
// same as the source media. Dont cause an error, just don't continue.
$derivative_media = $this->utils->getMediaWithTerm($entity, $derivative_term);
if (!is_null($derivative_media) && $derivative_media->id() == $source_media->id()) {
throw new IslandoraDerivativeException("Halting derivative, as source and target media are the same. Derivative term: [" . $this->configuration['derivative_term_uri'] . "] Source term: [" . $this->configuration['source_term_uri'] . "] Node id: [" . $entity->id() . "].", 500);
}

$route_params = [
'node' => $entity->id(),
'media_type' => $this->configuration['destination_media_type'],
Expand Down