Module: Assembly::ObjectFileable

Included in:
ObjectFile
Defined in:
lib/assembly-objectfile/object_fileable.rb

Overview

Common behaviors we need for other classes in the gem

Constant Summary collapse

VALID_MIMETYPE_METHODS =
i[override exif file extension].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_attributesObject

Returns the value of attribute file_attributes.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def file_attributes
  @file_attributes
end

#labelObject

Returns the value of attribute label.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def label
  @label
end

#mime_type_orderObject

Returns the value of attribute mime_type_order.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def mime_type_order
  @mime_type_order
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def path
  @path
end

#provider_md5Object

Returns the value of attribute provider_md5.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def provider_md5
  @provider_md5
end

#provider_sha1Object

Returns the value of attribute provider_sha1.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def provider_sha1
  @provider_sha1
end

#relative_pathObject

Returns the value of attribute relative_path.



9
10
11
# File 'lib/assembly-objectfile/object_fileable.rb', line 9

def relative_path
  @relative_path
end

Instance Method Details

#dirnameString

Returns base directory.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.dirname # "/input"

Returns:

  • (String)

    base directory



66
67
68
# File 'lib/assembly-objectfile/object_fileable.rb', line 66

def dirname
  File.dirname(path)
end

#dpg_basenameString

Returns DPG base filename, removing the extension and the ‘00’,‘05’, etc. placeholders.

Examples:

source_file = Assembly::ObjectFile.new('/input/cy565rm7188_00_001.tif')
puts source_file.dpg_basename # "cy565rm7188_001"

Returns:

  • (String)

    DPG base filename, removing the extension and the ‘00’,‘05’, etc. placeholders



40
41
42
43
# File 'lib/assembly-objectfile/object_fileable.rb', line 40

def dpg_basename
  file_parts = File.basename(path, ext).split('_')
  file_parts.size == 3 ? "#{file_parts[0]}_#{file_parts[2]}" : filename_without_ext
end

#dpg_folderString

Returns DPG subfolder for the given filename, i.e. ‘00’,‘05’, etc.

Examples:

source_file = Assembly::ObjectFile.new('/input/cy565rm7188_00_001.tif')
puts source_file.dpg_folder # "00"

Returns:

  • (String)

    DPG subfolder for the given filename, i.e. ‘00’,‘05’, etc.



49
50
51
52
# File 'lib/assembly-objectfile/object_fileable.rb', line 49

def dpg_folder
  file_parts = File.basename(path, ext).split('_')
  file_parts.size == 3 ? file_parts[1] : ''
end

#encodingString

Note:

Uses shell call to “file”, only expected to work on unix based systems

Returns encoding for supplied file.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.txt')
puts source_file.encoding # 'us-ascii'

Returns:

  • (String)

    encoding for supplied file



189
190
191
192
193
194
# File 'lib/assembly-objectfile/object_fileable.rb', line 189

def encoding
  @encoding ||= begin
    check_for_file
    `file --mime-encoding "#{path}"`.delete("\n").split(':')[1].strip
  end
end

#exifMiniExiftool

Returns exif information stored as a hash and an object.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.exif # hash with exif information

Returns:

  • (MiniExiftool)

    exif information stored as a hash and an object



90
91
92
93
94
95
96
97
# File 'lib/assembly-objectfile/object_fileable.rb', line 90

def exif
  @exif ||= begin
    check_for_file
    MiniExiftool.new(path, replace_invalid_chars: '?')
  rescue StandardError
    nil
  end
end

#exif_mimetypeString

Returns mimetype information for the current file based on exif data (if available and not a trusted source that we’d rather get from the file system command)

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.txt')
puts source_file.exif_mimetype # 'text/plain'

Returns:

  • (String)

    mime type for supplied file



175
176
177
178
179
180
181
182
# File 'lib/assembly-objectfile/object_fileable.rb', line 175

def exif_mimetype
  @exif_mimetype ||= begin
    check_for_file
    prefer_exif = !Assembly::TRUSTED_MIMETYPES.include?(file_mimetype) # if it's not a "trusted" mimetype and there is exif data; get the mimetype from the exif
    exif.mimetype if
exif&.mimetype && prefer_exif
  end
end

#extString

Returns filename extension.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.ext # ".tif"

Returns:

  • (String)

    filename extension



74
75
76
# File 'lib/assembly-objectfile/object_fileable.rb', line 74

def ext
  File.extname(path)
end

#extension_mimetypeString

Returns mimetype information using the mime-types gem (based on a file extension lookup)

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.txt')
puts source_file.extension_mimetype # 'text/plain'

Returns:

  • (String)

    mime type for supplied file



151
152
153
154
155
156
# File 'lib/assembly-objectfile/object_fileable.rb', line 151

def extension_mimetype
  @extension_mimetype ||= begin
    mtype = MIME::Types.type_for(path).first
    mtype ? mtype.content_type : ''
  end
end

#file_exists?Boolean

Determines if the file exists (and is not a directory)

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.file_exists? # true

Returns:

  • (Boolean)

    file exists



262
263
264
# File 'lib/assembly-objectfile/object_fileable.rb', line 262

def file_exists?
  @file_exists ||= (File.exist?(path) && !File.directory?(path))
end

#file_mimetypeString

Returns mimetype information for the current file based on unix file system command.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.txt')
puts source_file.file_mimetype # 'text/plain'

Returns:

  • (String)

    mime type for supplied file



163
164
165
166
167
168
# File 'lib/assembly-objectfile/object_fileable.rb', line 163

def file_mimetype
  @file_mimetype ||= begin
    check_for_file
    `file --mime-type "#{path}"`.delete("\n").split(':')[1].strip # first try and get the mimetype from the unix file command
  end
end

#filenameString

Returns base filename.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.filename # "path_to_file.tif"

Returns:

  • (String)

    base filename



58
59
60
# File 'lib/assembly-objectfile/object_fileable.rb', line 58

def filename
  File.basename(path)
end

#filename_without_extString

Returns base filename without extension.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.filename # "path_to_file"

Returns:

  • (String)

    base filename without extension



82
83
84
# File 'lib/assembly-objectfile/object_fileable.rb', line 82

def filename_without_ext
  File.basename(path, ext)
end

#filesizeInteger

Returns file size information for the current file in bytes.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.filesize # 1345

Returns:

  • (Integer)

    file size in bytes



252
253
254
255
# File 'lib/assembly-objectfile/object_fileable.rb', line 252

def filesize
  check_for_file
  @filesize ||= File.size(path)
end

#has_color_profile?Boolean

Returns true if image has a color profile, false if not.

Examples:

source_img = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_img.has_color_profile? # true

Returns:

  • (Boolean)

    true if image has a color profile, false if not.



229
230
231
232
233
# File 'lib/assembly-objectfile/object_fileable.rb', line 229

def has_color_profile?
  return false unless exif

  exif['profiledescription'] || exif['colorspace'] ? true : false
end

#image?Boolean

Returns if object is an image.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.image? # true

Returns:

  • (Boolean)

    if object is an image



209
210
211
# File 'lib/assembly-objectfile/object_fileable.rb', line 209

def image?
  object_type == :image
end

#initialize(path, params = {}) ⇒ Object

Examples:

Assembly::ObjectFile.new('/input/path_to_file.tif')

Parameters:

  • path (String)

    full path to the file to be worked with

  • params (Hash<Symbol => Object>) (defaults to: {})

    options used during content metadata generation

Options Hash (params):

  • :file_attributes (Hash<Symbol => ['yes', 'no']>)

    e.g. :preserve=>‘yes’,:shelve=>‘no’,:publish=>‘no’, defaults pulled from mimetype

  • :label (String)

    a resource label (files bundlded together will just get the first file’s label attribute if set)

  • :provider_md5 (String)

    pre-computed MD5 checksum

  • :provider_sha1 (String)

    pre-computed SHA1 checksum

  • :relative_path (String)

    if you want the file ids in the content metadata it can be set, otherwise content metadata will get the full path

  • :mime_type_order (Array)

    can be set to the order in which you want mimetypes to be determined options are :override (from manual overide mapping if exists), :exif (from exif if exists),

    :extension (from file extension), and :file (from unix file system command)
    

    the default is defined in the private ‘default_mime_type_order` method but you can override to set your own order



26
27
28
29
30
31
32
33
34
# File 'lib/assembly-objectfile/object_fileable.rb', line 26

def initialize(path, params = {})
  @path = path
  @label = params[:label]
  @file_attributes = params[:file_attributes]
  @relative_path = params[:relative_path]
  @provider_md5 = params[:provider_md5]
  @provider_sha1 = params[:provider_sha1]
  @mime_type_order = params[:mime_type_order] || default_mime_type_order
end

#jp2able?Boolean

Examines the input image for validity to create a jp2. Same as valid_image? but also confirms the existence of a profile description and further restricts mimetypes. It is used by the assembly robots to decide if a jp2 will be created and is also called before you create a jp2 using assembly-image.

Examples:

source_img = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_img.jp2able? # true

Returns:

  • (Boolean)

    true if image should have a jp2 created, false if not.



241
242
243
244
245
# File 'lib/assembly-objectfile/object_fileable.rb', line 241

def jp2able?
  return false unless exif

  Assembly::VALID_IMAGE_MIMETYPES.include?(mimetype)
end

#md5String

Computes md5 checksum or returns cached value

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.md5 # 'XXX123XXX1243XX1243'

Returns:

  • (String)

    md5 checksum



104
105
106
107
# File 'lib/assembly-objectfile/object_fileable.rb', line 104

def md5
  check_for_file unless @md5
  @md5 ||= Digest::MD5.file(path).hexdigest
end

#mimetypeString

Returns mimetype information for the current file based on the ordering set in default_mime_type_order

We stop computing mimetypes as soon as we have a method that returns a value

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.txt')
puts source_file.mimetype # 'text/plain'

Returns:

  • (String)

    mime type



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/assembly-objectfile/object_fileable.rb', line 125

def mimetype
  @mimetype ||= begin
    check_for_file
    mimetype = ''
    mime_type_order.each do |mime_type_method|
      mimetype = public_send("#{mime_type_method}_mimetype") if VALID_MIMETYPE_METHODS.include?(mime_type_method)
      break if mimetype.present?
    end
    mimetype
  end
end

#object_typeSymbol

Returns the type of object, could be :application (for PDF or Word, etc), :audio, :image, :message, :model, :multipart, :text or :video.

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.object_type # :image

Returns:

  • (Symbol)

    the type of object, could be :application (for PDF or Word, etc), :audio, :image, :message, :model, :multipart, :text or :video



200
201
202
203
# File 'lib/assembly-objectfile/object_fileable.rb', line 200

def object_type
  lookup = MIME::Types[mimetype][0]
  lookup.nil? ? :other : lookup.media_type.to_sym
end

#override_mimetypeString

Returns mimetype information using the manual override mapping (based on a file extension lookup)

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.json')
puts source_file.override_mimetype # 'application/json'

Returns:

  • (String)

    mime type for supplied file if a mapping exists for the file’s extension



142
143
144
# File 'lib/assembly-objectfile/object_fileable.rb', line 142

def override_mimetype
  @override_mimetype ||= Assembly::OVERRIDE_MIMETYPES.fetch(ext.to_sym, '')
end

#sha1String

Computes sha1 checksum or return cached value

Examples:

source_file = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_file.sha1 # 'XXX123XXX1243XX1243'

Returns:

  • (String)

    sha1 checksum



114
115
116
117
# File 'lib/assembly-objectfile/object_fileable.rb', line 114

def sha1
  check_for_file unless @sha1
  @sha1 ||= Digest::SHA1.file(path).hexdigest
end

#valid_image?Boolean

Examines the input image for validity. Used to determine if image is a valid and useful image. If image is not a jp2, also checks if it is jp2able?

Examples:

source_img = Assembly::ObjectFile.new('/input/path_to_file.tif')
puts source_img.valid_image? # true

Returns:

  • (Boolean)

    true if image is valid, false if not.



219
220
221
222
223
# File 'lib/assembly-objectfile/object_fileable.rb', line 219

def valid_image?
  return false unless image?

  mimetype == 'image/jp2' || jp2able?
end