Class: CHECKING::YOU

Inherits:
Object
  • Object
show all
Defined in:
lib/distorted/checking_you_out.rb

Defined Under Namespace

Classes: OUT

Class Method Summary collapse

Class Method Details

.IN(wanted_type_or_types) ⇒ Object

Returns a Set of MIME::Type objects matching a String search key of the format MEDIA_TYPE/SUB_TYPE. This can return multiple Types, e.g. ‘font/collection’ TTC/OTC variations:

#<MIME::Type: font/collection>, #<MIME::Type: font/collection>


158
159
160
161
162
163
164
165
166
167
# File 'lib/distorted/checking_you_out.rb', line 158

def self.IN(wanted_type_or_types)
  if wanted_type_or_types.is_a?(Enumerable)
    # Support taking a list of String types for Molecules whose Type support
    # isn't easily expressable as a single Regexp.
    types.select{ |type| wanted_type_or_types.include?(type.to_s) }
  else
    # Might be a single String or Regexp
    types[wanted_type_or_types, :complete => wanted_type_or_types.is_a?(Regexp)].to_set
  end
end

.OUT(path, so_deep: false, only_one_test: false) ⇒ Object

Returns a Set of MIME::Type for a given file path, by default only based on the file extension. If the file extension is unavailable— or if ‘so_deep` is enabled—the `path` will be used as an actual path to look at the magic bytes with ruby-filemagic.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/distorted/checking_you_out.rb', line 112

def self.OUT(path, so_deep: false, only_one_test: false)
  return Set[] if path.nil?
  if not (only_one_test || types.type_for(path).empty?)
    # NOTE: `type_for`'s return order is supposed to be deterministic:
    # https://github.com/mime-types/ruby-mime-types/issues/148
    # My use case so far has never required order but has required
    # many Set comparisons, so I am going to return a Set here
    # and possibly throw the order away.
    # In my experience the order is usually preserved anyway:
    # irb(main)> MIME::Types.type_for(File.expand_path('lol.ttf'))
    # => [#<MIME::Type: font/ttf>, #<MIME::Type: application/font-sfnt>, #<MIME::Type: application/x-font-truetype>, #<MIME::Type: application/x-font-ttf>]
    # irb(main)> MIME::Types.type_for('lol.ttf')).to_set
    # => #<Set: {#<MIME::Type: font/ttf>, #<MIME::Type: application/font-sfnt>, #<MIME::Type: application/x-font-truetype>, #<MIME::Type: application/x-font-ttf>}>
    return types.type_for(path).to_set
  elsif (so_deep && path[0] != '.'.freeze)  # Support taking hypothetical file extensions (e.g. '.jpg') without stat()ing anything.
    # Did we fail to guess any MIME::Types from the given filename?
    # We're going to have to look at the actual file
    # (or at least its first four bytes).
    FileMagic.open(:mime) do |fm|
      # The second argument makes fm.file return just the simple
      # MIME::Type String, e.g.:
      #
      # irb(main)>   fm.file('/home/okeeblow/IIDX-turntable.svg')
      # => "image/svg+xml; charset=us-ascii"
      # irb(main)>   fm.file('/home/okeeblow/IIDX-turntable.svg', true)
      # => "image/svg"
      #
      # However MIME::Types won't take short variants like 'image/svg',
      # so explicitly have FM return long types and split it ourself
      # on the semicolon:
      #
      # irb(main)> "image/svg+xml; charset=us-ascii".split(';').first
      # => "image/svg+xml"
      mime = types[fm.file(path, false).split(';'.freeze).first].to_set
    end  # FileMagic.open
  else
    # TODO: Warn here that we may need a custom type!
    #p "NO MATCH FOR #{path}"
    Set[]
  end  # if
end