Module: WaxTasks::Collection::Images

Included in:
WaxTasks::Collection
Defined in:
lib/wax_tasks/collection/images.rb

Instance Method Summary collapse

Instance Method Details

#add_font_matter_to_json_files(dir) ⇒ Object


82
83
84
85
86
# File 'lib/wax_tasks/collection/images.rb', line 82

def add_font_matter_to_json_files(dir)
  Dir.glob("#{dir}/**/*.json").each do |f|
    Utils.add_yaml_front_matter_to_file f
  end
end

#add_iiif_results_to_records(records, manifests) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wax_tasks/collection/images.rb', line 90

def add_iiif_results_to_records(records, manifests)
  records.map do |record|
    next nil if record.nil?

    manifest = manifests.find { |m| m.base_id == record.pid }
    next record if manifest.nil?

    json = JSON.parse manifest.to_json
    @image_variants.each do |k, _v|
      value = json.fetch k, ''
      record.set k, "/#{Utils.content_clean(value)}" unless value.empty?
    end

    record.set 'manifest', "/#{Utils.content_clean(manifest.id)}"
    record
  end.compact
end

#iiif_builder(dir) ⇒ Object


70
71
72
73
74
75
76
77
78
# File 'lib/wax_tasks/collection/images.rb', line 70

def iiif_builder(dir)
  build_opts = {
    base_url: "{{ '/' | absolute_url }}#{dir}",
    output_dir: dir,
    collection_label: @name,
    variants: @image_variants.dup.tap { |h| h.delete 'full' }
  }
  WaxIiif::Builder.new build_opts
end

#items_from_imagedataObject


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wax_tasks/collection/images.rb', line 15

def items_from_imagedata
  raise Error::MissingSource, "Cannot find image data source '#{@imagedata_source}'" unless Dir.exist? @imagedata_source

  pre_process_pdfs
  records = 
  Dir.glob(Utils.safe_join(@imagedata_source, '*')).map do |path|
    item = WaxTasks::Item.new(path, @image_variants)
    next if item.type == '.pdf'
    next puts Rainbow("Skipping #{path} because type #{item.type} is not an accepted format").yellow unless item.valid?

    item.record      = records.find { |r| r.pid == item.pid }
    item.iiif_config = @config.dig 'images', 'iiif'
    warn Rainbow("\nCould not find record in #{@metadata_source} for image item #{path}.\n").orange if item.record.nil?
    item
  end.compact
end

#pre_process_pdfsObject


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wax_tasks/collection/images.rb', line 34

def pre_process_pdfs
  Dir.glob(Utils.safe_join(@imagedata_source, '*.pdf')).each do |path|
    target_dir = path.gsub '.pdf', ''
    next unless Dir.glob("#{target_dir}/*").empty?

    puts Rainbow("\nPreprocessing #{path} into image files. This may take a minute.\n").cyan

    opts = { output_dir: File.dirname(target_dir) }
    WaxIiif::Utilities::PdfSplitter.split(path, opts)
  end
end

#write_iiif_derivativesObject


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/wax_tasks/collection/images.rb', line 110

def write_iiif_derivatives
  items     = items_from_imagedata
  iiif_data = items.map(&:iiif_image_records).flatten
  builder   = iiif_builder @iiif_derivative_source

  builder.load iiif_data

  puts Rainbow("Generating IIIF derivatives for collection '#{@name}'\nThis might take awhile.").cyan
  builder.process_data
  records = items.map(&:record).compact

  add_font_matter_to_json_files @iiif_derivative_source
  add_iiif_results_to_records records, builder.manifests
end

#write_simple_derivativesObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wax_tasks/collection/images.rb', line 48

def write_simple_derivatives
  puts Rainbow("Generating simple image derivatives for collection '#{@name}'\nThis might take awhile.").cyan

  bar = ProgressBar.new(items_from_imagedata.length)
  bar.write
  items_from_imagedata.map do |item|
    item.simple_derivatives.each do |d|
      path = "#{@simple_derivative_source}/#{d.path}"
      FileUtils.mkdir_p File.dirname(path)
      next if File.exist? path

      d.img.write path
      item.record.set d.label, "/#{path}" if item.record?
    end
    bar.increment!
    bar.write
    item
  end.flat_map(&:record).compact
end