diff --git a/openseadragon.module b/openseadragon.module index 161e42d..772e6bd 100644 --- a/openseadragon.module +++ b/openseadragon.module @@ -89,15 +89,44 @@ function template_preprocess_openseadragon_formatter(&$variables) { $variables['#attached']['library'] = [ 'openseadragon/init', ]; - $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [ - 'basePath' => Url::fromUri($iiif_address), - 'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'], - 'options' => [ - 'id' => $openseadragon_viewer_id, - 'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/', - 'tileSources' => $tile_sources, - ] + $viewer_settings, - ]; + + // For dsu-utsc. + if (!empty(\Drupal::hasService('jwt.authentication.jwt'))) { + $jwtService = \Drupal::service('jwt.authentication.jwt'); + $access_token = $jwtService->generateToken(); + $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [ + 'basePath' => Url::fromUri($iiif_address), + 'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'], + 'options' => [ + 'id' => $openseadragon_viewer_id, + 'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/', + 'tileSources' => $tile_sources, + + // For dsu-utsc. + 'loadTilesWithAjax' => TRUE, + 'ajaxWithCredentials' => TRUE, + 'ajaxHeaders' => [ + "Authorization" => "Bearer " . $access_token, + 'token' => $access_token, + ], + ] + $viewer_settings, + ]; + // Add cache metadata to limit the cache lifetime + $variables['#cache']['max-age'] = 3600; // 1 hour + $variables['#cache']['tags'][] = 'jwt_token'; // Add a cache tag to invalidate the cache when the token changes + } + else { + $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [ + 'basePath' => Url::fromUri($iiif_address), + 'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'], + 'options' => [ + 'id' => $openseadragon_viewer_id, + 'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/', + 'tileSources' => $tile_sources, + ] + $viewer_settings, + ]; + } + $variables['attributes']['class'] = $classes_array; $variables['attributes']['id'] = $openseadragon_viewer_id; @@ -111,7 +140,7 @@ function template_preprocess_openseadragon_formatter(&$variables) { */ function template_preprocess_openseadragon_iiif_manifest_block(&$variables) { $cache_meta = CacheableMetadata::createFromRenderArray($variables); - + // Get the tile sources from the manifest. $parser = \Drupal::service('openseadragon.manifest_parser'); $tile_sources = $parser->getTileSources($variables['iiif_manifest_url']); @@ -141,15 +170,43 @@ function template_preprocess_openseadragon_iiif_manifest_block(&$variables) { $variables['#attached']['library'] = [ 'openseadragon/init', ]; - $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [ - 'basePath' => Url::fromUri($iiif_address), - 'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'], - 'options' => [ - 'id' => $openseadragon_viewer_id, - 'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/', - 'tileSources' => $tile_sources, - ] + $viewer_settings, - ]; + if (!empty(\Drupal::hasService('jwt.authentication.jwt'))) { + $jwtService = \Drupal::service('jwt.authentication.jwt'); + $access_token = $jwtService->generateToken(); + + $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [ + 'basePath' => Url::fromUri($iiif_address), + 'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'], + 'options' => [ + 'id' => $openseadragon_viewer_id, + 'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/', + 'tileSources' => $tile_sources, + + // For dsu-utsc. + 'loadTilesWithAjax' => TRUE, + 'ajaxWithCredentials' => TRUE, + 'ajaxHeaders' => [ + "Authorization" => "Bearer " . $access_token, + 'token' => $access_token, + ], + ] + $viewer_settings, + ]; + // Add cache metadata to limit the cache lifetime + $variables['#cache']['max-age'] = 3600; // 1 hour + $variables['#cache']['tags'][] = 'jwt_token'; // Add a cache tag to invalidate the cache when the token changes + } + else { + $variables['#attached']['drupalSettings']['openseadragon'][$openseadragon_viewer_id] = [ + 'basePath' => Url::fromUri($iiif_address), + 'fitToAspectRatio' => $viewer_settings['fit_to_aspect_ratio'], + 'options' => [ + 'id' => $openseadragon_viewer_id, + 'prefixUrl' => 'https://cdnjs.cloudflare.com/ajax/libs/openseadragon/2.4.2/images/', + 'tileSources' => $tile_sources, + ] + $viewer_settings, + ]; + } + $variables['attributes']['class'] = $classes_array; $variables['attributes']['id'] = $openseadragon_viewer_id; diff --git a/src/IIIFManifestParser.php b/src/IIIFManifestParser.php index fc98f6a..0765bdf 100644 --- a/src/IIIFManifestParser.php +++ b/src/IIIFManifestParser.php @@ -68,7 +68,7 @@ class IIIFManifestParser { * @return array * The URLs of all the tile sources in a manifest. */ - public function getTileSources($manifest_url) { + public function getTileSources($manifest_url, $access_token = NULL) { // Try to construct the URL out of a tokenized string // if the node is available. @@ -85,7 +85,12 @@ class IIIFManifestParser { try { // Request the manifest. - $manifest_response = $this->httpClient->get($manifest_url); + #$manifest_response = $this->httpClient->get($manifest_url); + $manifest_response = $this->httpClient->request('GET', $manifest_url, [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $access_token, + ], + ]); // Decode the manifest json. $manifest_string = (string) $manifest_response->getBody();