forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
959 configurable width height caching (#1029)
* Refactor IIIF, create IIIF Info Service. * Islandora IIIF: Add action to retrieve image attributes from IIIF server. * Islandora IIIF: Add auth headers to IIIF Info request. * Islandora IIIF: Change media action to node. * Islandora IIIF: Update README. * Finish rebase. * Islandora IIIF: Address PHPCS errors. * Islandora IIIF: Address PHPCS errors. * islandora_iiif: Fix Authorization header syntax. * 959-iiif-width-height-caching Islandora IIIF: Add search endpoint config to manifest. * 959-iiif-width-height-caching Add handler for ServerException in Islandora IIIF manifest generator. * 959-iiif-width-height-caching Add option to skip retrieveing TIFF and JP2 dimensions from IIIF server. * Checking for width/height on media first when generating IIIF manifest * 959-iiif-width-height-caching Add IIIF service function to get downlaod link with given dimensions. * Fix redundant function from rebase. * Rebase resolve conflict. * WIP: Make Get dimensions from File image action target fields configurable. * WIP * WIP use configured height and width fields. * Extract TIFF and JP2 dimensions action: Make width and height field configurable. * Islandora IIIF: Make media height and width fields configurable. * Islandora IIIF: Make media width and height fields configurable in manifest plugin. * Islandora IIIF: Add config schema update hook. * Islandora IIIF: Fix warning on Views display plugin page. * Remove outdated action. * Islandora IIIF: Update the README. * Islandora IIIF: Update PHPDoc description of IIIF Dimensions complex action. * Islandora IIIF: Add instructions for setting up a context reaction for the IIIF Dimensions actions. * Update modules/islandora_iiif/config/schema/islandora_iiif.schema.yml Co-authored-by: Adam <[email protected]> * Islandora IIIF: Address PHPCS errors. * Islandora IIIF: Remove unused schema entry. * Islandora IIIF: Address PHPCS errors. * Islandora IIIF: Address PHPCS errors. * Islandora IIIF: Fix PHPCS errors. * Islandora IIIF: Fix PHPCS warnings. * Islandora IIIF: Address code review comment. * Update modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php Co-authored-by: Adam <[email protected]> * Update modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php Co-authored-by: Adam <[email protected]> * Islandora IIIF: Address PHPCS errors. --------- Co-authored-by: dannylamb <[email protected]> Co-authored-by: Adam <[email protected]>
- Loading branch information
1 parent
60f4925
commit 7bae08f
Showing
6 changed files
with
691 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Install/update hook implementations. | ||
*/ | ||
|
||
/** | ||
* Update config schema. | ||
*/ | ||
function islandora_iiif_update_92002(&$sandbox) { | ||
|
||
/** | ||
* @var \Drupal\Core\Config\ConfigFactoryInterface | ||
*/ | ||
$config_factory = \Drupal::service('config.factory'); | ||
$all_configs = $config_factory->listAll(); | ||
$views_configs = array_values(array_filter($all_configs, function ($config) { | ||
return str_starts_with($config, 'views.view.'); | ||
})); | ||
|
||
foreach ($views_configs as $views_config_name) { | ||
$needs_save = FALSE; | ||
$view_config = $config_factory->getEditable($views_config_name); | ||
$displays = $view_config->get('display'); | ||
foreach ($displays as $display_name => $display) { | ||
if ($display['display_plugin'] == 'rest_export' | ||
&& $display['display_options']['style']['type'] == 'iiif_manifest' | ||
&&!empty($display['display_options']['style']['options']['iiif_ocr_file_field'])) { | ||
|
||
$display['display_options']['style']['options']['advanced']['iiif_ocr_file_field'] = $display['display_options']['style']['options']['iiif_ocr_file_field']; | ||
unset($display['display_options']['style']['options']['iiif_ocr_file_field']); | ||
$view_config->set('display.' . $display_name . '.display_options.style.options', $display['display_options']['style']['options']); | ||
$needs_save = TRUE; | ||
} | ||
} | ||
if ($needs_save) { | ||
$view_config->save(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
islandora_iiif: | ||
class: Drupal\islandora_iiif\IiifInfo | ||
arguments: ['@config.factory', '@http_client', '@logger.channel.islandora', '@jwt.authentication.jwt'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora_iiif; | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Logger\LoggerChannelInterface; | ||
use Drupal\file\FileInterface; | ||
use Drupal\jwt\Authentication\Provider\JwtAuth; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Exception\ServerException; | ||
|
||
/** | ||
* Get IIIF related info for a given File or Image entity. | ||
*/ | ||
class IiifInfo { | ||
|
||
/** | ||
* The config factory. | ||
* | ||
* @var \Drupal\Core\Config\ConfigFactoryInterface | ||
*/ | ||
protected $configFactory; | ||
|
||
|
||
/** | ||
* The HTTP client. | ||
* | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
protected $httpClient; | ||
|
||
/** | ||
* This module's config. | ||
* | ||
* @var \Drupal\Core\Config\ImmutableConfig | ||
*/ | ||
protected $iiifConfig; | ||
|
||
/** | ||
* JWT Auth provider service. | ||
* | ||
* @var \Drupal\jwt\Authentication\Provider\JwtAuth | ||
*/ | ||
protected $jwtAuth; | ||
|
||
/** | ||
* The logger. | ||
* | ||
* @var \Drupal\Core\Logger\LoggerChannelInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* Constructs an IiifInfo object. | ||
* | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | ||
* The config factory. | ||
* @param \Guzzle\Http\Client $http_client | ||
* The HTTP Client. | ||
* @param \Drupal\Core\Logger\LoggerChannelInterface $channel | ||
* Logger channel. | ||
* @param \Drupal\jwt\Authentication\Provider\JwtAuth $jwt_auth | ||
* The JWT auth provider. | ||
*/ | ||
public function __construct(ConfigFactoryInterface $config_factory, Client $http_client, LoggerChannelInterface $channel, JwtAuth $jwt_auth) { | ||
$this->configFactory = $config_factory; | ||
|
||
$this->iiifConfig = $this->configFactory->get('islandora_iiif.settings'); | ||
$this->httpClient = $http_client; | ||
$this->logger = $channel; | ||
$this->jwtAuth = $jwt_auth; | ||
} | ||
|
||
/** | ||
* The IIIF base URL for an image. | ||
* | ||
* Visiting this URL will resolve to the info.json for the image. | ||
* | ||
* @return string | ||
* The absolute URL on the IIIF server. | ||
*/ | ||
public function baseUrl($image) { | ||
|
||
if ($this->iiifConfig->get('use_relative_paths')) { | ||
$file_url = ltrim($image->createFileUrl(TRUE), '/'); | ||
} | ||
else { | ||
$file_url = $image->createFileUrl(FALSE); | ||
} | ||
|
||
$iiif_address = $this->iiifConfig->get('iiif_server'); | ||
$iiif_url = rtrim($iiif_address, '/') . '/' . urlencode($file_url); | ||
|
||
return $iiif_url; | ||
} | ||
|
||
/** | ||
* Retrieve an image's original dimensions via the IIIF server. | ||
* | ||
* @param \Drupal\File\FileInterface $file | ||
* The image file. | ||
* | ||
* @return array|false | ||
* The image dimensions in an array as [$width, $height] | ||
*/ | ||
public function getImageDimensions(FileInterface $file) { | ||
$iiif_url = $this->baseUrl($file); | ||
try { | ||
$info_json = $this->httpClient->request('get', $iiif_url, [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer ' . $this->jwtAuth->generateToken(), | ||
], | ||
])->getBody(); | ||
$resource = json_decode($info_json, TRUE); | ||
$width = $resource['width']; | ||
$height = $resource['height']; | ||
if (is_numeric($width) && is_numeric($height)) { | ||
return [intval($width), intval($height)]; | ||
} | ||
} | ||
catch (ClientException | ConnectException | RequestException | ServerException $e) { | ||
$this->logger->info("Error getting image file dimensions from IIIF server: " . $e->getMessage()); | ||
} | ||
return FALSE; | ||
} | ||
|
||
/** | ||
* The IIIF base URL for an image. | ||
* | ||
* Visiting this URL resolves to the image resized to the maximum dimensions. | ||
* | ||
* @param \Drupal\file\FileInterface $image | ||
* The image entity. | ||
* @param int $width | ||
* The maximum width of the image to be returned. 0 for no constraint. | ||
* @param int $height | ||
* The maxim um height of the image to be returned. 0 for no contraint. | ||
* | ||
* @return string | ||
* The IIIF URl to retrieve the full image with the given max dimensions. | ||
*/ | ||
public function getImageWithMaxDimensions(FileInterface $image, $width = 0, $height = 0) { | ||
$base_url = $this->baseUrl($image); | ||
return $base_url . "/full/!$width,$height/0/default.jpg"; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.