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



88
89
90
91
92
# File 'lib/wax_tasks/collection/images.rb', line 88

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/wax_tasks/collection/images.rb', line 96

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.dig k
      record.set k, "/#{Utils.content_clean(value)}" unless value.nil?
    end

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

#iiif_builder(dir) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/wax_tasks/collection/images.rb', line 77

def iiif_builder(dir)
  build_opts = {
    base_url: "{{ '/' | absolute_url }}#{dir}",
    output_dir: dir,
    collection_label: @name
  }
  WaxIiif::Builder.new(build_opts)
end

#image_variantsObject



15
16
17
18
19
# File 'lib/wax_tasks/collection/images.rb', line 15

def image_variants
  default_variants = { 'thumbnail' => 250, 'full' => 1140 }
  custom_variants  = @config.dig('images', 'variants') || {}
  default_variants.merge custom_variants
end

#items_from_imagedataObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wax_tasks/collection/images.rb', line 23

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)
    if 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
    else
      puts Rainbow("Skipping #{path} because type #{item.type} is not an accepted format").yellow unless item.type == '.pdf'
    end
  end.compact
end

#pre_process_pdfsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/wax_tasks/collection/images.rb', line 43

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_derivatives(dir) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/wax_tasks/collection/images.rb', line 116

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

  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 dir
  add_iiif_results_to_records records, builder.manifests
end

#write_simple_derivatives(dir) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wax_tasks/collection/images.rb', line 56

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

  bar = ProgressBar.new(items_from_imagedata.length)
  items_from_imagedata.map do |item|
    item.simple_derivatives.each do |d|
      path = "#{dir}/#{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