Skip to content

Commit

Permalink
Merge pull request #9 from digitalutsc/issue-8
Browse files Browse the repository at this point in the history
Fixes #8 - Thumbnail not loading when media is private
  • Loading branch information
kylehuynh205 authored Jul 10, 2023
2 parents 34010b5 + f33b8d9 commit 9416496
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
6 changes: 6 additions & 0 deletions islandora_mirador.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ islandora_mirador.miradorconfig:
_title: 'Mirador Settings'
requirements:
_permission: 'administer site configuration'
islandora_mirador.service_worker:
path: '/islandora_mirador_service_worker'
defaults:
_controller: '\Drupal\islandora_mirador\Controller\ServiceWorkerController::serve'
requirements:
_permission: 'access content'
15 changes: 15 additions & 0 deletions js/mirador_viewer.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@
],
},
};

if ('serviceWorker' in navigator) {
// The Mirador viewer uses img tags for thumbnails so thumbnail image requests
// do not have authorization or token headers. Attach them using a service worker.
window.addEventListener('load', () => {
navigator.serviceWorker
.register('/islandora_mirador_service_worker?token=' + settings.token, { scope: '/' })
.then(registration => {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
})
.catch(err => {
console.log('ServiceWorker registration failed: ', err);
});
});
}
}


Expand Down
21 changes: 21 additions & 0 deletions js/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
self.addEventListener('activate', function (event) {
console.log('Service Worker: claiming control...');
return self.clients.claim();
});

self.addEventListener('fetch', function (event) {
if (event.request.destination === "image" && new URL(event.request.url).pathname.startsWith('/cantaloupe/iiif/') && new URL(location).searchParams.has('token')) {
console.log('Service Worker: fetching...');
var token = new URL(location).searchParams.get('token');
event.respondWith(
fetch(event.request, {
headers: {
'Authorization': 'Bearer ' + token,
'token': token
},
mode: "cors",
credentials: "include"
})
);
}
});
58 changes: 58 additions & 0 deletions src/Controller/ServiceWorkerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Drupal\islandora_mirador\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ExtensionPathResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* A controller for the Service Worker.
*/
class ServiceWorkerController extends ControllerBase {

/**
* The extension path resolver service.
*
* @var \Drupal\Core\Extension\ExtensionPathResolver
*/
protected $extensionPathResolver;

/**
* Constructs a ServiceWorkerController object.
*
* @param \Drupal\Core\Extension\ExtensionPathResolver $extension_path_resolver
* The extension path resolver service.
*/
public function __construct(ExtensionPathResolver $extension_path_resolver) {
$this->extensionPathResolver = $extension_path_resolver;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('extension.path.resolver')
);
}

/**
* Adds headers to the HTTP response.
*/
public function serve(Request $request) {
$file_str = $this->extensionPathResolver->getPath('module', 'islandora_mirador') . '/js/service_worker.js';
if (file_exists($file_str)) {
$response = new BinaryFileResponse($file_str, 200);
$response->headers->set('Content-Type', 'application/javascript');
// Allow same origin service worker.
$response->headers->set('Service-Worker-Allowed', '/');
return $response;
}
throw new NotFoundHttpException();
}

}

0 comments on commit 9416496

Please sign in to comment.