Skip to content

Commit

Permalink
Fixes #3422 - support images with alpha channels in Universal Viewer;…
Browse files Browse the repository at this point in the history
… also bumps the circle ci cache_key version to rebuild the test app
  • Loading branch information
Julie Allinson committed Jun 6, 2019
1 parent 237e82b commit 3b1676f
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
- attach_workspace:
at: ~/
- samvera/engine_cart_generate:
cache_key: v1-internal-test-app-{{ checksum "hyrax.gemspec" }}-{{ checksum "spec/test_app_templates/lib/generators/test_app_generator.rb" }}-{{ checksum "lib/generators/hyrax/install_generator.rb" }}-<< parameters.rails_version >>-<< parameters.ruby_version >>
cache_key: v2-internal-test-app-{{ checksum "hyrax.gemspec" }}-{{ checksum "spec/test_app_templates/lib/generators/test_app_generator.rb" }}-{{ checksum "lib/generators/hyrax/install_generator.rb" }}-<< parameters.rails_version >>-<< parameters.ruby_version >>
- samvera/bundle_for_gem:
ruby_version: << parameters.ruby_version >>
bundler_version: << parameters.bundler_version >>
Expand Down
1 change: 1 addition & 0 deletions app/indexers/hyrax/file_set_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def generate_solr_document
solr_doc['duration_tesim'] = object.duration
solr_doc['sample_rate_tesim'] = object.sample_rate
solr_doc['original_checksum_tesim'] = object.original_checksum
solr_doc['alpha_channels_ssi'] = object.alpha_channels
end
end

Expand Down
25 changes: 20 additions & 5 deletions app/jobs/characterize_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ class CharacterizeJob < Hyrax::ApplicationJob
def perform(file_set, file_id, filepath = nil)
raise "#{file_set.class.characterization_proxy} was not found for FileSet #{file_set.id}" unless file_set.characterization_proxy?
filepath = Hyrax::WorkingDirectory.find_or_retrieve(file_id, file_set.id) unless filepath && File.exist?(filepath)
Hydra::Works::CharacterizationService.run(file_set.characterization_proxy, filepath)
Rails.logger.debug "Ran characterization on #{file_set.characterization_proxy.id} (#{file_set.characterization_proxy.mime_type})"
file_set.characterization_proxy.save!
file_set.update_index
file_set.parent&.in_collections&.each(&:update_index)
characterize(file_set, file_id, filepath)
CreateDerivativesJob.perform_later(file_set, file_id, filepath)
end

private

def characterize(file_set, _file_id, filepath)
Hydra::Works::CharacterizationService.run(file_set.characterization_proxy, filepath)
Rails.logger.debug "Ran characterization on #{file_set.characterization_proxy.id} (#{file_set.characterization_proxy.mime_type})"
file_set.characterization_proxy.alpha_channels = channels(filepath) if file_set.image? && Hyrax.config.iiif_image_server?
file_set.characterization_proxy.save!
file_set.update_index
file_set.parent&.in_collections&.each(&:update_index)
end

def channels(filepath)
ch = Riiif::ImageMagickInfoExtractor.new(filepath).extract[:channels]
raise StandardError if ch.nil?
[Riiif::ImageMagickInfoExtractor.new(filepath).extract[:channels]]
rescue StandardError
[]
end
end
9 changes: 8 additions & 1 deletion app/models/concerns/hyrax/file_set/characterization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Characterization
self.characterization_terms = [
:format_label, :file_size, :height, :width, :filename, :well_formed,
:page_count, :file_title, :last_modified, :original_checksum,
:duration, :sample_rate
:duration, :sample_rate, :alpha_channels
]
self.characterization_proxy = :original_file

Expand Down Expand Up @@ -50,6 +50,13 @@ def respond_to_missing?(_method_name, _include_private = false)

def mime_type; end
end

# Add Alpha Channels to the Schema
class AlphaChannelsSchema < ActiveTriples::Schema
property :alpha_channels, predicate: ::RDF::URI.new('http://vocabulary.samvera.org/ns#alphaChannels')
end

ActiveFedora::WithMetadata::DefaultMetadataClassFactory.file_metadata_schemas << AlphaChannelsSchema
end
end
end
4 changes: 4 additions & 0 deletions app/models/concerns/hyrax/solr_document/characterization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def last_modified
def original_checksum
self[ActiveFedora.index_field_mapper.solr_name("original_checksum")]
end

def alpha_channels
self[ActiveFedora.index_field_mapper.solr_name("alpha_channels")]
end
end
end
end
2 changes: 1 addition & 1 deletion app/presenters/hyrax/characterization_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def characterization_terms
:capture_device, :scanning_software, :gps_timestamp, :latitude, :longitude,
:file_format, :file_title, :page_count, :duration, :sample_rate,
:format_label, :file_size, :filename, :well_formed, :last_modified,
:original_checksum, :mime_type
:original_checksum, :mime_type, :alpha_channels
]
end
end
Expand Down
8 changes: 7 additions & 1 deletion app/presenters/hyrax/displays_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ def display_image
url = Hyrax.config.iiif_image_url_builder.call(
original_file.id,
request.base_url,
Hyrax.config.iiif_image_size_default
Hyrax.config.iiif_image_size_default,
format: image_format(original_file.alpha_channels)
)
# @see https://github.com/samvera-labs/iiif_manifest
IIIFManifest::DisplayImage.new(url,
width: 640,
height: 480,
format: image_format(original_file.alpha_channels),
iiif_endpoint: iiif_endpoint(original_file.id))
end

Expand All @@ -35,5 +37,9 @@ def iiif_endpoint(file_id)
profile: Hyrax.config.iiif_image_compliance_level_uri
)
end

def image_format(channels)
channels.find { |c| c.include?('rgba') }.nil? ? 'jpg' : 'png'
end
end
end
4 changes: 2 additions & 2 deletions lib/generators/hyrax/riiif_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def banner
end

def add_to_gemfile
gem 'riiif', '~> 2.0'
gem 'riiif', '~> 2.1'

Bundler.with_clean_env do
run "bundle install"
Expand All @@ -33,7 +33,7 @@ def mount_route

def override_image_url_builder_in_hyrax_config
insert_into_file 'config/initializers/hyrax.rb', before: /^ # config.iiif_image_url_builder/ do
" config.iiif_image_url_builder = lambda do |file_id, base_url, size|\n" \
" config.iiif_image_url_builder = lambda do |file_id, base_url, size, format|\n" \
" Riiif::Engine.routes.url_helpers.image_url(file_id, host: base_url, size: size)\n" \
" end\n"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
# config.iiif_image_server = false

# Returns a URL that resolves to an image provided by a IIIF image server
# config.iiif_image_url_builder = lambda do |file_id, base_url, size|
# config.iiif_image_url_builder = lambda do |file_id, base_url, size, format|
# "#{base_url}/downloads/#{file_id.split('/').first}"
# end

Expand Down
4 changes: 2 additions & 2 deletions lib/generators/hyrax/templates/config/initializers/riiif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
resp = ActiveFedora::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'] }
{ 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 = lambda do |id|
Expand All @@ -23,4 +23,4 @@
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_in_days = 365
Riiif::Engine.config.cache_duration = 365.days
2 changes: 1 addition & 1 deletion lib/hyrax/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def iiif_image_server?
#
# @return [#call] lambda/proc that generates a URL to an image
def iiif_image_url_builder
@iiif_image_url_builder ||= ->(file_id, base_url, _size) { "#{base_url}/downloads/#{file_id.split('/').first}" }
@iiif_image_url_builder ||= ->(file_id, base_url, _size, _format) { "#{base_url}/downloads/#{file_id.split('/').first}" }
end
attr_writer :iiif_image_url_builder

Expand Down
1 change: 1 addition & 0 deletions spec/models/file_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
expect(subject).to respond_to(:sample_rate)
# :creator is characterization metadata?
expect(subject).to respond_to(:creator)
expect(subject).to respond_to(:alpha_channels)
end

it 'redefines to_param to make redis keys more recognizable' do
Expand Down
4 changes: 2 additions & 2 deletions spec/presenters/hyrax/file_set_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"subject", "language", "license", "format_label", "file_size",
"height", "width", "filename", "well_formed", "page_count",
"file_title", "last_modified", "original_checksum", "mime_type",
"duration", "sample_rate"]
"duration", "sample_rate", "alpha_channels"]
end

it "delegates to the solr_document" do
Expand Down Expand Up @@ -364,7 +364,7 @@
context 'with custom image url builder' do
let(:id) { file_set.original_file.id }
let(:custom_builder) do
->(file_id, base_url, _size) { "#{base_url}/downloads/#{file_id.split('/').first}" }
->(file_id, base_url, _size, _format) { "#{base_url}/downloads/#{file_id.split('/').first}" }
end

around do |example|
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/riiif_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
context "when the user is authorized" do
it "returns an image" do
login_as user
get Riiif::Engine.routes.url_helpers.image_path(file.id, size: size)
get Riiif::Engine.routes.url_helpers.image_path(file.id, size: size, format: 'jpg', channels: nil)
expect(response).to have_http_status(:ok)
expect(response.content_type).to eq 'image/jpeg'
end
end

context "when the user is not authorized" do
it "returns an image" do
get Riiif::Engine.routes.url_helpers.image_path(file.id, size: size)
get Riiif::Engine.routes.url_helpers.image_path(file.id, size: size, format: 'jpg', channels: nil)
expect(response).to have_http_status(:unauthorized)
expect(response.content_type).to eq 'image/jpeg'
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/factory_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def mock_file_factory(opts = {})
file_title: opts.fetch(:file_title, []),
last_modified: opts.fetch(:last_modified, []),
original_checksum: opts.fetch(:original_checksum, []),
alpha_channels: opts.fetch(:alpha_channels, []),
digest: opts.fetch(:digest, []),
duration: opts.fetch(:duration, []),
sample_rate: opts.fetch(:sample_rate, []))
Expand Down

0 comments on commit 3b1676f

Please sign in to comment.