Class: Assembly::ObjectFile

Inherits:
Object
  • Object
show all
Defined in:
lib/assembly/object_file.rb,
lib/assembly/object_file/version.rb

Overview

This class contains generic methods to operate on any file.

Constant Summary collapse

VALID_MIMETYPE_METHODS =
%i[override exif file extension].freeze
VERSION =

Gem version

'2.2.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ObjectFile.

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 bundled together will just get the first file’s label attribute if set)

  • :provider_md5 (String)

    pre-computed MD5 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)
    :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



51
52
53
54
55
56
57
58
# File 'lib/assembly/object_file.rb', line 51

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

Instance Attribute Details

#file_attributesObject

Returns the value of attribute file_attributes.



30
31
32
# File 'lib/assembly/object_file.rb', line 30

def file_attributes
  @file_attributes
end

#labelObject

Returns the value of attribute label.



30
31
32
# File 'lib/assembly/object_file.rb', line 30

def label
  @label
end

#mime_type_orderObject

Returns the value of attribute mime_type_order.



30
31
32
# File 'lib/assembly/object_file.rb', line 30

def mime_type_order
  @mime_type_order
end

#pathObject

Returns the value of attribute path.



30
31
32
# File 'lib/assembly/object_file.rb', line 30

def path
  @path
end

#provider_md5Object

Returns the value of attribute provider_md5.



30
31
32
# File 'lib/assembly/object_file.rb', line 30

def provider_md5
  @provider_md5
end

#relative_pathObject

Returns the value of attribute relative_path.



30
31
32
# File 'lib/assembly/object_file.rb', line 30

def relative_path
  @relative_path
end

Class Method Details

.common_path(strings) ⇒ String

Example:

puts Assembly::ObjectFile.common_path(['/Users/peter/00/test.tif','/Users/peter/05/test.jp2'])
# => '/Users/peter/'

Parameters:

  • strings (Array)

    Array of filenames with paths

Returns:

  • (String)

    longest common initial path of filenames passed in



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/assembly/object_file.rb', line 16

def self.common_path(strings)
  return nil if strings.empty?

  n = 0
  x = strings.last
  n += 1 while strings.all? { |s| s[n] && (s[n] == x[n]) }
  common_prefix = x[0...n]
  if common_prefix[-1, 1] == '/' # check if last element of the common string is the end of a directory
    common_prefix # if not, split string along directories, and reject last one
  else
    "#{common_prefix.split('/')[0..-2].join('/')}/" # if it was, then return the common prefix directly
  end
end

Instance Method Details

#dirnameObject



64
65
66
# File 'lib/assembly/object_file.rb', line 64

def dirname
  File.dirname(path)
end

#exifMiniExiftool

Returns exif mini_exiftool gem object wrapper for exiftool.

Returns:

  • (MiniExiftool)

    exif mini_exiftool gem object wrapper for exiftool



77
78
79
80
81
82
83
84
85
86
# File 'lib/assembly/object_file.rb', line 77

def exif
  @exif ||= begin
    check_for_file
    MiniExiftool.new(path, replace_invalid_chars: '?')
  rescue MiniExiftool::Error
    # MiniExiftool may raise an error on files it doesn't know how to handle (disk images for example)
    # but we don't want this to prevent an ObjectFile from being created, so just swallow it.
    nil
  end
end

#extObject



68
69
70
# File 'lib/assembly/object_file.rb', line 68

def ext
  File.extname(path)
end

#file_exists?Boolean

Returns file exists and is not a directory.

Returns:

  • (Boolean)

    file exists and is not a directory



154
155
156
# File 'lib/assembly/object_file.rb', line 154

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

#filenameObject



60
61
62
# File 'lib/assembly/object_file.rb', line 60

def filename
  File.basename(path)
end

#filename_without_extObject



72
73
74
# File 'lib/assembly/object_file.rb', line 72

def filename_without_ext
  File.basename(path, ext)
end

#filesizeInteger

Returns file size in bytes.

Returns:

  • (Integer)

    file size in bytes



148
149
150
151
# File 'lib/assembly/object_file.rb', line 148

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

#image?Boolean

Returns true if the mime-types gem recognizes it as an image.

Returns:

  • (Boolean)

    true if the mime-types gem recognizes it as an image



123
124
125
126
127
128
129
130
# File 'lib/assembly/object_file.rb', line 123

def image?
  return false if object_type != :image

  # We exclude TARGA images here because we've seen where the file is a disk image and
  # when we look for a mime type it is `image/x-tga', however it is not
  # recognizable by exiftool.  See https://github.com/sul-dlss/assembly-objectfile/issues/98
  mimetype != 'image/x-tga'
end

#jp2able?Boolean

Returns true if we can create a jp2 from the file.

Returns:

  • (Boolean)

    true if we can create a jp2 from the file



141
142
143
144
145
# File 'lib/assembly/object_file.rb', line 141

def jp2able?
  return false unless exif

  Assembly::VALID_IMAGE_MIMETYPES.include?(mimetype)
end

#md5String

Returns computed md5 checksum.

Returns:

  • (String)

    computed md5 checksum



89
90
91
92
# File 'lib/assembly/object_file.rb', line 89

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

Returns:

  • (String)

    mimetype of the file



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/assembly/object_file.rb', line 103

def mimetype
  @mimetype ||= begin
    check_for_file
    mimetype = ''
    mime_type_order.each do |mime_type_method|
      mimetype = 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.

Returns:

  • (Symbol)

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



117
118
119
120
# File 'lib/assembly/object_file.rb', line 117

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

#sha1String

Returns computed sha1 checksum.

Returns:

  • (String)

    computed sha1 checksum



95
96
97
98
# File 'lib/assembly/object_file.rb', line 95

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

#valid_image?Boolean

Returns true if the mime-types gem recognizes it as an image AND it is a jp2 or jp2able?.

Returns:

  • (Boolean)

    true if the mime-types gem recognizes it as an image AND it is a jp2 or jp2able?



134
135
136
137
138
# File 'lib/assembly/object_file.rb', line 134

def valid_image?
  return false unless image?

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