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 metadata 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"


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"


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"


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'


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

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


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,replace_invalid_chars: '?')
  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"


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


281
282
283
# File 'lib/assembly-objectfile/object_fileable.rb', line 281

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

#file_mimetypestring

Returns mimetype information for the current file based on unix file system command or exif data (if available).

Example:

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


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

def file_mimetype
  check_for_file unless @file_mimetype
  if @file_mimetype.nil? # if we haven't computed it yet once for this object, try and get the mimetype
    @file_mimetype = `file --mime-type "#{@path}"`.gsub(/\n/,"").split(':')[1].strip # first try and get the mimetype from the unix file command
    @file_mimetype = exif.mimetype if (!Assembly::TRUSTED_MIMETYPES.include?(@file_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 @file_mimetype 
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"


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"


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


268
269
270
271
# File 'lib/assembly-objectfile/object_fileable.rb', line 268

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

#has_color_profile?boolean

Examines the input image for a color profile.

Example:

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


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

def has_color_profile?
  exif.nil? ? false : (!exif['profiledescription'].nil? || !exif['colorspace'].nil?) # check for existence of profile description  
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


213
214
215
# File 'lib/assembly-objectfile/object_fileable.rb', line 213

def image?
  object_type == :image 
end

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

Initialize file from given path.

Example:

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


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


251
252
253
254
255
256
257
258
259
# File 'lib/assembly-objectfile/object_fileable.rb', line 251

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
  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


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 based on file extension or exif data (if available)

Example:

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


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

def mimetype
  if @mimetype.nil? # if we haven't computed it yet once for this object, try and get the mimetype
    if (!exif.nil? && !exif.mimetype.nil?)  # try and get the mimetype from the exif data if it exists
      @mimetype = exif.mimetype
    else # otherwise get it from the mime-types gem (using the file extension) assuming we can find, if not, return blank     
      mimetype = MIME::Types.type_for(@path).first
      @mimetype= mimetype ? mimetype.content_type : ''
    end
  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


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

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


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 if it is jp2able?

Example:

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


224
225
226
227
228
229
230
231
# File 'lib/assembly-objectfile/object_fileable.rb', line 224

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