Module: Assembly::ObjectFileable
- Included in:
- ObjectFile
- Defined in:
- lib/assembly-objectfile/object_fileable.rb
Overview
Common behaviors we need for other classes in the gem
Constant Summary collapse
- VALID_MIMETYPE_METHODS =
%i[exif file extension].freeze
Instance Attribute Summary collapse
-
#file_attributes ⇒ Object
Returns the value of attribute file_attributes.
-
#label ⇒ Object
Returns the value of attribute label.
-
#mime_type_order ⇒ Object
Returns the value of attribute mime_type_order.
-
#path ⇒ Object
Returns the value of attribute path.
-
#provider_md5 ⇒ Object
Returns the value of attribute provider_md5.
-
#provider_sha1 ⇒ Object
Returns the value of attribute provider_sha1.
-
#relative_path ⇒ Object
Returns the value of attribute relative_path.
Instance Method Summary collapse
-
#dirname ⇒ String
Base directory.
-
#dpg_basename ⇒ String
DPG base filename, removing the extension and the ‘00’,‘05’, etc.
-
#dpg_folder ⇒ String
DPG subfolder for the given filename, i.e.
-
#encoding ⇒ String
Encoding for supplied file.
-
#exif ⇒ MiniExiftool
Exif information stored as a hash and an object.
-
#exif_mimetype ⇒ String
Returns mimetype information for the current file based on exif data (if available and not a trusted source that we’d rather get from the file system command).
-
#ext ⇒ String
Filename extension.
-
#extension_mimetype ⇒ String
Returns mimetype information using the mime-types gem (based on a file extension lookup).
-
#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.
-
#filename ⇒ String
Base filename.
-
#filename_without_ext ⇒ String
Base filename without extension.
-
#filesize ⇒ Integer
Returns file size information for the current file in bytes.
-
#has_color_profile? ⇒ Boolean
True if image has a color profile, false if not.
-
#image? ⇒ Boolean
If object is an image.
- #initialize(path, params = {}) ⇒ Object
-
#jp2able? ⇒ Boolean
Examines the input image for validity to create a jp2.
-
#md5 ⇒ String
Computes md5 checksum or returns cached value.
-
#mimetype ⇒ String
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.
-
#object_type ⇒ Symbol
The type of object, could be :application (for PDF or Word, etc), :audio, :image, :message, :model, :multipart, :text or :video.
-
#sha1 ⇒ String
Computes sha1 checksum or return cached value.
-
#valid_image? ⇒ Boolean
Examines the input image for validity.
Instance Attribute Details
#file_attributes ⇒ Object
Returns the value of attribute file_attributes.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def file_attributes @file_attributes end |
#label ⇒ Object
Returns the value of attribute label.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def label @label end |
#mime_type_order ⇒ Object
Returns the value of attribute mime_type_order.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def mime_type_order @mime_type_order end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def path @path end |
#provider_md5 ⇒ Object
Returns the value of attribute provider_md5.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def provider_md5 @provider_md5 end |
#provider_sha1 ⇒ Object
Returns the value of attribute provider_sha1.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def provider_sha1 @provider_sha1 end |
#relative_path ⇒ Object
Returns the value of attribute relative_path.
9 10 11 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 9 def relative_path @relative_path end |
Instance Method Details
#dirname ⇒ String
Returns base directory.
65 66 67 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 65 def dirname File.dirname(path) end |
#dpg_basename ⇒ String
Returns DPG base filename, removing the extension and the ‘00’,‘05’, etc. placeholders.
39 40 41 42 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 39 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 given filename, i.e. ‘00’,‘05’, etc.
48 49 50 51 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 48 def dpg_folder file_parts = File.basename(path, ext).split('_') file_parts.size == 3 ? file_parts[1] : '' end |
#encoding ⇒ String
Uses shell call to “file”, only expected to work on unix based systems
Returns encoding for supplied file.
179 180 181 182 183 184 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 179 def encoding @encoding ||= begin check_for_file `file --mime-encoding "#{path}"`.delete("\n").split(':')[1].strip end end |
#exif ⇒ MiniExiftool
Returns exif information stored as a hash and an object.
89 90 91 92 93 94 95 96 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 89 def exif @exif ||= begin check_for_file MiniExiftool.new(path, replace_invalid_chars: '?') rescue StandardError nil end end |
#exif_mimetype ⇒ String
Returns mimetype information for the current file based on exif data (if available and not a trusted source that we’d rather get from the file system command)
165 166 167 168 169 170 171 172 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 165 def exif_mimetype @exif_mimetype ||= begin check_for_file prefer_exif = !Assembly::TRUSTED_MIMETYPES.include?(file_mimetype) # if it's not a "trusted" mimetype and there is exif data; get the mimetype from the exif exif.mimetype if exif&.mimetype && prefer_exif end end |
#ext ⇒ String
Returns filename extension.
73 74 75 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 73 def ext File.extname(path) end |
#extension_mimetype ⇒ String
Returns mimetype information using the mime-types gem (based on a file extension lookup)
141 142 143 144 145 146 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 141 def extension_mimetype @extension_mimetype ||= begin mtype = MIME::Types.type_for(path).first mtype ? mtype.content_type : '' end end |
#file_exists? ⇒ Boolean
Determines if the file exists (and is not a directory)
252 253 254 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 252 def file_exists? @file_exists ||= (File.exist?(path) && !File.directory?(path)) end |
#file_mimetype ⇒ String
Returns mimetype information for the current file based on unix file system command.
153 154 155 156 157 158 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 153 def file_mimetype @file_mimetype ||= begin check_for_file `file --mime-type "#{path}"`.delete("\n").split(':')[1].strip # first try and get the mimetype from the unix file command end end |
#filename ⇒ String
Returns base filename.
57 58 59 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 57 def filename File.basename(path) end |
#filename_without_ext ⇒ String
Returns base filename without extension.
81 82 83 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 81 def filename_without_ext File.basename(path, ext) end |
#filesize ⇒ Integer
Returns file size information for the current file in bytes.
242 243 244 245 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 242 def filesize check_for_file @filesize ||= File.size(path) end |
#has_color_profile? ⇒ Boolean
Returns true if image has a color profile, false if not.
219 220 221 222 223 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 219 def has_color_profile? return false unless exif exif['profiledescription'] || exif['colorspace'] ? true : false end |
#image? ⇒ Boolean
Returns if object is an image.
199 200 201 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 199 def image? object_type == :image end |
#initialize(path, params = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 25 def initialize(path, params = {}) @path = path @label = params[:label] @file_attributes = params[:file_attributes] @relative_path = params[:relative_path] @provider_md5 = params[:provider_md5] @provider_sha1 = params[:provider_sha1] @mime_type_order = params[:mime_type_order] || default_mime_type_order 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.
231 232 233 234 235 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 231 def jp2able? return false unless exif Assembly::VALID_IMAGE_MIMETYPES.include?(mimetype) end |
#md5 ⇒ String
Computes md5 checksum or returns cached value
103 104 105 106 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 103 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 the ordering set in default_mime_type_order
We stop computing mimetypes as soon as we have a method that returns a value
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 124 def mimetype @mimetype ||= begin check_for_file mimetype = '' mime_type_order.each do |mime_type_method| mimetype = public_send("#{mime_type_method}_mimetype") if VALID_MIMETYPE_METHODS.include?(mime_type_method) break if !mimetype.nil? && mimetype != '' end mimetype end end |
#object_type ⇒ Symbol
Returns the type of object, could be :application (for PDF or Word, etc), :audio, :image, :message, :model, :multipart, :text or :video.
190 191 192 193 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 190 def object_type lookup = MIME::Types[mimetype][0] lookup.nil? ? :other : lookup.media_type.to_sym end |
#sha1 ⇒ String
Computes sha1 checksum or return cached value
113 114 115 116 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 113 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?
209 210 211 212 213 |
# File 'lib/assembly-objectfile/object_fileable.rb', line 209 def valid_image? return false unless image? mimetype == 'image/jp2' || jp2able? end |