From 72be6f15ac7eeff22666f4b27ad4c49ab730bfb9 Mon Sep 17 00:00:00 2001 From: Daniel Pierce Date: Fri, 1 Dec 2023 16:00:46 -0500 Subject: [PATCH] Move riiif dependent code to the generated riiif initializer --- .dassie/config/initializers/riiif.rb | 77 ++++++++++++++++++ .koppie/config/initializers/riiif.rb | 78 +++++++++++++++++++ app/models/concerns/hyrax/riiif_file.rb | 30 ------- app/services/hyrax/riiif_file_resolver.rb | 50 ------------ .../templates/config/initializers/riiif.rb | 77 ++++++++++++++++++ 5 files changed, 232 insertions(+), 80 deletions(-) delete mode 100644 app/models/concerns/hyrax/riiif_file.rb delete mode 100644 app/services/hyrax/riiif_file_resolver.rb diff --git a/.dassie/config/initializers/riiif.rb b/.dassie/config/initializers/riiif.rb index 3d32938c09..b675e5afa0 100644 --- a/.dassie/config/initializers/riiif.rb +++ b/.dassie/config/initializers/riiif.rb @@ -31,3 +31,80 @@ Riiif::Engine.config.cache_duration = 1.day end + +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 + + 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] + 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 diff --git a/.koppie/config/initializers/riiif.rb b/.koppie/config/initializers/riiif.rb index 3d32938c09..e4835610ea 100644 --- a/.koppie/config/initializers/riiif.rb +++ b/.koppie/config/initializers/riiif.rb @@ -31,3 +31,81 @@ Riiif::Engine.config.cache_duration = 1.day end + +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 + + 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] + 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 + diff --git a/app/models/concerns/hyrax/riiif_file.rb b/app/models/concerns/hyrax/riiif_file.rb deleted file mode 100644 index 5d8ddf0b9b..0000000000 --- a/app/models/concerns/hyrax/riiif_file.rb +++ /dev/null @@ -1,30 +0,0 @@ -# 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 diff --git a/app/services/hyrax/riiif_file_resolver.rb b/app/services/hyrax/riiif_file_resolver.rb deleted file mode 100644 index 8886bb7abc..0000000000 --- a/app/services/hyrax/riiif_file_resolver.rb +++ /dev/null @@ -1,50 +0,0 @@ -# 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] - 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 diff --git a/lib/generators/hyrax/templates/config/initializers/riiif.rb b/lib/generators/hyrax/templates/config/initializers/riiif.rb index e5d3f8dfdf..eace5cdad8 100644 --- a/lib/generators/hyrax/templates/config/initializers/riiif.rb +++ b/lib/generators/hyrax/templates/config/initializers/riiif.rb @@ -31,3 +31,80 @@ Riiif::Engine.config.cache_duration = 1.month end + +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 + + 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] + 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