diff --git a/app/controllers/public/resources_controller.rb b/app/controllers/public/resources_controller.rb index 69db471..828a814 100644 --- a/app/controllers/public/resources_controller.rb +++ b/app/controllers/public/resources_controller.rb @@ -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 @@ -34,11 +49,15 @@ 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 @@ -46,15 +65,20 @@ def thumbnail 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? diff --git a/app/services/iiif/collection.rb b/app/services/iiif/collection.rb index 4fccdec..a8ad0e6 100644 --- a/app/services/iiif/collection.rb +++ b/app/services/iiif/collection.rb @@ -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) @@ -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, diff --git a/app/services/iiif/manifest.rb b/app/services/iiif/manifest.rb index f4c7e91..f41c623 100644 --- a/app/services/iiif/manifest.rb +++ b/app/services/iiif/manifest.rb @@ -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 @@ -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' }] @@ -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'] = { @@ -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)] }] diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index daabfa7..07aba21 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -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