Class: IiifPrint::TextFormatsFromALTOService
Overview
Plugin to make text format derviatives (JSON, plain-text) from ALTO,
either existing derivative, or an impending attachment.
NOTE: to keep this from conflicting with ,
this class should be invoked by it, not PluggableDerivativeService.
Instance Attribute Summary
#file_set, #master_format
Instance Method Summary
collapse
#convert_cmd, #derivative_path_factory, #identify, #im_convert, #initialize, #jp2_convert, #jp2_to_intermediate, #load_destpath, #mime_type, #mime_type_for, #one_bit?, #prepare_path, #use_color?, #valid?
Instance Method Details
#alto ⇒ Object
51
52
53
54
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 51
def alto
path = alto_path
File.read(path, encoding: 'UTF-8') unless path.nil?
end
|
#alto_path ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 41
def alto_path
path = derivative_path_factory.derivative_path_for_reference(
@file_set,
'xml'
)
return path if nonempty_file?(path)
incoming_alto_path
end
|
#cleanup_derivatives(*args) ⇒ Object
74
75
76
77
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 74
def cleanup_derivatives(*args)
end
|
#create_derivatives(_filename) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 56
def create_derivatives(_filename)
source_file = alto
return if source_file.nil?
file = @file_set.original_file
width = file.nil? ? nil : file.width[0].to_i
height = file.nil? ? nil : file.height[0].to_i
reader = IiifPrint::::AltoReader.new(
source_file,
width,
height
)
save_derivative('json', reader.json)
save_derivative('txt', reader.text)
end
|
#incoming_alto_path ⇒ Object
if there was no derivative yet, there might be one in-transit from
an ingest, so check for that, and use its source if applicable:
33
34
35
36
37
38
39
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 33
def incoming_alto_path
path = IiifPrint::DerivativeAttachment.where(
fileset_id: @file_set.id,
destination_name: 'xml'
).pluck(:path).uniq.first
path if nonempty_file?(path)
end
|
#nonempty_file?(path) ⇒ Boolean
25
26
27
28
29
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 25
def nonempty_file?(path)
return false if path.nil?
return false unless File.exist?(path)
!File.size(path).zero?
end
|
#save_derivative(destination, data) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/iiif_print/text_formats_from_alto_service.rb', line 9
def save_derivative(destination, data)
mime_type = mime_type_for(destination)
prepare_path(destination)
save_path = derivative_path_factory.derivative_path_for_reference(
@file_set,
destination
)
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
|