Skip to content

Instantly share code, notes, and snippets.

@bess
Created March 14, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bess/431774642b87767db55a1334f3549184 to your computer and use it in GitHub Desktop.
Save bess/431774642b87767db55a1334f3549184 to your computer and use it in GitHub Desktop.
cantaloupe delegate script for hyrax
require 'net/http'
require 'uri'
require 'json'
require 'java'
##
# Delegate script to connect Cantaloupe to Fedora. It slices a piece of
# Cantaloupe for Samvera to consume.
#
# This is a first pass and doesn't have a lot of error checking built in yet.
# It also assumes a very basic use case of a single image on an item.
#
class CustomDelegate
##
# Attribute for the request context, which is a hash containing information
# about the current request.
#
# This attribute will be set by the server before any other methods are
# called. Methods can access its keys like:
#
# ```
# identifier = context['identifier']
# ```
#
# The hash will contain the following keys in response to all requests:
#
# * `client_ip` [String] Client IP address.
# * `cookies` [Hash<String,String>] Hash of cookie name-value pairs.
# * `identifier` [String] Image identifier.
# * `request_headers` [Hash<String,String>] Hash of header name-value pairs.
# * `request_uri` [String] Public request URI.
#
# It will contain the following additional string keys in response to image
# requests:
#
# * `full_size` [Hash<String,Integer>] Hash with `width` and `height`
# keys corresponding to the pixel dimensions of the
# source image.
# * `operations` [Array<Hash<String,Object>>] Array of operations in
# order of application. Only operations that are not
# no-ops will be included. Every hash contains a `class`
# key corresponding to the operation class name, which
# will be one of the `e.i.l.c.operation.Operation`
# implementations.
# * `output_format` [String] Output format media (MIME) type.
# * `resulting_size` [Hash<String,Integer>] Hash with `width` and `height`
# keys corresponding to the pixel dimensions of the
# resulting image after all operations have been applied.
#
# @return [Hash] Request context.
#
attr_accessor :context
##
# Tells the server whether to redirect in response to the request. Will be
# called upon all image requests.
#
# @param options [Hash] Empty hash.
# @return [Hash<String,Object>,nil] Hash with `location` and `status_code`
# keys. `location` must be a URI string; `status_code` must be an
# integer from 300 to 399. Return nil for no redirect.
#
def redirect(_options = {})
nil
end
##
# Tells the server whether the given request is authorized. Will be called
# upon all image requests to any endpoint.
#
# Implementations should assume that the underlying resource is available,
# and not try to check for it.
#
# @param options [Hash] Empty hash.
# @return [Boolean] Whether the request is authorized.
#
def authorized?(_options = {})
true
end
##
# Used to add additional keys to an information JSON response. See the
# [Image API specification](http://iiif.io/api/image/2.1/#image-information).
#
# @param options [Hash] Empty hash.
# @return [Hash] Hash that will be merged into an IIIF Image API 2.x
# information response. Return an empty hash to add nothing.
#
def extra_iiif2_information_response_keys(_options = {})
{}
end
##
# Tells the server which source to use for the given identifier.
#
# @param options [Hash] Empty hash.
# @return [String] Source name.
#
def source(_options = {})
'HttpSource'
end
##
# Gets the HTTP-sourced image resource information.
#
# @param options [Hash] Empty hash.
# @return [String,Hash<String,String>,nil] String URI; Hash with `uri` key,
# and optionally `username` and `secret` keys; or nil if not found.
#
def httpsource_resource_info(_options = {})
file_id = context['identifier']
# Split the parts into Fedora's pseudo-pairtree (only first four pairs)
paths = file_id.split(/(.{0,2})/).reject!(&:empty?)[0, 4]
fedora_base_url = ENV['FEDORA_URL'] + ENV['FEDORA_BASE_PATH']
## DCE: Add CGI Unescape
url = fedora_base_url + '/' + paths.join('/') + '/' + file_id
require "cgi"
CGI::unescape(url)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment