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).
-
#file_mimetype ⇒ string
Returns mimetype information for the current file based on unix file system command or exif data (if available).
-
#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 based on file extension or exif data (if available).
-
#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 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
#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'
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 |
#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,replace_invalid_chars: '?') 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
281 282 283 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 281 def file_exists? File.exists?(@path) && !File.directory?(@path) end |
#file_mimetype ⇒ string
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 |
#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
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
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 |
#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 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_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
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 |
#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
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 |