-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6368 from samvera/versioned_riiif
Use a hashed version identifier in IIIF manifest
- Loading branch information
Showing
17 changed files
with
174 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
# Adds file locking to Riiif::File | ||
# @see RiiifFileResolver | ||
class RiiifFile < Riiif::File | ||
include ActiveSupport::Benchmarkable | ||
|
||
attr_reader :id | ||
def initialize(input_path, tempfile = nil, id:) | ||
super(input_path, tempfile) | ||
raise(ArgumentError, "must specify id") if id.blank? | ||
@id = id | ||
end | ||
|
||
# Wrap extract in a read lock and benchmark it | ||
def extract(transformation, image_info = nil) | ||
Riiif::Image.file_resolver.file_locks[id].with_read_lock do | ||
benchmark "RiiifFile extracted #{path} with #{transformation.to_params}", level: :debug do | ||
super | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def logger | ||
Hyrax.logger | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
# Riiif file resolver for valkyrie resources | ||
class RiiifFileResolver | ||
include ActiveSupport::Benchmarkable | ||
|
||
# @param [String] id from iiif manifest | ||
# @return [Riiif::File] | ||
def find(id) | ||
path = nil | ||
file_locks[id].with_write_lock do | ||
path = build_path(id) | ||
path = build_path(id, force: true) unless File.exist?(path) # Ensures the file is locally available | ||
end | ||
RiiifFile.new(path, id: id) | ||
end | ||
|
||
# tracks individual file locks | ||
# @see RiiifFile | ||
# @return [Concurrent::Map<Concurrent::ReadWriteLock>] | ||
def file_locks | ||
@file_locks ||= Concurrent::Map.new do |k, v| | ||
k.compute_if_absent(v) { Concurrent::ReadWriteLock.new } | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_path(id, force: false) | ||
Riiif::Image.cache.fetch("riiif:" + Digest::MD5.hexdigest("path:#{id}"), | ||
expires_in: Riiif::Image.expires_in, | ||
force: force) do | ||
load_file(id) | ||
end | ||
end | ||
|
||
def load_file(id) | ||
benchmark "RiiifFileResolver loaded #{id}", level: :debug do | ||
fs_id = id.sub(/\A([^\/]*)\/.*/, '\1') | ||
file_set = Hyrax.query_service.find_by(id: fs_id) | ||
file_metadata = Hyrax.custom_queries.find_original_file(file_set: file_set) | ||
file_metadata.file.disk_path.to_s # Stores a local copy in tmpdir | ||
end | ||
end | ||
|
||
def logger | ||
Hyrax.logger | ||
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
32 changes: 13 additions & 19 deletions
32
lib/generators/hyrax/templates/config/initializers/riiif.rb
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 |
---|---|---|
@@ -1,39 +1,33 @@ | ||
# frozen_string_literal: true | ||
ActiveSupport::Reloader.to_prepare do | ||
Riiif::Image.file_resolver = Riiif::HttpFileResolver.new | ||
Rails.application.reloader.to_prepare do | ||
Riiif::Image.info_service = lambda do |id, _file| | ||
# id will look like a path to a pcdm:file | ||
# (e.g. rv042t299%2Ffiles%2F6d71677a-4f80-42f1-ae58-ed1063fd79c7) | ||
# but we just want the id for the FileSet it's attached to. | ||
|
||
# Capture everything before the first slash | ||
fs_id = id.sub(/\A([^\/]*)\/.*/, '\1') | ||
resp = Hyrax::SolrService.get("id:#{fs_id}") | ||
doc = resp['response']['docs'].first | ||
raise "Unable to find solr document with id:#{fs_id}" unless doc | ||
{ height: doc['height_is'], width: doc['width_is'], format: doc['mime_type_ssi'], channels: doc['alpha_channels_ssi'] } | ||
end | ||
|
||
Riiif::Image.file_resolver.id_to_uri = if Hyrax.config.use_valkyrie? | ||
# Use Valkyrie adapter to make sure file is available locally. Riiif will just open it then | ||
# id comes in with the format "FILE_SET_ID/files/FILE_ID" | ||
lambda do |id| | ||
file_metadata = Hyrax.query_service.find_by(id: id.split('/').last) | ||
file = Hyrax.storage_adapter.find_by(id: file_metadata.file_identifier) | ||
file.disk_path.to_s | ||
end | ||
else | ||
lambda do |id| | ||
Hyrax::Base.id_to_uri(CGI.unescape(id)).tap do |url| | ||
Rails.logger.info "Riiif resolved #{id} to #{url}" | ||
end | ||
end | ||
end | ||
if Hyrax.config.use_valkyrie? | ||
Riiif::Image.file_resolver = Hyrax::RiiifFileResolver.new | ||
else | ||
Riiif::Image.file_resolver = Riiif::HttpFileResolver.new | ||
|
||
Riiif::Image.file_resolver.id_to_uri = lambda do |id| | ||
Hyrax::Base.id_to_uri(CGI.unescape(id)).tap do |url| | ||
Rails.logger.info "Riiif resolved #{id} to #{url}" | ||
end | ||
end | ||
end | ||
|
||
Riiif::Image.authorization_service = Hyrax::IiifAuthorizationService | ||
|
||
Riiif.not_found_image = Rails.root.join('app', 'assets', 'images', 'us_404.svg') | ||
Riiif.unauthorized_image = Rails.root.join('app', 'assets', 'images', 'us_404.svg') | ||
|
||
Riiif::Engine.config.cache_duration = 365.days | ||
Riiif::Engine.config.cache_duration = 1.month | ||
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
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,25 @@ | ||
# frozen_string_literal: true | ||
require 'spec_helper' | ||
|
||
RSpec.describe Hyrax::RiiifFileResolver do | ||
let(:resolver) { described_class.new } | ||
|
||
context 'with a file' do | ||
let(:file_metadata) { FactoryBot.valkyrie_create(:hyrax_file_metadata, :with_file) } | ||
let(:file_set) { Hyrax.query_service.find_by(id: file_metadata.file_set_id) } | ||
|
||
describe '#find' do | ||
it 'returns a locally available RiiifFile using a write lock' do | ||
expect(resolver.file_locks[file_set.iiif_id]).to receive(:with_write_lock).and_call_original | ||
expect(resolver.find(file_set.iiif_id)).to be_instance_of Hyrax::RiiifFile | ||
expect(File.exist?(file_metadata.file.disk_path)).to eq true | ||
end | ||
end | ||
end | ||
|
||
describe '#file_locks' do | ||
it 'is a Concurrent::Map of Concurrent::ReadWriteLocks' do | ||
expect(resolver.file_locks[SecureRandom.uuid]).to be_instance_of Concurrent::ReadWriteLock | ||
end | ||
end | ||
end |
Oops, something went wrong.