-
Notifications
You must be signed in to change notification settings - Fork 2
/
islandora_iiif.module
214 lines (196 loc) · 8.1 KB
/
islandora_iiif.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* @file
* Main module file for islandora iiif.
*/
/**
* Implements hook_init().
*/
function islandora_iiif_init() {
require_once __DIR__ . "/vendor/autoload.php";
}
/**
* Implements hook_menu().
*/
function islandora_iiif_menu() {
return array(
'islandora_iiif/%islandora_object/%/%/%/%' => array(
'page callback' => 'islandora_iiif_proxied_image_response',
'page arguments' => array(1, 2, 3, 4, 5),
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 1),
'load arguments' => array(1),
),
'islandora_iiif/%islandora_object/info.json' => array(
'page callback' => 'islandora_iiif_proxied_info_response',
'page arguments' => array(1),
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 1),
'load arguments' => array(1),
),
'islandora_iiif/%islandora_object/manifest' => array(
'page callback' => 'islandora_iiif_manifest_callback',
'page arguments' => array(1),
'file' => 'includes/manifest.inc',
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 1),
'load arguments' => array(1),
),
'islandora_iiif/%islandora_object/sequence/%' => array(
'page callback' => 'islandora_iiif_sequence_callback',
'page arguments' => array(1),
'file' => 'includes/sequence.inc',
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 1),
'load arguments' => array(1),
),
'islandora_iiif/%islandora_object/canvas/%' => array(
'page callback' => 'islandora_iiif_canvas_callback',
'page arguments' => array(1),
'file' => 'includes/canvas.inc',
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 1),
'load arguments' => array(1),
),
'islandora_iiif/collection/%islandora_object' => array(
'page callback' => 'islandora_iiif_collection_callback',
'file' => 'includes/collection.inc',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2),
'load arguments' => array(2),
),
'islandora_iiif/collection/%islandora_object/%' => array(
'page callback' => 'islandora_iiif_collection_callback',
'file' => 'includes/collection.inc',
'page arguments' => array(2, 3),
'type' => MENU_CALLBACK,
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2),
'load arguments' => array(2),
),
'admin/islandora/tools/internet_iiif_admin' => array(
'title' => 'IIIF Module configuration',
'description' => 'Configure the IIIF Module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_iiif_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
);
}
/**
*
*/
function islandora_iiif_rootcollection_access($op, $object, $user = NULL) {
// Get root as we have it configured globally. We can always get
// islandora:root directly via the PID specific collection manifest.
$root_pid = variable_get('islandora_repository_pid', 'islandora:root');
if ($object = islandora_object_load($root_pid)) {
return islandora_object_access($op, $object, $user);
}
return FALSE;
}
/**
* Our Proxied callback handler()
*/
function islandora_iiif_proxied_image_response(AbstractObject $object, $region, $size, $rotation = 0, $qualityandformat = "default.jp2") {
global $base_root;
module_load_include('inc', 'islandora_iiif', 'includes/utils');
$allowedExtensions = array('png', 'jpg', 'jp2');
$qualityandformatparsed = islandora_iiif_safefile($qualityandformat, $allowedExtensions);
// @TODO implement a hook here to allow per CMODEL backend \
// $hooks = islandora_build_hook_list(ISLANDORA_IMAGESERVER_HOOK, $object->models);
$iiif_request = array(
"pid" => $object->id,
"region" => $region,
"size" => $size,
"rotation" => $rotation,
"quality" => $qualityandformatparsed[0],
"format" => $qualityandformatparsed[1],
);
module_load_include('inc', 'islandora', 'includes/authtokens');
$uri_params = array(
'absolute' => TRUE,
'query' => array(
'token' => islandora_get_object_token($object->id, "OBJ", 2),
),
);
$metadata_response = islandora_iiif_djatoka_metadata_request($object->id);
// dpm($iiif_request);
// dpm($metadata_response);
$theresponsecode = (int) $metadata_response->code;
// dpm($theresponsecode);
if ($theresponsecode >= 200 && $theresponsecode < 400) {
$translated_params = islandora_iiif_djatoka_region(json_decode($metadata_response->data, TRUE), $iiif_request);
drupal_add_http_header('Content-Type', 'image/jp2');
// @TODO hookify this so others can alter the canonical
drupal_add_http_header('Link', '<' . $base_root . 'islandora_iiif/' . urlencode($object->id) . 'full/max/0/default.jp2>;rel="canonical"');
drupal_add_http_header('access-control-allow-origin', '*');
$djatoka_url = islandora_iiif_djatoka_image_request_url($object->id, $translated_params);
// Djatoka is terrible at resizing (quality loss) so allow
// for a setting to override that and resize after via imagemagick.
if ((variable_get('islandora_iiif_resizeondrupal', FALSE)) &&
isset($translated_params['scale']) && ($iiif_request['size'] != "max" && $iiif_request['size'] != "full")) {
// The idea is this:
// If this is enabled, the right Level will be selected
// but the final scale should happen via imagemagick just before returning.
// That should lead to a compromise situation: a bit slower than asking
// Adored directly, but since level should be close to final, imagemagick
// overhead will be not that wrong and mostly and better quality.
$theimage = drupal_http_request($djatoka_url);
$imagick = new Imagick();
$imagick->readImageBlob($theimage->data);
$imagick->setImageFormat('jp2');
$scale = explode(",", $translated_params['scale']);
// Make every empty string a 0.
$scale = array_map(function ($val) {
if (empty(trim($val))) {
$val = 0;
} return $val;
}, $scale);
if (count($scale) == 1) {
$scale[] = 0;
}
$imagick->scaleImage($scale[0], $scale[1]);
print $imagick->getImageBlob();
}
else {
$theimage = drupal_http_request($djatoka_url);
print $theimage->data;
}
}
else {
drupal_add_http_header('Status', '404 Not Found');
}
drupal_page_footer();
}
/**
* Our Proxied callback handler()
*/
function islandora_iiif_proxied_info_response(AbstractObject $object) {
module_load_include('inc', 'islandora_iiif', 'includes/utils');
global $base_root;
// @TODO implement a hook here to allow per CMODEL/Context backend.
// $hooks = islandora_build_hook_list(ISLANDORA_IMAGESERVER_HOOK, $object->models);
$metadata_response = islandora_iiif_djatoka_metadata_request($object->id);
$theresponsecode = (int) $metadata_response->code;
if ($theresponsecode >= 200 && $theresponsecode < 400) {
drupal_add_http_header('Content-Type', 'application/json');
drupal_add_http_header('access-control-allow-origin', '*');
// @TODO hookify this so others can alter the canonical
drupal_add_http_header('Link', '<' . $base_root . 'islandora_iiif/' . urlencode($object->id) . 'full/max/0/default.jp2>;rel="canonical"');
$info = islandora_iiif_adoredjatoka_info_json(json_decode($metadata_response->data, TRUE));
print json_encode($info, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
else {
drupal_add_http_header('Status', '404 Not Found');
}
}