-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ESSI-2062] fix iiif image lookup cases being unaware of external_sto…
…rage
- Loading branch information
Showing
12 changed files
with
250 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
class IIIFFileSetPathService | ||
attr_reader :file_set, :versioned_lookup | ||
|
||
# @param [ActiveFedora::SolrHit, FileSet, Hash, SolrDocument, Hyrax::FileSetPresenter] file_set | ||
# @param [TrueClass, FalseClass, NilClass] versioned_lookup: whether to use versioned file lookup if original_file_id fails | ||
def initialize(file_set, versioned_lookup: false) | ||
@versioned_lookup = versioned_lookup | ||
file_set = SolrDocument.new(file_set) if file_set.class.in? [ActiveFedora::SolrHit, Hash] | ||
if [:id, :content_location, :original_file_id].map { |m| file_set.respond_to?(m) }.all? | ||
@file_set = file_set | ||
else | ||
raise StandardError, 'Provided file_set does not meet interface requirements' | ||
end | ||
end | ||
|
||
def lookup_id | ||
@lookup_id ||= (@versioned_lookup ? versioned_lookup_id : basic_lookup_id) | ||
end | ||
|
||
# @return [String] a URL that resolves to an image provided by a IIIF image server | ||
def iiif_image_url(base_url: nil, size: nil) | ||
return unless lookup_id | ||
Hyrax.config.iiif_image_url_builder.call(lookup_id, base_url, size || Hyrax.config.iiif_image_size_default) | ||
end | ||
|
||
# @return [String] a URL that resolves to an info.json file provided by a IIIF image server | ||
def iiif_info_url(base_url) | ||
return unless lookup_id | ||
Hyrax.config.iiif_info_url_builder.call(lookup_id, base_url) | ||
end | ||
|
||
private | ||
# imported logic from IIIFThumbnailPathService, etc | ||
def basic_lookup_id | ||
@basic_lookup_id ||= (file_set.content_location || file_set.original_file_id) | ||
end | ||
|
||
# imported from Hyrax::DisplaysImage | ||
def versioned_lookup_id | ||
@versioned_lookup_id ||= begin | ||
return file_set.content_location if file_set.content_location&.start_with?('s3://') | ||
result = file_set.original_file_id | ||
if result.blank? | ||
Rails.logger.warn "original_file_id for #{file_set.id} not found, falling back to Fedora." | ||
# result = Hyrax::VersioningService.versioned_file_id(original_file) | ||
result = versioned_file_id(original_file) if original_file | ||
|
||
end | ||
if result.blank? | ||
Rails.logger.warn "original_file for #{file_set.id} not found, versioned_lookup_id failed." | ||
nil | ||
else | ||
result | ||
end | ||
end | ||
end | ||
|
||
# @return [Hydra::PCDM::File, nil] | ||
def original_file | ||
@original_file ||= | ||
case file_set | ||
when FileSet | ||
file_set.original_file | ||
else | ||
begin | ||
FileSet.find(file_set.id).original_file | ||
rescue => error | ||
Rails.logger.error "original_file lookup for #{file_set.id} raised error: #{error.inspect}" | ||
nil | ||
end | ||
end | ||
end | ||
|
||
# @todo remove after upgrade to Hyrax 3.x | ||
# cherry-picked from Hyrax 3.x VersioningService | ||
# @param [ActiveFedora::File | Hyrax::FileMetadata] content | ||
def versioned_file_id(file) | ||
@versioned_file_id ||= begin | ||
raise StandardError, 'No original_file available for versioning' if file.nil? | ||
versions = file.versions.all | ||
if versions.present? | ||
ActiveFedora::File.uri_to_id versions.last.uri | ||
else | ||
file.id | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Extensions | ||
module Hyrax | ||
module FileSetPresenter | ||
module ContentLocation | ||
delegate :content_location, to: :solr_document | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe IIIFFileSetPathService do | ||
let(:base_url) { 'http://localhost:3000/' } | ||
let(:content_location) { 's3://localhost:9000/essi-test/ext-store/12/34/ab/cd/1234abcd-original_file.ptif' } | ||
let(:local_file) { File.open(RSpec.configuration.fixture_path + '/world.png') } | ||
# must explicitly define :file_set, normally as one of three following options | ||
let(:empty_file_set) { FactoryBot.create(:file_set) } | ||
let(:local_file_set) { FactoryBot.create(:file_set, content: local_file) } | ||
let(:remote_file_set) { FactoryBot.create(:file_set, content_location: content_location) } | ||
let(:solr_hit) { FileSet.search_with_conditions(id: file_set.id).first } | ||
let(:solr_document) { SolrDocument.new(solr_hit) } | ||
# must explicitly define :source if using file_set_presenter | ||
let(:file_set_presenter) { Hyrax::FileSetPresenter.new(source, Ability.new(FactoryBot.build(:user))) } | ||
let(:versioned_lookup) { false } # default, override when desired | ||
# must explicitly define :resource | ||
let(:service) { described_class.new(resource, versioned_lookup: versioned_lookup) } | ||
|
||
# requires defining lookup_id, url | ||
shared_examples "url examples" do | ||
context "without a lookup_id value" do | ||
let(:file_set) { empty_file_set } | ||
it "returns nil" do | ||
expect(lookup_id).to be_nil | ||
expect(url).to be_nil | ||
end | ||
end | ||
context "with an original_file_id lookup_id value" do | ||
let(:file_set) { local_file_set } | ||
it "returns a url" do | ||
expect(lookup_id).not_to match /^s3/ | ||
expect(url).to match /^http/ | ||
end | ||
end | ||
context "with a content_location lookup_id value" do | ||
let(:file_set) { remote_file_set } | ||
it "returns a url" do | ||
expect(lookup_id).to match /^s3/ | ||
expect(url).to match /^http/ | ||
end | ||
end | ||
context "with versioned_file_id lookup" do | ||
let(:file_set) { empty_file_set } | ||
let(:versioned_lookup) { true } | ||
context "that fails" do | ||
it "returns nil" do | ||
expect(lookup_id).to be_nil | ||
expect(url).to be_nil | ||
end | ||
end | ||
context "that succeeds" do | ||
before do | ||
allow(service).to receive(:original_file).and_return(local_file_set.original_file) | ||
end | ||
it "returns a url" do | ||
expect(lookup_id).not_to match /^s3/ | ||
expect(url).to match /^http/ | ||
# below is proxy check that #versioned_file_id was called | ||
expect(service.instance_variable_get(:@versioned_file_id)).not_to be_nil | ||
end | ||
end | ||
end | ||
end | ||
|
||
# requires defining resource | ||
shared_examples "#iiif_(image|info)_url examples" do | ||
let(:lookup_id) { service.lookup_id } | ||
describe "#iiif_image_url" do | ||
let(:url) { service.iiif_image_url } | ||
include_examples "url examples" | ||
end | ||
describe "#iiif_info_url" do | ||
let(:url) { service.iiif_info_url(base_url) } | ||
include_examples "url examples" | ||
end | ||
end | ||
|
||
context "for an ActiveFedora::SolrHit" do | ||
let(:resource) { solr_hit } | ||
include_examples "#iiif_(image|info)_url examples" | ||
end | ||
|
||
context "for a FileSet" do | ||
context "directly" do | ||
let(:resource) { file_set } | ||
include_examples "#iiif_(image|info)_url examples" | ||
end | ||
context "sourcing a Hyrax::FileSetPresenter" do | ||
let(:source) { file_set } | ||
let(:resource) { file_set_presenter } | ||
include_examples "#iiif_(image|info)_url examples" | ||
end | ||
end | ||
|
||
context "for a SolrDocument" do | ||
context "directly" do | ||
let(:resource) { solr_document } | ||
include_examples "#iiif_(image|info)_url examples" | ||
end | ||
context "sourcing a Hyrax::FileSetPresenter" do | ||
let(:source) { solr_document } | ||
let(:resource) { file_set_presenter } | ||
include_examples "#iiif_(image|info)_url examples" | ||
end | ||
end | ||
|
||
context "for a Hash" do | ||
let(:resource) { solr_document.to_h } | ||
include_examples "#iiif_(image|info)_url examples" | ||
end | ||
end |