From 13b74009dec5b5f9a84c5a75e21c9457a375b71e Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Tue, 17 Oct 2023 00:02:56 -0300 Subject: [PATCH] Islandora IIIF: Add action to retrieve image attributes from IIIF server. --- ...tion.media_attributes_from_iiif_action.yml | 10 ++ modules/islandora_iiif/islandora_iiif.install | 16 ++ .../Plugin/Action/MediaAttributesFromIiif.php | 169 ++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml create mode 100644 modules/islandora_iiif/islandora_iiif.install create mode 100644 modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php diff --git a/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml new file mode 100644 index 00000000..dc0b18b9 --- /dev/null +++ b/modules/islandora_iiif/config/optional/system.action.media_attributes_from_iiif_action.yml @@ -0,0 +1,10 @@ +langcode: en +status: true +dependencies: + module: + - islandora_iiif +id: media_attributes_from_iiif_action +label: 'Media attributes from IIIF' +type: media +plugin: islandora_iiif:media_attributes_from_iiif_action:media +configuration: { } diff --git a/modules/islandora_iiif/islandora_iiif.install b/modules/islandora_iiif/islandora_iiif.install new file mode 100644 index 00000000..9b040735 --- /dev/null +++ b/modules/islandora_iiif/islandora_iiif.install @@ -0,0 +1,16 @@ +getPath('islandora_iiif') . '/config/optional/' . $config_id .'.yml'; + $data = \Symfony\Component\Yaml\Yaml::parseFile($config_path); + \Drupal::configFactory()->getEditable($config_id)->setData($data)->save(TRUE); +} diff --git a/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php new file mode 100644 index 00000000..9bb951e2 --- /dev/null +++ b/modules/islandora_iiif/src/Plugin/Action/MediaAttributesFromIiif.php @@ -0,0 +1,169 @@ +httpClient = $http_client; +$this->iiifInfo = $iiif_info; +$this->utils = $islandora_utils; +$this->mediaSource = $media_source; +$this->logger = $channel; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('datetime.time'), + $container->get('http_client'), + $container->get('islandora_iiif'), + $container->get('islandora.utils'), + $container->get('islandora.media_source_service'), + $container->get('logger.channel.islandora') + ); + } + + /** + * {@inheritdoc} + */ + public function execute($entity = NULL ) { + $width = $height = FALSE; + + // Get the original File media use term. + $original_file_term = $this->utils->getTermForUri('http://pcdm.org/use#OriginalFile'); + + /** + * @var \Drupal\media\MediaInterface $original_file_media + */ + $original_file_mids = $this->utils->getMediaReferencingNodeAndTerm($entity, $original_file_term); + if (!empty($original_file_mids)) { + + // Ordinarily there shouldn't be more than one Original File media but it's not guaranteed. + foreach($original_file_mids as $original_file_mid) { + + /* + * @var \Drupal\Media\MediaInterface $original_file_media + */ + $original_file_media = $this->entityTypeManager->getStorage('media')->load($original_file_mid); + + // Get the media MIME Type + $original_file = $this->mediaSource->getSourceFile($original_file_media); + $mime_type = $original_file->getMimeType(); + + if (in_array($mime_type, ['image/tiff', 'image/jp2'])) { + [$width, $height] = $this->iiifInfo->getImageDimensions($original_file); + } + + + // @todo Make field configurable. Low priority since this whole thing is a workaround for an Islandora limitation. + if ($original_file_media->hasField('field_width') && $original_file_media->hasField('field_height')) { + $original_file_media->set('field_height', $height); + $original_file_media->set('field_width', $width); + $original_file_media->save(); + } + } + } + } + + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + + /** @var \Drupal\Core\Entity\EntityInterface $object */ + return $object->access('update', $account, $return_as_object); + } + + }