Skip to content

Commit

Permalink
Merge pull request #687 from UCLALibrary/iiif
Browse files Browse the repository at this point in the history
Use IIIF URLs that won't depend on Hyrax / Fedora
  • Loading branch information
sourcefilter authored Jul 24, 2019
2 parents 5adf8e8 + c46db05 commit a9eb587
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 58 deletions.
2 changes: 1 addition & 1 deletion app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def self.modified_field
# solr field configuration for document/show views
config.index.title_field = solr_name('title', :stored_searchable)
config.index.display_type_field = solr_name('has_model', :symbol)
config.index.thumbnail_field = 'thumbnail_path_ss'
config.index.thumbnail_field = 'thumbnail_url_ss'

# solr fields that will be treated as facets by the blacklight application
# The ordering of the field names is the order of the display
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/hyrax/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def manifest_builder
else
curation_concern = _curation_concern_type.find(params[:id]) unless curation_concern
builder_service = Californica::ManifestBuilderService.new(curation_concern: curation_concern)
@sets = builder_service.sets
@image_concerns = builder_service.image_concerns
@root_url = "#{request.protocol}#{request.host_with_port}/concern/works/#{@solr_doc.id}/manifest"

manifest_json = render_to_string('/manifest.json')
Expand Down
13 changes: 4 additions & 9 deletions app/services/californica/manifest_builder_service.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
# frozen_string_literal: true
module Californica
class ManifestBuilderService
attr_accessor :curation_concern

def initialize(curation_concern:)
@curation_concern = curation_concern
@child_works = @curation_concern.ordered_members.to_a.select { |member| member.class == ChildWork || member.class == Work }
@file_sets = @curation_concern.ordered_members.to_a.select { |member| member.class == FileSet }
end

def sets
sets = if @child_works.length >= 1
@child_works.each(&:file_sets) + @file_sets.each(&:files)
else
@file_sets.each(&:files)
end
sets
def image_concerns
@image_concerns ||= ([@curation_concern] + @curation_concern.ordered_members.to_a).select { |member| member.respond_to?(:master_file_path) && member.master_file_path }
end
end
end
6 changes: 3 additions & 3 deletions app/views/hyrax/base/_representative_media.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% has_child_works = !Californica::ChildWorkService.new.child_works(member_ids_ssim: SolrDocument.find(presenter.id)[:member_ids_ssim]).empty? %>
<% if presenter.representative_id.present? && presenter.representative_presenter.present? || has_child_works %>
<% if defined?(viewer) && viewer || has_child_works %>
<% has_images = !Californica::ManifestBuilderService.new(curation_concern: ActiveFedora::Base.find(presenter.id)).image_concerns.empty? %>
<% if presenter.representative_id.present? && presenter.representative_presenter.present? || has_images %>
<% if defined?(viewer) && viewer || has_images %>
<%= iiif_viewer_display presenter %>
<% else %>
<%= media_display presenter.representative_presenter %>
Expand Down
14 changes: 3 additions & 11 deletions app/views/manifest.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,16 @@ json.description @solr_doc.description.first
json.sequences [''] do
json.set! :@type, 'sc:Sequence'
json.set! :@id, "#{@root_url}/sequence/normal"
json.canvases @sets do |child|
json.canvases @image_concerns do |child|
json.set! :@id, "#{@root_url}/canvas/#{child.id}"
json.set! :@type, 'sc:Canvas'
json.label child.title.first
json.description child.description.first
json.width 640
json.height 480
json.images [child] do |child_image|
file_set_id = if child_image.try(:file_sets)
child_image.file_sets.first.id
else
child_image.id
end

original_file = ::FileSet.find(file_set_id).original_file

url = Hyrax.config.iiif_image_url_builder.call(
original_file.id,
CGI.escape(child_image.master_file_path),
request.base_url,
Hyrax.config.iiif_image_size_default
)
Expand All @@ -44,7 +36,7 @@ json.sequences [''] do

# The base url for the info.json file
info_url = Hyrax.config.iiif_info_url_builder.call(
original_file.id,
CGI.escape(child_image.master_file_path),
ENV['IIIF_SERVER_URL'] || request.base_url
)

Expand Down
54 changes: 43 additions & 11 deletions spec/services/californica/manifest_builder_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,54 @@
require 'rails_helper'

RSpec.describe Californica::ManifestBuilderService do
let(:work) { FactoryBot.create(:work) }
let(:work2) { FactoryBot.create(:work) }
let(:work_with_file_sets) { FactoryBot.create(:work) }
let(:parent_image_path) { 'parent/image.tif' }
let(:child_image_path_1) { 'child/image_1.tif' }
let(:child_image_path_2) { 'child/image_2.tif' }
let(:child_work1) { FactoryBot.create(:child_work, master_file_path: child_image_path_1) }
let(:child_work2) { FactoryBot.create(:child_work, master_file_path: child_image_path_2) }
let(:work) do
w = FactoryBot.create(:work, master_file_path: parent_image_path)
w.ordered_members << child_work1
w.ordered_members << child_work2
w
end
let(:service) { described_class.new(curation_concern: work) }
let(:service_with_file_sets) { described_class.new(curation_concern: work_with_file_sets) }
let(:file_set) { FactoryBot.create(:file_set) }

describe '#sets' do
describe '#image_concerns' do
it 'returns the set of works needed to create a manifest' do
work.ordered_members << work2
expect(service.sets.first.class).to eq Work
expect(service.image_concerns).to eq [work, child_work1, child_work2]
end

context 'When the parent work has no master_file_path' do
let(:parent_image_path) { nil }

it 'returns the set of child works' do
expect(service.image_concerns).to eq [child_work1, child_work2]
end
end

it 'returns the set of filsets needed to create a manifest' do
work_with_file_sets.ordered_members << file_set
expect(service_with_file_sets.sets.first.class).to eq FileSet
context 'When the parent work has no children' do
let(:work) { FactoryBot.create(:work, master_file_path: parent_image_path) }

it 'returns only the parent' do
expect(service.image_concerns).to eq [work]
end
end

context 'When the parent work has neither master_file_path nor children' do
let(:work) { FactoryBot.create(:work, master_file_path: nil) }

it 'returns nothing' do
expect(service.image_concerns).to eq []
end
end

context 'When the a child work has no master_file_path' do
let(:child_image_path_1) { nil }

it 'does not include the child' do
expect(service.image_concerns).to eq [work, child_work2]
end
end
end
end
22 changes: 11 additions & 11 deletions spec/views/manifest.json.jbuilder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
require 'cgi'

RSpec.describe "manifest", type: :view do
let(:tif) { File.open(Rails.root.join('spec', 'fixtures', 'images', 'good', 'food.tif')) }
let(:work) { FactoryBot.create(:work, tif: tif) }
let(:master_file_path) { 'a/b.tif' }
let(:work) { FactoryBot.create(:work, master_file_path: master_file_path) }
let(:solr_doc) { SolrDocument.find(work.id) }
let(:sets) { Californica::ManifestBuilderService.new(curation_concern: work).sets }
let(:image_concerns) { Californica::ManifestBuilderService.new(curation_concern: work).image_concerns }

before do
assign(:root_url, 'http://localhost:3000/manifest')
assign(:solr_doc, solr_doc)
assign(:sets, sets)
assign(:image_concerns, image_concerns)
end

it "displays a valid IIIF Presentation API manifest" do
render
file_id = CGI.escape(work.file_sets.first.original_file.id)
iiif_id = CGI.escape(work.master_file_path)
doc = <<~HEREDOC
{
"@context": "http://iiif.io/api/presentation/2/context.json",
Expand All @@ -30,10 +30,10 @@
"@id": "http://localhost:3000/manifest/sequence/normal",
"canvases": [
{
"@id": "http://localhost:3000/manifest/canvas/#{work.file_sets.first.id}",
"@id": "http://localhost:3000/manifest/canvas/#{work.id}",
"@type": "sc:Canvas",
"label": null,
"description": null,
"label": "#{work.title.first}",
"description": "#{work.description.first}",
"width": 640,
"height": 480,
"images": [
Expand All @@ -42,16 +42,16 @@
"motivation": "sc:painting",
"resource": {
"@type": "dctypes:Image",
"@id": "#{ENV['IIIF_SERVER_URL']}#{file_id}/full/600,/0/default.jpg",
"@id": "#{ENV['IIIF_SERVER_URL']}#{iiif_id}/full/600,/0/default.jpg",
"width": 640,
"height": 480,
"service": {
"@context": "http://iiif.io/api/image/2/context.json",
"@id": "#{ENV['IIIF_SERVER_URL']}#{file_id}",
"@id": "#{ENV['IIIF_SERVER_URL']}#{iiif_id}",
"profile": "http://iiif.io/api/image/2/level2.json"
}
},
"on": "http://test.host/concern/works/#{work.id}/manifest/canvas/#{work.file_sets.first.id}"
"on": "http://test.host/concern/works/#{work.id}/manifest/canvas/#{work.id}"
}
]
}
Expand Down
22 changes: 11 additions & 11 deletions spec/views/manifest.json.jbuilder_with_child_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
require 'cgi'

RSpec.describe "manifest", type: :view do
let(:tif) { File.open(Rails.root.join('spec', 'fixtures', 'images', 'good', 'food.tif')) }
let(:master_file_path) { 'a/b/c.tif' }
let(:work) { FactoryBot.create(:work, child_work: child_work) }
let(:child_work) { FactoryBot.create(:work, tif: tif) }
let(:child_work) { FactoryBot.create(:work, master_file_path: master_file_path) }
let(:solr_doc) { SolrDocument.find(work.id) }
let(:sets) { Californica::ManifestBuilderService.new(curation_concern: work).sets }
let(:image_concerns) { Californica::ManifestBuilderService.new(curation_concern: work).image_concerns }

before do
assign(:root_url, 'http://localhost:3000/manifest')
assign(:solr_doc, solr_doc)
assign(:sets, sets)
assign(:image_concerns, image_concerns)
end

it "displays manifest for a work with child works" do
render
file_id = CGI.escape(work.ordered_members.to_a.first.file_sets.first.original_file.id)
iiif_id = CGI.escape(master_file_path)
doc = <<~HEREDOC
{
"@context": "http://iiif.io/api/presentation/2/context.json",
Expand All @@ -31,10 +31,10 @@
"@id": "http://localhost:3000/manifest/sequence/normal",
"canvases": [
{
"@id": "http://localhost:3000/manifest/canvas/#{work.ordered_members.to_a.first.id}",
"@id": "http://localhost:3000/manifest/canvas/#{child_work.id}",
"@type": "sc:Canvas",
"label": "#{work.ordered_members.to_a.first.title.first}",
"description": "#{work.ordered_members.to_a.first.description.first}",
"label": "#{child_work.title.first}",
"description": "#{child_work.description.first}",
"width": 640,
"height": 480,
"images": [
Expand All @@ -43,16 +43,16 @@
"motivation": "sc:painting",
"resource": {
"@type": "dctypes:Image",
"@id": "#{ENV['IIIF_SERVER_URL']}#{file_id}/full/600,/0/default.jpg",
"@id": "#{ENV['IIIF_SERVER_URL']}#{iiif_id}/full/600,/0/default.jpg",
"width": 640,
"height": 480,
"service": {
"@context": "http://iiif.io/api/image/2/context.json",
"@id": "#{ENV['IIIF_SERVER_URL']}#{file_id}",
"@id": "#{ENV['IIIF_SERVER_URL']}#{iiif_id}",
"profile": "http://iiif.io/api/image/2/level2.json"
}
},
"on": "http://test.host/concern/works/#{work.id}/manifest/canvas/#{work.ordered_members.to_a.first.id}"
"on": "http://test.host/concern/works/#{work.id}/manifest/canvas/#{child_work.id}"
}
]
}
Expand Down

0 comments on commit a9eb587

Please sign in to comment.