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

Fix/file access #42

Merged
merged 5 commits into from
Aug 11, 2022
Merged
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
50 changes: 37 additions & 13 deletions openseadragon.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\file\Entity\File;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
Expand Down Expand Up @@ -34,19 +35,28 @@ function openseadragon_theme() {
* Implements template_preprocess_HOOK().
*/
function template_preprocess_openseadragon_formatter(&$variables) {
$item = $variables['item'];
$entity = $variables['entity'];
// Load the global settings.
$config = \Drupal::service('openseadragon.config');
$fileinfo_service = \Drupal::service('openseadragon.fileinfo');

// Initialize our attributes.
$variables['attributes'] = new Attribute();

$cache_meta = CacheableMetadata::createFromRenderArray($variables)
->addCacheableDependency($config);

$classes_array = ['openseadragon-viewer'];
$viewer_settings = $config->getSettings(TRUE);
$iiif_address = $config->getIiifAddress();
if (is_null($iiif_address) || empty($iiif_address)) {
$cache_meta->applyTo($variables);
return;
}

$item = $variables['item'];
$entity = $variables['entity'];
$cache_meta->addCacheableDependency($entity);

// Build the gallery id.
$id = $entity->id();
$openseadragon_viewer_id = 'openseadragon-viewer-' . $id;
Expand All @@ -59,6 +69,14 @@ function template_preprocess_openseadragon_formatter(&$variables) {
if (isset($value['target_id'])) {
$fid = $value['target_id'];
$file = File::load($fid);
$access_result = $file->access('view', NULL, TRUE);
$cache_meta->addCacheableDependency($file)
->addCacheableDependency($access_result);

if (!$access_result->isAllowed()) {
continue;
}

$resource = $fileinfo_service->getFileData($file);
if (isset($resource['full_path'])) {
$tile_sources[] = rtrim($iiif_address, '/') . '/' . urlencode($resource['full_path']);
Expand All @@ -67,7 +85,6 @@ function template_preprocess_openseadragon_formatter(&$variables) {
}

if (!empty($tile_sources)) {

$viewer_settings['sequenceMode'] = count($tile_sources) > 1 && !$viewer_settings['collectionMode'];
$variables['#attached']['library'] = [
'openseadragon/init',
Expand All @@ -82,18 +99,31 @@ function template_preprocess_openseadragon_formatter(&$variables) {
] + $viewer_settings,
];

$variables['attributes'] = new Attribute();
$variables['attributes']['class'] = $classes_array;
$variables['attributes']['id'] = $openseadragon_viewer_id;
}

$cache_meta->applyTo($variables);
}

/**
* Implements template_preprocess_HOOK().
*/
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']);

if (empty($tile_sources)) {
$cache_meta->applyTo($variables);
return;
}

// Load the global settings.
$config = \Drupal::service('openseadragon.config');
$cache_meta->addCacheableDependency($config);

// Build the gallery id.
$openseadragon_viewer_id = Html::getUniqueId('openseadragon-viewer-iiif-manifest-block');
Expand All @@ -102,16 +132,10 @@ function template_preprocess_openseadragon_iiif_manifest_block(&$variables) {
$viewer_settings = $config->getSettings(TRUE);
$iiif_address = $config->getIiifAddress();

// Get the tile sources from the manifest.
$parser = \Drupal::service('openseadragon.manifest_parser');
$tile_sources = $parser->getTileSources($variables['iiif_manifest_url']);

if (empty($tile_sources)) {
return;
}

$viewer_settings['sequenceMode'] = count($tile_sources) > 1 && !$viewer_settings['collectionMode'];

$variables['attributes'] = new Attribute();

// Attach the viewer, using the image urls obtained from the manifest.
if (!is_null($iiif_address) && !empty($iiif_address) && !empty($tile_sources)) {
$variables['#attached']['library'] = [
Expand All @@ -127,11 +151,11 @@ function template_preprocess_openseadragon_iiif_manifest_block(&$variables) {
] + $viewer_settings,
];

$variables['attributes'] = new Attribute();
$variables['attributes']['class'] = $classes_array;
$variables['attributes']['id'] = $openseadragon_viewer_id;
}

$cache_meta->applyTo($variables);
}

/**
Expand Down