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



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

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)


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

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

#create_derivatives(filename) ⇒ Object



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

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



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

def derivative_path_factory
  Hyrax::DerivativePath
end

#identifyObject



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

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


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

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


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

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

#jp2_to_intermediateObject



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

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


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

def load_destpath
  @dest_path = prepare_path(target_extension)
end

#mime_typeObject



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

def mime_type
  identify[:content_type]
end

#one_bit?Boolean

is source one-bit monochrome?

Returns:

  • (Boolean)


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

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

#prepare_path(extension) ⇒ Object

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



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

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)


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

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
30
# File 'lib/iiif_print/base_derivative_service.rb', line 27

def valid?
  # @note We are taking a shortcut because currently we are only concerned about images.
  file_set.class.image_mime_types.include?(file_set.mime_type)
end