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
-
#file_attributes ⇒ Object
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.
-
#label ⇒ Object
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).
-
#path ⇒ Object
path is the full path to the user provided image.
-
#provider_md5 ⇒ Object
provider checksums are optional checksums given by the provider used in content metadata generation.
-
#provider_sha1 ⇒ Object
provider checksums are optional checksums given by the provider used in content metadata generation.
-
#relative_path ⇒ Object
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.
Instance Method Summary collapse
-
#dirname ⇒ String
Returns base directory path for the current file.
-
#dpg_basename ⇒ String
Returns base DPG name for the current file.
-
#dpg_folder ⇒ String
Returns DPG subfolder for the current file.
-
#encoding ⇒ string
Returns encoding information for the current file (only on unix based systems).
-
#exif ⇒ MiniExiftool
Returns exif information for the current file.
-
#ext ⇒ String
Returns filename extension.
-
#file_exists? ⇒ boolean
Determines if the file exists (and is not a directory).
-
#filename ⇒ String
Returns base filename for the current file.
-
#filename_without_ext ⇒ String
Returns base filename without extension for the current file.
-
#filesize ⇒ integer
Returns file size information for the current file in bytes.
-
#has_color_profile? ⇒ boolean
Examines the input image for a color profile.
-
#image? ⇒ boolean
Returns if the object file is an image.
-
#initialize(path, params = {}) ⇒ Object
Initialize file from given path.
-
#jp2able? ⇒ boolean
Examines the input image for validity to create a jp2.
-
#md5 ⇒ string
Compute md5 checksum or return value if already computed.
-
#mimetype ⇒ string
Returns mimetype information for the current file (only on unix based systems).
-
#object_type ⇒ symbol
Returns a symbol with the objects type.
-
#sha1 ⇒ string
Compute sha1 checksum or return value if already computed.
-
#valid_image? ⇒ boolean
Examines the input image for validity.
Instance Attribute Details
#file_attributes ⇒ Object
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 |
#label ⇒ Object
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 |
#path ⇒ Object
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_md5 ⇒ Object
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_sha1 ⇒ Object
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_path ⇒ Object
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
#dirname ⇒ String
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_basename ⇒ String
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_folder ⇒ String
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 |
#encoding ⇒ string
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'
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 |
#exif ⇒ MiniExiftool
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,:convert_encoding=>true) rescue @exif = nil end end |
#ext ⇒ String
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
261 262 263 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 261 def file_exists? File.exists?(@path) && !File.directory?(@path) end |
#filename ⇒ String
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_ext ⇒ String
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 |
#filesize ⇒ integer
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
248 249 250 251 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 248 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
220 221 222 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 220 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
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')
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
231 232 233 234 235 236 237 238 239 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 231 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 |
#md5 ⇒ string
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 |
#mimetype ⇒ string
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'
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_type ⇒ symbol
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
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 |
#sha1 ⇒ string
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
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 |