Skip to content

Commit

Permalink
WIP addressing derivatives
Browse files Browse the repository at this point in the history
Creation of derivatives should be complete. However
accessing the derivatives is not correct.
  • Loading branch information
LaRita Robinson committed May 9, 2024
1 parent cee9752 commit 7bc1ec4
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 56 deletions.
6 changes: 5 additions & 1 deletion app/services/iiif_print/pluggable_derivative_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class IiifPrint::PluggableDerivativeService
class_attribute :derivative_path_factory, default: Hyrax::DerivativePath

def initialize(file_set, plugins: plugins_for(file_set))
@file_set = file_set
@file_set = if file_set.is_a?(Hyrax::FileMetadata)
Hyrax.query_service.find_by(id: file_set.file_set_id)
else
file_set
end
@plugins = Array.wrap(plugins)
@valid_plugins = plugins.map { |plugin| plugin.new(file_set) }.select(&:valid?)
end
Expand Down
46 changes: 0 additions & 46 deletions app/views/hyrax/file_sets/_actions.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions lib/iiif_print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class << self

delegate(
:clean_for_tests!,
:copy_derivatives_from_data_store,
:create_relationship_between,
:destroy_children_split_from,
:find_by,
Expand Down
15 changes: 13 additions & 2 deletions lib/iiif_print/base_derivative_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class BaseDerivativeService
class_attribute :target_extension, default: nil

def initialize(file_set)
@file_set = file_set
@file_set = if file_set.is_a?(Hyrax::FileMetadata)
Hyrax.query_service.find_by(id: file_set.file_set_id)
else
file_set
end
@dest_path = nil
@source_path = nil
@source_meta = nil
Expand All @@ -26,7 +30,10 @@ def initialize(file_set)
# @return [Boolean]
def valid?
# @note We are taking a shortcut because currently we are only concerned about images.
file_set.class.image_mime_types.include?(file_set.mime_type)
# @TODO: verify if this works for ActiveFedora and if so, remove commented code.
# If not, modify to use adapter.
# file_set.class.image_mime_types.include?(file_set.mime_type)
file_set.original_file.image?
end

def derivative_path_factory
Expand Down Expand Up @@ -110,5 +117,9 @@ def jp2_convert
# intermediate -> PDF
im_convert
end

def mime_type_for(extension)
Marcel::MimeType.for extension: extension
end
end
end
5 changes: 4 additions & 1 deletion lib/iiif_print/jp2_derivative_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def create_derivatives(filename)
render_cmd = opj_command

# Run the generated command to make derivative file at @dest_path
`#{render_cmd}`
data = `#{render_cmd}`

# Create Hyrax::FileMetadata object for the derivatives (if Valkyrie)
IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'service_file', mime_type: mime_type_for(self.target_extension) })

# Clean up any intermediate files or symlinks used during creation
cleanup_intermediate
Expand Down
3 changes: 2 additions & 1 deletion lib/iiif_print/pdf_derivative_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def initialize(file_set)
# JP2 source, and whether we have color or grayscale material.
def convert_cmd
template = use_color? ? COLOR_PDF_CMD : GRAY_PDF_CMD
format(template, source_file: @source_path, out_file: @dest_path)
data = format(template, source_file: @source_path, out_file: @dest_path)
IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'service_file', mime_type: mime_type_for(self.target_extension) })
end

def create_derivatives(filename)
Expand Down
6 changes: 5 additions & 1 deletion lib/iiif_print/persistence_layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ def self.save(object:)
raise NotImplementedError, "#{self}.{__method__}"
end

def index_works(objects:)
def self.index_works(objects:)
raise NotImplementedError, "#{self}.{__method__}"
end

def self.copy_derivatives_from_data_store(stream:, directives:)
raise NotImplementedError, "#{self}.{__method__}"
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/iiif_print/persistence_layer/active_fedora_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ def self.index_works(objects:)
end
true
end

##
# does nothing for ActiveFedora;
# allows valkyrie works to have an extra step to create the Hyrax::Metadata objects.
#
# @param []
# @return [TrueClass]
def self.copy_derivatives_from_data_store(stream:, directives:)
true
end
end
end
end
10 changes: 10 additions & 0 deletions lib/iiif_print/persistence_layer/valkyrie_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ def self.index_works(objects:)
end
true
end

##
# Performs an extra step to create the Hyrax::Metadata objects
# for derivatives.
#
# @param []
# @return [TrueClass]
def self.copy_derivatives_from_data_store(stream:, directives:)
Hyrax::ValkyriePersistDerivatives.call(stream, directives)
end
end
end
end
6 changes: 4 additions & 2 deletions lib/iiif_print/text_extraction_derivative_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ def create_derivatives_from_ocr(filename)

ocr_derivatives.each do |extension, method_name|
path = prepare_path(extension.to_s)
write(content: ocr.public_send(method_name), path: path)
write(content: ocr.public_send(method_name), path: path, extension: extension)
end
end

def write(content:, path:)
def write(content:, path:, extension:)
mime_type = mime_type_for(extension)
File.open(path, 'w') do |outfile|
outfile.write(content)
IiifPrint.copy_derivatives_from_data_store(stream: content, directives: { url: path, container: 'extracted_text', mime_type: mime_type })
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/iiif_print/text_formats_from_alto_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ module IiifPrint
# NOTE: to keep this from conflicting with TextExtractionDerivativeService,
# this class should be invoked by it, not PluggableDerivativeService.
class TextFormatsFromALTOService < BaseDerivativeService
self.target_extension = 'tiff'.freeze
self.target_extension = 'txt'.freeze

def save_derivative(destination, data)
mime_type = mime_type_for(destination)
# Load/prepare base of "pairtree" dir structure for extension, fileset
prepare_path(destination)
#
Expand All @@ -17,6 +18,7 @@ def save_derivative(destination, data)
# Write data as UTF-8 encoded text
File.open(save_path, "w:UTF-8") do |f|
f.write(data)
IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'extracted_text', mime_type: mime_type })
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/iiif_print/tiff_derivative_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def convert_cmd
source_path += '[0]' if @source_path.ends_with?('pdf')
template = use_color? ? COLOR_CMD : GRAY_CMD
template = MONO_CMD if one_bit?
format(template, source_file: source_path, out_file: @dest_path)
data = format(template, source_file: source_path, out_file: @dest_path)
IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'service_file', mime_type: mime_type_for(self.target_extension) })
end

def create_derivatives(filename)
Expand Down

0 comments on commit 7bc1ec4

Please sign in to comment.