Skip to content

Commit f4cf9b5

Browse files
committed
[ESSI-2062] fix iiif image lookup cases being unaware of external_storage
1 parent b3eb285 commit f4cf9b5

9 files changed

+102
-58
lines changed

Diff for: app/controllers/purl_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def rescue_url
7676

7777
def jp2_url(solr_hit)
7878
begin
79-
Hyrax.config.iiif_image_url_builder.call(solr_hit['original_file_id_ssi'], nil, '!600,600')
79+
IIIFFileSetPathService.new(solr_hit).iiif_image_url(size: '!600,600')
8080
rescue StandardError
8181
nil
8282
end

Diff for: app/helpers/catalog_helper.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ def thumbnail_url document
1515
else
1616
representative_document = ::SolrDocument.find(document.thumbnail_id)
1717
end
18-
19-
thumbnail_file_id = representative_document&.content_location
20-
thumbnail_file_id ||= representative_document.original_file_id
21-
if thumbnail_file_id
22-
Hyrax.config.iiif_image_url_builder.call(thumbnail_file_id, nil, '250,')
18+
iiif_path_service = IIIFFileSetPathService.new(representative_document)
19+
if iiif_path_service.lookup_id
20+
iiif_path_service.iiif_image_url(size: '250,')
2321
else
2422
raise 'thumbnail_file_id is nil'
2523
end

Diff for: app/models/collection_branding_info.rb

+6-8
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,10 @@ def find_local_dir_name(collection_id, role)
9595
File.join(Hyrax.config.branding_path, collection_id.to_s, role.to_s)
9696
end
9797

98-
# this passes a nil value for request base_url, as our custom url builder
99-
# does not use that argument, and the model also doesn't have a request
10098
def generate_image_path!
10199
if image_path.blank? && file_set_versions.any?
102-
original_uri = file_set_versions.all.last.uri
103-
uri_to_id = ActiveFedora::File.uri_to_id(original_uri.sub(/\/fcr.versions.*/,''))
104-
self.image_path = \
105-
Hyrax.config.iiif_image_url_builder.call(uri_to_id,
106-
nil,
107-
ESSI.config.dig(:essi, :collection_banner_size) || Hyrax.config.iiif_image_size_default)
100+
return unless iiif_path_service.lookup_id
101+
self.image_path = iiif_path_service.iiif_image_url(size: ESSI.config.dig(:essi, :collection_banner_size))
108102
save
109103
end
110104
end
@@ -117,4 +111,8 @@ def uploaded_files(uploaded_file_ids)
117111
return [] if uploaded_file_ids.empty?
118112
Hyrax::UploadedFile.find(uploaded_file_ids)
119113
end
114+
115+
def iiif_path_service
116+
@iiif_path_service ||= IIIFFileSetPathService.new(file_set)
117+
end
120118
end

Diff for: app/models/file_set.rb

+16
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ def ocr_language
2222
ESSI.config.dig(:essi, :ocr_language),
2323
'eng'].map { |l| Tesseract.try_languages(l) }.select(&:present?).first
2424
end
25+
26+
# @todo revisit after Hyrax 3.x upgrade
27+
# copied from Hyrax::FileSetIndexer to provide a common interface across:
28+
# - FileSet
29+
# - SolrDocument
30+
# - Hyrax::FileSetPresenter, initialized with either of above
31+
def original_file_id
32+
@original_file_id ||= begin
33+
return unless self.original_file
34+
if self.original_file.versions.present?
35+
ActiveFedora::File.uri_to_id(self.current_content_version_uri)
36+
else
37+
self.original_file.id
38+
end
39+
end
40+
end
2541
end

Diff for: app/presenters/hyrax/displays_image.rb

+8-39
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,31 @@ module Hyrax
88
module DisplaysImage
99
extend ActiveSupport::Concern
1010

11-
delegate :content_location, to: :solr_document
12-
1311
# Creates a display image only where FileSet is an image.
1412
#
1513
# @return [IIIFManifest::DisplayImage] the display image required by the manifest builder.
1614
def display_image
1715
return nil unless solr_document.image? && current_ability.can?(:read, id)
18-
19-
latest_file_id = lookup_original_file_id
20-
21-
return nil unless latest_file_id
22-
23-
url = Hyrax.config.iiif_image_url_builder.call(
24-
latest_file_id,
25-
request.base_url,
26-
Hyrax.config.iiif_image_size_default
27-
)
16+
return nil unless iiif_path_service.lookup_id
2817

2918
# @see https://github.com/samvera-labs/iiif_manifest
30-
IIIFManifest::DisplayImage.new(url,
19+
IIIFManifest::DisplayImage.new(iiif_path_service.iiif_image_url,
3120
width: width,
3221
height: height,
33-
iiif_endpoint: iiif_endpoint(latest_file_id))
34-
end
35-
36-
def lookup_original_file_id
37-
return content_location if content_location&.start_with?('s3://')
38-
result = original_file_id
39-
if result.blank?
40-
Rails.logger.warn "original_file_id for #{id} not found, falling back to Fedora."
41-
# result = Hyrax::VersioningService.versioned_file_id ::FileSet.find(id).original_file
42-
result = versioned_file_id ::FileSet.find(id).original_file
43-
end
44-
result
22+
iiif_endpoint: iiif_endpoint)
4523
end
4624

4725
private
26+
def iiif_path_service
27+
@iiif_path_service ||= IIIFFileSetPathService.new(solr_document)
28+
end
4829

49-
def iiif_endpoint(file_id)
30+
def iiif_endpoint
5031
return unless Hyrax.config.iiif_image_server?
5132
IIIFManifest::IIIFEndpoint.new(
52-
Hyrax.config.iiif_info_url_builder.call(file_id, request.base_url),
33+
iiif_path_service.iiif_info_url(request.base_url),
5334
profile: Hyrax.config.iiif_image_compliance_level_uri
5435
)
5536
end
56-
57-
# @todo remove after upgrade to Hyrax 3.x
58-
# cherry-picked from Hyrax 3.x VersioningService
59-
# @param [ActiveFedora::File | Hyrax::FileMetadata] content
60-
def versioned_file_id(file)
61-
versions = file.versions.all
62-
if versions.present?
63-
ActiveFedora::File.uri_to_id versions.last.uri
64-
else
65-
file.id
66-
end
67-
end
6837
end
6938
end

Diff for: app/services/essi/generate_pdf_service.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def create_tmp_files(pdf)
4444
fs_solr = SolrDocument.find(fs)
4545
image_width = get_image_width(fs_solr).to_i
4646
raise StandardError, 'Image width unavailable' unless image_width > 0 # IIIF server call requires a positive integer value
47-
uri = Hyrax.config.iiif_image_url_builder.call(fs_solr.original_file_id, nil, render_dimensions(image_width))
47+
iiif_path_service = IIIFFileSetPathService.new(fs_solr)
48+
raise StandardError, 'Source image file unavailable' unless iiif_path_service.lookup_id
49+
uri = iiif_path_service.iiif_image_url(size: render_dimensions(image_width))
4850
URI.open(uri) do |file|
4951
page_size = [CoverPageGenerator::LETTER_WIDTH, CoverPageGenerator::LETTER_HEIGHT]
5052
file.binmode

Diff for: app/services/iiif_collection_thumbnail_path_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class << self
33
# @return the network path to the thumbnail
44
# @param [FileSet] thumbnail the object that is the thumbnail
55
def thumbnail_path(thumbnail)
6-
Hyrax.config.iiif_image_url_builder.call(thumbnail.original_file.id, nil, '250,')
6+
IIIFFileSetPathService.new(thumbnail).iiif_image_url(size: '250,')
77
# Hyrax::Engine.routes.url_helpers.download_path(thumbnail.id,
88
# file: 'thumbnail')
99
end

Diff for: app/services/iiif_file_set_path_service.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class IIIFFileSetPathService
2+
attr_reader :file_set, :lookup_id
3+
4+
# @param [ActiveFedora::SolrHit, FileSet, SolrDocument, FileSetPresenter] file_set
5+
def initialize(file_set, versioned_lookup: false)
6+
file_set = SolrDocument.new(file_set) if file_set.is_a? ActiveFedora::SolrHit
7+
@file_set = file_set
8+
@lookup_id = (versioned_lookup ? versioned_lookup_id : basic_lookup_id)
9+
end
10+
11+
# @return [String] a URL that resolves to an image provided by a IIIF image server
12+
def iiif_image_url(base_url: nil, size: nil)
13+
return unless lookup_id
14+
Hyrax.config.iiif_image_url_builder.call(lookup_id, base_url, size || Hyrax.config.iiif_image_size_default)
15+
end
16+
17+
# @return [String] a URL that resolves to an info.json file provided by a IIIF image server
18+
def iiif_info_url(base_url)
19+
return unless lookup_id
20+
Hyrax.config.iiif_info_url_builder.call(lookup_id, base_url)
21+
end
22+
23+
private
24+
# imported logic from IIIFThumbnailPathService, etc
25+
def basic_lookup_id
26+
file_set.content_location || file_set.original_file_id
27+
end
28+
29+
# imported from Hyrax::DisplaysImage
30+
def versioned_lookup_id
31+
return file_set.content_location if file_set.content_location&.start_with?('s3://')
32+
result = file_set.original_file_id
33+
if result.blank?
34+
Rails.logger.warn "original_file_id for #{id} not found, falling back to Fedora."
35+
# result = Hyrax::VersioningService.versioned_file_id(original_file)
36+
result = versioned_file_id(original_file)
37+
end
38+
result
39+
end
40+
41+
# @return Hydra::PCDM::File
42+
def original_file
43+
@original_file ||=
44+
case file_set
45+
when FileSet
46+
file_set.original_file
47+
else
48+
FileSet.find(id).original_file
49+
end
50+
end
51+
52+
# @todo remove after upgrade to Hyrax 3.x
53+
# cherry-picked from Hyrax 3.x VersioningService
54+
# @param [ActiveFedora::File | Hyrax::FileMetadata] content
55+
def versioned_file_id(file)
56+
versions = file.versions.all
57+
if versions.present?
58+
ActiveFedora::File.uri_to_id versions.last.uri
59+
else
60+
file.id
61+
end
62+
end
63+
end

Diff for: app/services/iiif_thumbnail_path_service.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ class << self
33
# @return the network path to the thumbnail
44
# @param [FileSet] thumbnail the object that is the thumbnail
55
def thumbnail_path(thumbnail)
6-
return unless thumbnail.original_file
7-
id = thumbnail.content_location || thumbnail.original_file.id
8-
Hyrax.config.iiif_image_url_builder.call(id, nil, '250,')
6+
IIIFFileSetPathService.new(thumbnail).iiif_image_url(size: '250,')
97
# Hyrax::Engine.routes.url_helpers.download_path(thumbnail.id,
108
# file: 'thumbnail')
119
end

0 commit comments

Comments
 (0)