Class: IiifPrint::BaseDerivativeService

Inherits:
Object
  • Object
show all
Defined in:
lib/iiif_print/base_derivative_service.rb

Overview

Base type for IiifPrint derivative services

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_set) ⇒ BaseDerivativeService

Returns a new instance of BaseDerivativeService.



9
10
11
12
13
14
# File 'lib/iiif_print/base_derivative_service.rb', line 9

def initialize(file_set)
  @file_set = file_set
  @dest_path = nil
  @source_path = nil
  @source_meta = nil
end

Instance Attribute Details

#file_setObject (readonly)

Returns the value of attribute file_set.



4
5
6
# File 'lib/iiif_print/base_derivative_service.rb', line 4

def file_set
  @file_set
end

#master_formatObject (readonly)

Returns the value of attribute master_format.



4
5
6
# File 'lib/iiif_print/base_derivative_service.rb', line 4

def master_format
  @master_format
end

Instance Method Details

#cleanup_derivatives(extension = target_extension, *_args) ⇒ Object



79
80
81
82
83
# File 'lib/iiif_print/base_derivative_service.rb', line 79

def cleanup_derivatives(extension = target_extension, *_args)
  derivative_path_factory.derivatives_for_reference(file_set).each do |path|
    FileUtils.rm_f(path) if path.ends_with?(extension)
  end
end

#convert_cmdObject

Raises:

  • (NotImplementedError)


93
94
95
# File 'lib/iiif_print/base_derivative_service.rb', line 93

def convert_cmd
  raise NotImplementedError, 'Calling subclass missing convert_cmd method'
end

#create_derivatives(filename) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/iiif_print/base_derivative_service.rb', line 70

def create_derivatives(filename)
  # presuming that filename is full path to source file
  @source_path = filename

  # Get destination path from Hyrax for file extension defined in
  #   self.target_extension constant on respective derivative service subclass.
  load_destpath
end

#derivative_path_factoryObject



31
32
33
# File 'lib/iiif_print/base_derivative_service.rb', line 31

def derivative_path_factory
  Hyrax::DerivativePath
end

#identifyObject



52
53
54
55
# File 'lib/iiif_print/base_derivative_service.rb', line 52

def identify
  return @source_meta unless @source_meta.nil?
  @source_meta = IiifPrint::ImageTool.new(@source_path).
end

#im_convertObject

convert non-JP2 source/primary file to PDF derivative with ImageMagick6

calls convert_cmd on calling subclasses


99
100
101
# File 'lib/iiif_print/base_derivative_service.rb', line 99

def im_convert
  `#{convert_cmd}`
end

#jp2_convertObject

convert JP2 source/primary file to PDF derivative, via

opj_decompress to intermediate TIFF, then ImageMagick6 convert


105
106
107
108
109
110
111
# File 'lib/iiif_print/base_derivative_service.rb', line 105

def jp2_convert
  # jp2 source -> intermediate
  intermediate_path = jp2_to_intermediate
  @source_path = intermediate_path
  # intermediate -> PDF
  im_convert
end

#jp2_to_intermediateObject



85
86
87
88
89
90
91
# File 'lib/iiif_print/base_derivative_service.rb', line 85

def jp2_to_intermediate
  intermediate_path = File.join(Dir.mktmpdir, 'intermediate.tif')
  jp2_cmd = "opj_decompress -i #{@source_path} -o #{intermediate_path}"
  # make intermediate, then...
  `#{jp2_cmd}`
  intermediate_path
end

#load_destpathObject

calculate and ensure directory components for singular @dest_path

should only be used by subclasses producing a single derivative


48
49
50
# File 'lib/iiif_print/base_derivative_service.rb', line 48

def load_destpath
  @dest_path = prepare_path(target_extension)
end

#mime_typeObject



57
58
59
# File 'lib/iiif_print/base_derivative_service.rb', line 57

def mime_type
  identify[:content_type]
end

#one_bit?Boolean

is source one-bit monochrome?

Returns:

  • (Boolean)


66
67
68
# File 'lib/iiif_print/base_derivative_service.rb', line 66

def one_bit?
  identify[:color] == 'monochrome'
end

#prepare_path(extension) ⇒ Object

prepare full path for passed extension/destination name, return path



36
37
38
39
40
41
42
43
44
# File 'lib/iiif_print/base_derivative_service.rb', line 36

def prepare_path(extension)
  dest_path = derivative_path_factory.derivative_path_for_reference(
    @file_set,
    extension
  )
  dir = File.join(dest_path.split('/')[0..-2])
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  dest_path
end

#use_color?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/iiif_print/base_derivative_service.rb', line 61

def use_color?
  identify[:color] == 'color'
end

#valid?Boolean

We assume that for the file set’s parent that this is an acceptable plugin. Now, we ask for this specific file_set is it valid. For example, we would not attempt to extract text from a movie even though the parent work says to attempt to extract text on any attached file sets. Put another way, we can upload a PDF or a Movie to the parent.

In subclass, you’ll want to consider the attributes of the file_set and whether that subclass should process the given file_set.

Returns:

  • (Boolean)

See Also:

  • PluggableDerivativeService#plugins_for


27
28
29
# File 'lib/iiif_print/base_derivative_service.rb', line 27

def valid?
  true
end