Skip to content

Commit

Permalink
IIIF #63 - Updating manifest generation to use IIIF Cloud URLs instea…
Browse files Browse the repository at this point in the history
…d of Cantaloupe IIIF URLs
  • Loading branch information
dleadbetter committed Jan 14, 2025
1 parent 52b90e2 commit 71c79af
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
44 changes: 34 additions & 10 deletions app/controllers/public/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,44 @@ class Public::ResourcesController < Api::ResourcesController
include Public::Authenticateable

# Actions
prepend_before_action :set_page, only: [:iiif, :info]
prepend_before_action :set_project_id, only: :index
prepend_before_action :set_resource_id, only: [:show, :destroy, :update]
prepend_before_action :set_resource_project_id, only: [:create, :update]
skip_before_action :authenticate_request, only: [:content, :download, :iiif, :info, :inline, :manifest, :preview, :thumbnail]

def content
redirect_resource { :content_url }
redirect_resource do |resource|
resource.content_url
end
end

def download
redirect_resource { :content_download_url }
redirect_resource do |resource|
resource.content_download_url
end
end

def iiif
redirect_resource { |resource| resource.image? ? :content_converted_iiif_url : :content_iiif_url }
page_number = params[:page] || 1

redirect_resource do |resource|
resource.image? ? resource.content_converted_iiif_url(page_number) : resource.content_iiif_url(page_number)
end
end

def info
redirect_resource { |resource| resource.image? ? :content_converted_iiif_url : :content_iiif_url }
page_number = params[:page] || 1

redirect_resource do |resource|
resource.image? ? resource.content_converted_info_url(page_number) : resource.content_info_url(page_number)
end
end

def inline
redirect_resource { :content_inline_url }
redirect_resource do |resource|
resource.content_inline_url
end
end

def manifest
Expand All @@ -34,27 +49,36 @@ def manifest
end

def preview
redirect_resource { |resource| resource.image? ? :content_converted_preview_url : :content_iiif_url }
redirect_resource do |resource|
resource.image? ? resource.content_converted_preview_url : resource.content_preview_url
end
end

def thumbnail
redirect_resource { |resource| resource.image? ? :content_converted_thumbnail_url : :content_thumbnail_url }
redirect_resource do |resource|
resource.image? ? resource.content_converted_thumbnail_url : resource.content_thumbnail_url
end
end

private

def redirect_resource
resource = Resource.find_by_uuid(params[:id])
render status: :not_found and return if resource.nil?

attribute = yield resource

redirect = resource.send(attribute)
redirect = yield resource
render status: :not_found and return if redirect.nil?

redirect_to redirect, allow_other_host: true
end

def set_page
id, page = params[:id].split(';')

params[:id] = id
params[:page] = page
end

def set_project_id
project = Project.find_by_uuid(params[:project_id])
render_unauthorized I18n.t('errors.unauthorized') and return if project.nil?
Expand Down
6 changes: 5 additions & 1 deletion app/services/iiif/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def self.create(id:, label:, items:)

private

def self.base_url(resource)
"#{ENV['HOSTNAME']}/public/resources/#{resource.uuid}"
end

def self.to_item(item, resource)
collection_item = item.slice(:id, :type)

Expand All @@ -34,7 +38,7 @@ def self.to_item(item, resource)

if resource.present?
collection_item[:thumbnail] = [{
id: resource.content_thumbnail_url,
id: "#{base_url(resource)}/thumbnail",
type: 'Image',
format: 'image/jpeg',
width: 250,
Expand Down
10 changes: 5 additions & 5 deletions app/services/iiif/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def self.base_url(resource)

def self.create_annotation(resource, target, page_number)
annotation = to_json('annotation.json')
annotation['id'] = "#{base_url(resource)}/page/#{page_number}/annotation_page/1/annotation/1"
annotation['id'] = "#{base_url(resource)}/canvas/#{page_number}/page/1/annotation/1"
annotation['target'] = target

if resource.image? || resource.pdf?
id = resource.content_iiif_url(page_number)
id = "#{base_url(resource)};#{page_number}/iiif"
else
id = resource.content_url
end
Expand All @@ -73,7 +73,7 @@ def self.create_annotation(resource, target, page_number)

if resource.image? || resource.pdf?
annotation['body']['service'] = [{
id: "#{resource.content_base_url};#{page_number}",
id: "#{base_url(resource)};#{page_number}",
type: 'ImageService3',
profile: 'http://iiif.io/api/image/3/level2.json'
}]
Expand All @@ -84,7 +84,7 @@ def self.create_annotation(resource, target, page_number)

def self.create_canvas(resource, width, height, page_number)
canvas = to_json('canvas.json')
canvas['id'] = "#{base_url(resource)}/page/#{page_number}"
canvas['id'] = "#{base_url(resource)}/canvas/#{page_number}"
canvas['width'] = width
canvas['height'] = height
canvas['label'] = {
Expand All @@ -94,7 +94,7 @@ def self.create_canvas(resource, width, height, page_number)
}

canvas['items'] = [{
id: "#{base_url(resource)}/page/#{page_number}/annotation_page/1",
id: "#{base_url(resource)}/canvas/#{page_number}/page/1",
type: 'AnnotationPage',
items: [create_annotation(resource, canvas['id'], page_number)]
}]
Expand Down
4 changes: 3 additions & 1 deletion config/initializers/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '/public/resources/:id/manifest', headers: :any, methods: :get
resource '/public/resources/:id/content', headers: :any, methods: :get
resource '/public/resources/:id/manifest', headers: :any, methods: :get
resource '/public/resources/:id/iiif', headers: :any, methods: :get
resource '/public/resources/:id/info*', headers: :any, methods: :get
resource '*/rails/active_storage/blobs/redirect/*', headers: :any, methods: :get
end
end

0 comments on commit 71c79af

Please sign in to comment.