Module: Assembly::ObjectFileable

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

Overview

Namespace to include common behaviors we need for other classes in the gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_attributesObject

an optional hash that is used to set the file attributes (publish,preserve,shelve) for the given file when generating content metadata (if not supplied, mimetype defaults are used) e.g. :preserve=>‘yes’,:shelve=>‘no’,:publish=>‘no’



18
19
20
# File 'lib/assembly-objectfile/object_fileable.rb', line 18

def file_attributes
  @file_attributes
end

#labelObject

an optional label that can be set for each file – if provided, this will be used as a resource label when generating content metadata (files bundlded together will just get the first’s files label attribute if set)



14
15
16
# File 'lib/assembly-objectfile/object_fileable.rb', line 14

def label
  @label
end

#pathObject

path is the full path to the user provided image



11
12
13
# File 'lib/assembly-objectfile/object_fileable.rb', line 11

def path
  @path
end

#provider_md5Object

provider checksums are optional checksums given by the provider used in content metadata generation



25
26
27
# File 'lib/assembly-objectfile/object_fileable.rb', line 25

def provider_md5
  @provider_md5
end

#provider_sha1Object

provider checksums are optional checksums given by the provider used in content metadata generation



25
26
27
# File 'lib/assembly-objectfile/object_fileable.rb', line 25

def provider_sha1
  @provider_sha1
end

#relative_pathObject

relative path is useful when generating content metadata, if you want the file ids in the content metadata to be something other than the full path, it can be set

if not, content  will get the full path


22
23
24
# File 'lib/assembly-objectfile/object_fileable.rb', line 22

def relative_path
  @relative_path
end

Instance Method Details

#dirnameString

Returns base directory path for the current file.

Example:

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

Returns:

  • (String)

    base directory



80
81
82
# File 'lib/assembly-objectfile/object_fileable.rb', line 80

def dirname
  File.dirname(path)
end

#dpg_basenameString

Returns base DPG name for the current file.

Example:

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



45
46
47
48
# File 'lib/assembly-objectfile/object_fileable.rb', line 45

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 current file.

Example:

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.



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

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

#encodingstring

Returns encoding information for the current file (only on unix based systems).

Example:

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

Returns:

  • (string)

    encoding for supplied file



169
170
171
172
# File 'lib/assembly-objectfile/object_fileable.rb', line 169

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

#exifMiniExiftool

Returns exif information for the current file.

Example:

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

Returns:

  • (MiniExiftool)

    exif information stored as a hash and an object



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

def exif
  check_for_file unless @exif
  begin
    @exif ||= MiniExiftool.new @path  
  rescue
    @exif = nil
  end
end

#extString

Returns filename extension

Example:

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

Returns:

  • (String)

    filename extension



91
92
93
# File 'lib/assembly-objectfile/object_fileable.rb', line 91

def ext
  File.extname(path)
end

#file_exists?boolean

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

Example:

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

Returns:

  • (boolean)

    file exists



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

def file_exists?
  File.exists?(@path) && !File.directory?(@path) 
end

#filenameString

Returns base filename for the current file.

Example:

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

Returns:

  • (String)

    base filename



69
70
71
# File 'lib/assembly-objectfile/object_fileable.rb', line 69

def filename
  File.basename(path)
end

#filename_without_extString

Returns base filename without extension for the current file.

Example:

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

Returns:

  • (String)

    base filename without extension



102
103
104
# File 'lib/assembly-objectfile/object_fileable.rb', line 102

def filename_without_ext
  File.basename(path,ext)
end

#filesizeinteger

Returns file size information for the current file in bytes.

Example:

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

Returns:

  • (integer)

    file size in bytes



238
239
240
241
# File 'lib/assembly-objectfile/object_fileable.rb', line 238

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

#image?boolean

Returns if the object file is an image.

Example:

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

Returns:

  • (boolean)

    if object is an image



193
194
195
# File 'lib/assembly-objectfile/object_fileable.rb', line 193

def image?
  object_type == :image 
end

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

Initialize file from given path.

Example:

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

Parameters:

  • path (String)

    full path to the file to be worked with



33
34
35
36
# File 'lib/assembly-objectfile/object_fileable.rb', line 33

def initialize(path,params={})
  @path = path
  @label = params[:label]
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. Example:

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

Returns:

  • (boolean)

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



220
221
222
223
224
225
226
227
228
229
# File 'lib/assembly-objectfile/object_fileable.rb', line 220

def jp2able?
  
  result=false
  unless exif.nil?
    result=(Assembly::VALID_IMAGE_MIMETYPES.include?(mimetype)) # check for allowed image mimetypes that can be converted to jp2
    result=(exif['profiledescription'] != nil) # check for existence of profile description
  end
  return result

end

#md5string

Compute md5 checksum or return value if already computed

Example:

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

Returns:

  • (string)

    md5 checksum for given file



129
130
131
132
# File 'lib/assembly-objectfile/object_fileable.rb', line 129

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

#mimetypestring

Returns mimetype information for the current file (only on unix based systems).

Example:

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

Returns:

  • (string)

    mime type for supplied file



153
154
155
156
157
158
159
160
# File 'lib/assembly-objectfile/object_fileable.rb', line 153

def mimetype
  check_for_file unless @mimetype
  if @mimetype.nil? # if we haven't computed it yet once for this object, try and get the mimetype
    @mimetype = `file --mime-type "#{@path}"`.gsub(/\n/,"").split(':')[1].strip # first try and get the mimetype from the unix file command
    @mimetype = exif.mimetype if (!Assembly::TRUSTED_MIMETYPES.include?(@mimetype) && !exif.nil? && !exif.mimetype.nil?)  # if it's not a "trusted" mimetype and there is exif data; get the mimetype from the exif
  end
  return @mimetype 
end

#object_typesymbol

Returns a symbol with the objects type

Example:

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

Returns:

  • (symbol)

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



181
182
183
184
# File 'lib/assembly-objectfile/object_fileable.rb', line 181

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

#sha1string

Compute sha1 checksum or return value if already computed

Example:

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

Returns:

  • (string)

    sha1 checksum for given file



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

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 for a valid profile.

Example:

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

Returns:

  • (boolean)

    true if image is valid, false if not.



204
205
206
207
208
209
210
211
# File 'lib/assembly-objectfile/object_fileable.rb', line 204

def valid_image?  
  
  result= image? ? true : false
  result= jp2able? unless mimetype == 'image/jp2' # further checks if we are not already a jp2

  return result
  
end