Class: FileWrapper

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

Overview

FileWrapper wraps the ‘file’ command line utility.

Class Method Summary collapse

Class Method Details

.get_mime(filename) ⇒ Object

Extract the mime type from a file.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
# File 'lib/file_wrapper.rb', line 5

def get_mime(filename)
  raise ArgumentError, "Invalid file" unless File.exist? filename and File.file? filename and File.readable? filename
   file_output = `file --mime "#{filename}" 2>&1`.strip
   matches = file_output.match(/.+?:\s+([\/\d\w-]+\/[\/\d\w-]+)/)
   if matches
     return matches[1]
   else
     return nil
   end
end