Class: Assembly::ObjectFile
- Inherits:
-
Object
- Object
- Assembly::ObjectFile
- Defined in:
- lib/assembly/object_file.rb,
lib/assembly/object_file/version.rb
Overview
This class contains generic methods to operate on any file.
Constant Summary collapse
- VALID_MIMETYPE_METHODS =
%i[override exif file extension].freeze
- VERSION =
Gem version
'2.1.0'
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.
Class Method Summary collapse
-
.common_path(strings) ⇒ String
Class level method that given an array of strings, return the longest common initial path.
Instance Method Summary collapse
-
#dirname ⇒ String
Base directory.
-
#exif ⇒ MiniExiftool
Exif information stored as a hash and an object.
-
#ext ⇒ String
Filename extension.
-
#file_exists? ⇒ Boolean
Determines if the file exists (and is not a directory).
-
#filename ⇒ String
Base filename.
-
#filename_without_ext ⇒ String
Base filename without extension.
-
#filesize ⇒ Integer
Returns file size information for the current file in bytes.
-
#image? ⇒ Boolean
If object is an image.
-
#initialize(path, params = {}) ⇒ ObjectFile
constructor
A new instance of ObjectFile.
-
#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.
Constructor Details
#initialize(path, params = {}) ⇒ ObjectFile
Returns a new instance of ObjectFile.
57 58 59 60 61 62 63 64 65 |
# File 'lib/assembly/object_file.rb', line 57 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 |
Instance Attribute Details
#file_attributes ⇒ Object
Returns the value of attribute file_attributes.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def file_attributes @file_attributes end |
#label ⇒ Object
Returns the value of attribute label.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def label @label end |
#mime_type_order ⇒ Object
Returns the value of attribute mime_type_order.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def mime_type_order @mime_type_order end |
#path ⇒ Object
Returns the value of attribute path.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def path @path end |
#provider_md5 ⇒ Object
Returns the value of attribute provider_md5.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def provider_md5 @provider_md5 end |
#provider_sha1 ⇒ Object
Returns the value of attribute provider_sha1.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def provider_sha1 @provider_sha1 end |
#relative_path ⇒ Object
Returns the value of attribute relative_path.
33 34 35 |
# File 'lib/assembly/object_file.rb', line 33 def relative_path @relative_path end |
Class Method Details
.common_path(strings) ⇒ String
Class level method that given an array of strings, return the longest common initial path. Useful for removing a common path from a set of filenames when producing content metadata
Example:
puts Assembly::ObjectFile.common_prefix(['/Users/peter/00/test.tif','/Users/peter/05/test.jp2'])
# => '/Users/peter/0'
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/assembly/object_file.rb', line 19 def self.common_path(strings) return nil if strings.empty? n = 0 x = strings.last n += 1 while strings.all? { |s| s[n] && (s[n] == x[n]) } common_prefix = x[0...n] if common_prefix[-1, 1] == '/' # check if last element of the common string is the end of a directory common_prefix # if not, split string along directories, and reject last one else "#{common_prefix.split('/')[0..-2].join('/')}/" # if it was, then return the common prefix directly end end |
Instance Method Details
#dirname ⇒ String
Returns base directory.
79 80 81 |
# File 'lib/assembly/object_file.rb', line 79 def dirname File.dirname(path) end |
#exif ⇒ MiniExiftool
Returns exif information stored as a hash and an object.
103 104 105 106 107 108 |
# File 'lib/assembly/object_file.rb', line 103 def exif @exif ||= begin check_for_file MiniExiftool.new(path, replace_invalid_chars: '?') end end |
#ext ⇒ String
Returns filename extension.
87 88 89 |
# File 'lib/assembly/object_file.rb', line 87 def ext File.extname(path) end |
#file_exists? ⇒ Boolean
Determines if the file exists (and is not a directory)
207 208 209 |
# File 'lib/assembly/object_file.rb', line 207 def file_exists? @file_exists ||= (File.exist?(path) && !File.directory?(path)) end |
#filename ⇒ String
Returns base filename.
71 72 73 |
# File 'lib/assembly/object_file.rb', line 71 def filename File.basename(path) end |
#filename_without_ext ⇒ String
Returns base filename without extension.
95 96 97 |
# File 'lib/assembly/object_file.rb', line 95 def filename_without_ext File.basename(path, ext) end |
#filesize ⇒ Integer
Returns file size information for the current file in bytes.
197 198 199 200 |
# File 'lib/assembly/object_file.rb', line 197 def filesize check_for_file @filesize ||= File.size(path) end |
#image? ⇒ Boolean
Returns if object is an image.
162 163 164 |
# File 'lib/assembly/object_file.rb', line 162 def image? object_type == :image 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.
186 187 188 189 190 |
# File 'lib/assembly/object_file.rb', line 186 def jp2able? return false unless exif Assembly::VALID_IMAGE_MIMETYPES.include?(mimetype) end |
#md5 ⇒ String
Computes md5 checksum or returns cached value
115 116 117 118 |
# File 'lib/assembly/object_file.rb', line 115 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
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/assembly/object_file.rb', line 136 def mimetype @mimetype ||= begin check_for_file mimetype = '' mime_type_order.each do |mime_type_method| mimetype = send("#{mime_type_method}_mimetype") if VALID_MIMETYPE_METHODS.include?(mime_type_method) break if mimetype.present? 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.
153 154 155 156 |
# File 'lib/assembly/object_file.rb', line 153 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
125 126 127 128 |
# File 'lib/assembly/object_file.rb', line 125 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?
172 173 174 175 176 |
# File 'lib/assembly/object_file.rb', line 172 def valid_image? return false unless image? mimetype == 'image/jp2' || jp2able? end |