Skip to content

Commit

Permalink
Refactor DisplayImagePresenterBehavior
Browse files Browse the repository at this point in the history
This commit will add OVERRIDE comments where useful and add a blurb in
the README for this new feature.  The #display_content method was added
to support presentation 3 manifests.  `SERVERLESS` has been renamed to
`EXTERNAL`.  `DisplayImagePresenterBehavior` has beeen refactored to
incorporate existing code.  Specs have also been refactored.
  • Loading branch information
kirkkwang committed Mar 24, 2023
1 parent 8bbbd27 commit 4a08e55
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 40 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ IiifPrint supports:
* configuring how the manifest canvases are sorted in the viewer
* adding metadata fields to the manifest with faceted search links and external links
* excluding specified work types to be found in the catalog search
* external IIIF image urls that work with services such as AWS

A complete list of features can be found [here](https://github.com/scientist-softserv/iiif_print/wiki/Features-List).

Expand Down Expand Up @@ -153,7 +154,7 @@ TO ENABLE OCR Search (from the UV and catalog search)
}
```

To remove child works from recent works on homepage
To remove child works from recent works on homepage
### homepage_controller.rb
* In the HomepageController, change the search_builder_class to remove works from recent_documents if `is_child_bsi: true`
```rb
Expand Down
2 changes: 1 addition & 1 deletion app/indexers/concerns/iiif_print/file_set_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def generate_solr_document
end
end

private
private

def digest_from_content
return unless object.original_file
Expand Down
55 changes: 43 additions & 12 deletions app/presenters/iiif_print/iiif_manifest_presenter_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module IiifPrint
module IiifManifestPresenterBehavior
extend ActiveSupport::Concern

# Extending the presenter to the base url which includes the protocol.
# We need the base url to render the facet links and normalize the interface.
attr_accessor :base_url

def manifest_metadata
@manifest_metadata ||= IiifPrint.manifest_metadata_from(work: model, presenter: self)
end
Expand All @@ -11,7 +15,7 @@ def search_service
Rails.application.routes.url_helpers.solr_document_iiif_search_url(id, host: hostname)
end

# OVERRIDE Hyrax 3x, avoid nil returning to IIIF Manifest gem
# OVERRIDE: Hyrax 3x, avoid nil returning to IIIF Manifest gem
# @see https://github.com/samvera/iiif_manifest/blob/c408f90eba11bef908796c7236ba6bcf8d687acc/lib/iiif_manifest/v3/manifest_builder/record_property_builder.rb#L28
##
# @return [Array<Hash{String => String}>]
Expand All @@ -26,20 +30,47 @@ def sequence_rendering
end.flatten
end

# OVERRIDE: Hyrax v3.x
module DisplayImagePresenterBehavior
# Extending the presenter to the base url which includes the protocol.
# We need the base url to render the facet links and normalize the interface.
attr_accessor :base_url

# Extending this class because there is an #ability= but not #ability and this definition
# mirrors the Hyrax::IiifManifestPresenter#ability.
def ability
@ability ||= NullAbility.new
end

def display_image
return nil unless latest_file_id
return nil unless model.image?
return nil unless IiifPrint.config.default_iiif_manifest_version == 2

IIIFManifest::DisplayImage
.new(display_image_url(hostname),
format: image_format(alpha_channels),
width: width,
height: height,
iiif_endpoint: iiif_endpoint(latest_file_id, base_url: hostname))
format: image_format(alpha_channels),
width: width,
height: height,
iiif_endpoint: iiif_endpoint(latest_file_id, base_url: hostname))
end

# OVERRIDE: IIIF Hyrax AV v0.2 #display_content for prez 3 manifests
def display_content
return nil unless latest_file_id
return super unless model.image?

IIIFManifest::V3::DisplayContent
.new(display_image_url(hostname),
format: image_format(alpha_channels),
width: width,
height: height,
type: 'Image',
iiif_endpoint: iiif_endpoint(latest_file_id, base_url: hostname))
end

def display_image_url(base_url)
if ENV['SERVERLESS_IIIF_URL'].present?
if ENV['EXTERNAL_IIIF_URL'].present?
# At the moment we are only concerned about Hyrax's default image url builder
iiif_image_url_builder(url_builder: Hyrax.config.iiif_image_url_builder)
else
Expand All @@ -48,9 +79,9 @@ def display_image_url(base_url)
end

def iiif_endpoint(file_id, base_url: request.base_url)
if ENV['SERVERLESS_IIIF_URL'].present?
if ENV['EXTERNAL_IIIF_URL'].present?
IIIFManifest::IIIFEndpoint.new(
File.join(ENV['SERVERLESS_IIIF_URL'], file_id),
File.join(ENV['EXTERNAL_IIIF_URL'], file_id),
profile: Hyrax.config.iiif_image_compliance_level_uri
)
else
Expand All @@ -71,21 +102,21 @@ def work?
private

def latest_file_id
if ENV['SERVERLESS_IIIF_URL'].present?
serverless_latest_file_id
if ENV['EXTERNAL_IIIF_URL'].present?
external_latest_file_id
else
super
end
end

def serverless_latest_file_id
def external_latest_file_id
@latest_file_id ||= digest_sha1
end

def iiif_image_url_builder(url_builder:)
args = [
latest_file_id,
ENV['SERVERLESS_IIIF_URL'],
ENV['EXTERNAL_IIIF_URL'],
Hyrax.config.iiif_image_size_default
]
# In Hyrax 3, Hyrax.config.iiif_image_url_builder takes an additional argument
Expand Down
13 changes: 0 additions & 13 deletions lib/iiif_print/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ class Engine < ::Rails::Engine
::BlacklightIiifSearch::IiifSearchAnnotation.prepend(IiifPrint::BlacklightIiifSearch::AnnotationDecorator)
Hyrax::Actors::FileSetActor.prepend(IiifPrint::Actors::FileSetActorDecorator)

# Extending the presenter to the base url which includes the protocol.
# We need the base url to render the facet links and normalize the interface.
Hyrax::IiifManifestPresenter.send(:attr_accessor, :base_url)
Hyrax::IiifManifestPresenter::DisplayImagePresenter.send(:attr_accessor, :base_url)
# Extending this class because there is an #ability= but not #ability and this definition
# mirrors the Hyrax::IiifManifestPresenter#ability.
module Hyrax::IiifManifestPresenter::DisplayImagePresenterDecorator
def ability
@ability ||= NullAbility.new
end
end
Hyrax::IiifManifestPresenter::DisplayImagePresenter.prepend(Hyrax::IiifManifestPresenter::DisplayImagePresenterDecorator)

Hyrax.config do |config|
config.callback.set(:after_create_fileset) do |file_set, user|
IiifPrint.config.handle_after_create_fileset(file_set, user)
Expand Down
48 changes: 35 additions & 13 deletions spec/presenters/iiif_print/iiif_manifest_presenter_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,53 @@
end
end

# this method is inside of the DisplayImagePresenterBehavior module
describe '#display_image' do
context 'with IIIF external support' do
let(:presenter) { Hyrax::IiifManifestPresenter::DisplayImagePresenter.new(solr_document) }
let(:id) { 'abc123' }
let(:url) { 'external_iiif_url' }
let(:iiif_info_url_builder) { ->(file_id, base_url) { "#{base_url}/#{file_id}" } }

context 'when serverless_iiif is enabled' do
let(:url) { 'serverless_iiif_url' }
before { allow(solr_document).to receive(:image?).and_return(true) }

it 'renders a serverless url' do
context 'when external iiif is enabled' do
before do
allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with('SERVERLESS_IIIF_URL').and_return(url)
allow(ENV).to receive(:[]).with('EXTERNAL_IIIF_URL').and_return(url)
allow(presenter).to receive(:latest_file_id).and_return(id)
expect(presenter.display_image.iiif_endpoint.url).to eq "#{url}/#{id}"
expect(presenter.display_image.iiif_endpoint.profile).to eq "http://iiif.io/api/image/2/level2.json"
end
end

context 'when serverless_iiif is not enabled' do
let(:iiif_info_url_builder) { ->(file_id, base_url) { "#{base_url}/#{file_id}" } }
describe '#display_image' do
it 'renders a external url' do
expect(presenter.display_image.iiif_endpoint.url).to eq "#{url}/#{id}"
expect(presenter.display_image.iiif_endpoint.profile).to eq "http://iiif.io/api/image/2/level2.json"
end
end

describe '#display_content' do
it 'renders a external url' do
expect(presenter.display_content.iiif_endpoint.url).to eq "#{url}/#{id}"
expect(presenter.display_content.iiif_endpoint.profile).to eq "http://iiif.io/api/image/2/level2.json"
end
end
end

it 'does not render a serverless url' do
context 'when external iiif is not enabled' do
before do
allow(presenter).to receive(:latest_file_id).and_return(id)
allow(Hyrax.config).to receive(:iiif_image_server?).and_return(true)
allow(Hyrax.config).to receive(:iiif_info_url_builder).and_return(iiif_info_url_builder)
expect(presenter.display_image.iiif_endpoint.url).to eq "localhost/#{id}"
end

describe '#display_image' do
it 'does not render a external url' do
expect(presenter.display_image.iiif_endpoint.url).to eq "localhost/#{id}"
end
end

describe '#display_content' do
it 'does not render a external url' do
expect(presenter.display_content.iiif_endpoint.url).to eq "localhost/#{id}"
end
end
end
end
Expand Down

0 comments on commit 4a08e55

Please sign in to comment.