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
15
16
17
18
# File 'lib/iiif_print/base_derivative_service.rb', line 9

def initialize(file_set)
  @file_set = if file_set.is_a?(Hyrax::)
                Hyrax.query_service.find_by(id: file_set.file_set_id)
              else
                file_set
              end
  @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



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

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)


101
102
103
# File 'lib/iiif_print/base_derivative_service.rb', line 101

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

#create_derivatives(filename) ⇒ Object



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

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



39
40
41
# File 'lib/iiif_print/base_derivative_service.rb', line 39

def derivative_path_factory
  Hyrax::DerivativePath
end

#identifyObject



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

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


107
108
109
# File 'lib/iiif_print/base_derivative_service.rb', line 107

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


113
114
115
116
117
118
119
# File 'lib/iiif_print/base_derivative_service.rb', line 113

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

#jp2_to_intermediateObject



93
94
95
96
97
98
99
# File 'lib/iiif_print/base_derivative_service.rb', line 93

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


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

def load_destpath
  @dest_path = prepare_path(target_extension)
end

#mime_typeObject



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

def mime_type
  identify[:content_type]
end

#mime_type_for(extension) ⇒ Object



121
122
123
# File 'lib/iiif_print/base_derivative_service.rb', line 121

def mime_type_for(extension)
  Marcel::MimeType.for extension: extension
end

#one_bit?Boolean

is source one-bit monochrome?

Returns:

  • (Boolean)


74
75
76
# File 'lib/iiif_print/base_derivative_service.rb', line 74

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

#prepare_path(extension) ⇒ Object

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



44
45
46
47
48
49
50
51
52
# File 'lib/iiif_print/base_derivative_service.rb', line 44

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)


69
70
71
# File 'lib/iiif_print/base_derivative_service.rb', line 69

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


31
32
33
34
35
36
37
# File 'lib/iiif_print/base_derivative_service.rb', line 31

def valid?
  # @note We are taking a shortcut because currently we are only concerned about images.
  # @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