From aab7d3912aa57dd1e0e862bfc5d179869720badf Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Tue, 28 Nov 2023 18:20:15 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=81=20Only=20run=20thumbnail=20derivat?= =?UTF-8?q?ives=20on=20thumbnails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We were seeing that our thumbnail files were getting word coordinates derivatives, which is a waste of processing time. The jobs would eventually fail but ideally we don't want them to run at all for thumbnails. This commit will upgrade to the latest version of IIIF Print where a new configurable class attribute has been added to enable a more granular control over which derivatives are run on what files. Also in this commit, we were seeing a nilClass error for a logger in development, so that has also been fixed. Ref: - https://github.com/scientist-softserv/adventist-dl/issues/676 - https://github.com/scientist-softserv/iiif_print/pull/306 --- Gemfile.lock | 3 +- config/application.rb | 9 +++++ config/initializers/iiif_print.rb | 2 ++ .../canvas_builder_factory_decorator.rb | 4 +-- spec/config/application_spec.rb | 36 +++++++++++++++++++ 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 spec/config/application_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index a09ae49f..afc62766 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,7 +31,7 @@ GIT GIT remote: https://github.com/scientist-softserv/iiif_print.git - revision: e42cfd2a6d9b0aac9901084625ee1e1a0a3ccb6d + revision: 5c5fa6880b3dc22beed738cfcba7af57b698ac36 branch: main specs: iiif_print (1.0.0) @@ -39,7 +39,6 @@ GIT derivative-rodeo (~> 0.5) hyrax (>= 2.5, < 6) nokogiri (>= 1.13.2) - rails (>= 5.0, < 8.0) rdf-vocab (~> 3.0) GIT diff --git a/config/application.rb b/config/application.rb index 8d8292bc..4b92069b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -96,6 +96,13 @@ class Application < Rails::Application Rails.configuration.cache_classes ? require(c) : load(c) end + # See: https://github.com/scientist-softserv/adventist-dl/issues/676 + IiifPrint::DerivativeRodeoService.named_derivatives_and_generators_filter = + ->(file_set:, filename:, named_derivatives_and_generators:) do + named_derivatives_and_generators.reject do |named_derivative, generators| + named_derivative != :thumbnail && filename.downcase.ends_with?(Hyku::THUMBNAIL_FILE_SUFFIX) + end + end end # resolve reloading issue in dev mode @@ -121,4 +128,6 @@ class Application < Rails::Application # copies tinymce assets directly into public/assets config.tinymce.install = :copy end + + THUMBNAIL_FILE_SUFFIX = '.tn.jpg' end diff --git a/config/initializers/iiif_print.rb b/config/initializers/iiif_print.rb index 1bc8fa90..23f3d9a9 100644 --- a/config/initializers/iiif_print.rb +++ b/config/initializers/iiif_print.rb @@ -127,4 +127,6 @@ if ENV.fetch('LOCAL_RODEO_LOG', 'false') == 'true' FileUtils.mkdir_p(Rails.root.join('log').to_s) DerivativeRodeo.config.logger = Logger.new(Rails.root.join("log/dr.log").to_s, level: Logger::DEBUG) +else + DerivativeRodeo.config.logger = Rails.logger end diff --git a/lib/iiif_manifest/manifest_builder/canvas_builder_factory_decorator.rb b/lib/iiif_manifest/manifest_builder/canvas_builder_factory_decorator.rb index 6fc66425..ff3a68de 100644 --- a/lib/iiif_manifest/manifest_builder/canvas_builder_factory_decorator.rb +++ b/lib/iiif_manifest/manifest_builder/canvas_builder_factory_decorator.rb @@ -5,12 +5,10 @@ module IIIFManifest module ManifestBuilderDecorator module CanvasBuilderFactoryDecorator - THUMBNAIL_FILE_SUFFIX = '.tn.jpg' - def from(work) composite_builder.new( *file_set_presenters(work).map do |presenter| - next if presenter.label.downcase.end_with?(THUMBNAIL_FILE_SUFFIX) || !presenter.image? + next if presenter.label.downcase.end_with?(Hyku::THUMBNAIL_FILE_SUFFIX) || !presenter.image? canvas_builder_factory.new(presenter, work) end ) diff --git a/spec/config/application_spec.rb b/spec/config/application_spec.rb new file mode 100644 index 00000000..ac907905 --- /dev/null +++ b/spec/config/application_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# rubocop:disable RSpec/DescribeClass +RSpec.describe 'config/application.rb' do + # rubocop:enable RSpec/DescribeClass + describe 'IiifPrint::DerivativeRodeoService.named_derivatives_and_generators_filter' do + subject do + IiifPrint::DerivativeRodeoService + .named_derivatives_and_generators_filter + .call(file_set: file_set, + filename: filename, + named_derivatives_and_generators: named_derivatives_and_generators).keys + end + + let(:file_set) { double('file_set') } + let(:named_derivatives_and_generators) do + IiifPrint::DerivativeRodeoService.named_derivatives_and_generators_by_type.fetch(:image).deep_dup + end + + context 'for a thumbnail file' do + let(:filename) { 'image.TN.jpg' } + + it 'will only generate a thumbnail' do + expect(subject).to eq [:thumbnail] + end + end + + context 'for a non-thumbnail file' do + let(:filename) { 'image.jpg' } + + it 'will generate all derivatives' do + expect(subject).to eq %i[thumbnail json xml txt] + end + end + end +end